From 7020a502738fcda934dc18fa1bef1194223b9c00 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sun, 26 Dec 2021 01:34:25 +0400
Subject: [PATCH] Updated deploy.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 .gitignore                            |  1 +
 .gitlab-ci.yml                        | 37 +++++++++++----------
 deploy/helm/.helmignore               | 23 ++++++++++++++
 deploy/helm/Chart.yaml                | 24 ++++++++++++++
 deploy/helm/files/dockerauth.json     |  8 +++++
 deploy/helm/templates/deployment.yml  | 41 ++++++++++++++++++++++++
 deploy/helm/templates/ingress.yml     |  7 ++++
 deploy/helm/templates/pull_secret.yml |  9 ++++++
 deploy/helm/templates/service.yml     | 12 +++++++
 deploy/helm/values.yaml               | 46 +++++++++++++++++++++++++++
 deploy/kube/deployment.yml            | 35 --------------------
 deploy/kube/ingress.yml               | 19 -----------
 deploy/kube/service.yml               | 12 -------
 13 files changed, 192 insertions(+), 82 deletions(-)
 create mode 100644 deploy/helm/.helmignore
 create mode 100644 deploy/helm/Chart.yaml
 create mode 100644 deploy/helm/files/dockerauth.json
 create mode 100644 deploy/helm/templates/deployment.yml
 create mode 100644 deploy/helm/templates/ingress.yml
 create mode 100644 deploy/helm/templates/pull_secret.yml
 create mode 100644 deploy/helm/templates/service.yml
 create mode 100644 deploy/helm/values.yaml
 delete mode 100644 deploy/kube/deployment.yml
 delete mode 100644 deploy/kube/ingress.yml
 delete mode 100644 deploy/kube/service.yml

diff --git a/.gitignore b/.gitignore
index a5aaed3..6f1621b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -144,3 +144,4 @@ dmypy.json
 
 # Cython debug symbols
 cython_debug/
+*.local.yml
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3697087..0350071 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -41,34 +41,39 @@ build:
   tags:
     - kube
   only:
-    - tags
+    - master
   script:
     - mkdir -p /kaniko/.docker
     - echo "$DOCKER_AUTH_CONFIG" > /kaniko/.docker/config.json
     - /kaniko/executor --context .
       --dockerfile deploy/Dockerfile
-      --destination "$IMAGE_NAME:$APP_VERSION"
+      --destination "docker.le-memese.com/bots/s3rius-bot:latest"
       --force
 
 deploy:
   stage: deploy
-  image: docker.le-memese.com/kubectl:1.22.0
+  image:
+    name: alpine/helm:3.7.1
+    entrypoint: ["/bin/sh", "-c"]
   tags:
     - kube
   only:
-    - tags
+    - master
   environment:
-    name: production
-    url: https://${BOT_HOST}/
+    name: s3rius-production
     kubernetes:
-      namespace: $NAMESPACE
+      namespace: "$NAMESPACE"
+    action: start
+    url: "https://{BOT_UI_HOST}"
   script:
-    - env | grep "^BOT_" > .deploy-env
-    - kubectl delete configmap "$CONFIG_MAP" || true
-    - kubectl create configmap "$CONFIG_MAP" --from-env-file=.deploy-env
-    - echo "$DOCKER_AUTH_CONFIG" > .dockerauth.json
-    - kubectl delete secret "$PULL_SECRET" || true
-    - kubectl create secret generic "$PULL_SECRET"
-      --from-file=.dockerconfigjson=.dockerauth.json
-      --type=kubernetes.io/dockerconfigjson
-    - find deploy/kube -name "*.yml" | xargs cat | envsubst | kubectl apply -f -
+    - helm
+      upgrade
+      "$HELM_APP_NAME"
+      ./deploy/helm
+      --install
+      --wait
+      --create-namespace
+      --atomic
+      --timeout 2m
+      --namespace "$NAMESPACE"
+      -f "$HELM_CONFIG"
diff --git a/deploy/helm/.helmignore b/deploy/helm/.helmignore
new file mode 100644
index 0000000..0e8a0eb
--- /dev/null
+++ b/deploy/helm/.helmignore
@@ -0,0 +1,23 @@
+# Patterns to ignore when building packages.
+# This supports shell glob matching, relative path matching, and
+# negation (prefixed with !). Only one pattern per line.
+.DS_Store
+# Common VCS dirs
+.git/
+.gitignore
+.bzr/
+.bzrignore
+.hg/
+.hgignore
+.svn/
+# Common backup files
+*.swp
+*.bak
+*.tmp
+*.orig
+*~
+# Various IDEs
+.project
+.idea/
+*.tmproj
+.vscode/
diff --git a/deploy/helm/Chart.yaml b/deploy/helm/Chart.yaml
new file mode 100644
index 0000000..20c9412
--- /dev/null
+++ b/deploy/helm/Chart.yaml
@@ -0,0 +1,24 @@
+apiVersion: v2
+name: s3rius_bot
+description: A Helm chart for Kubernetes
+
+# A chart can be either an 'application' or a 'library' chart.
+#
+# Application charts are a collection of templates that can be packaged into versioned archives
+# to be deployed.
+#
+# Library charts provide useful utilities or functions for the chart developer. They're included as
+# a dependency of application charts to inject those utilities and functions into the rendering
+# pipeline. Library charts do not define any templates and therefore cannot be deployed.
+type: application
+
+# This is the chart version. This version number should be incremented each time you make changes
+# to the chart and its templates, including the app version.
+# Versions are expected to follow Semantic Versioning (https://semver.org/)
+version: 0.1.0
+
+# This is the version number of the application being deployed. This version number should be
+# incremented each time you make changes to the application. Versions are not expected to
+# follow Semantic Versioning. They should reflect the version the application is using.
+# It is recommended to use it with quotes.
+appVersion: "1.16.0"
diff --git a/deploy/helm/files/dockerauth.json b/deploy/helm/files/dockerauth.json
new file mode 100644
index 0000000..ece9146
--- /dev/null
+++ b/deploy/helm/files/dockerauth.json
@@ -0,0 +1,8 @@
+{
+    "auths": {
+        {{ .Values.image.pullSecret.registry | quote}}: {
+            "username": {{.Values.image.pullSecret.username | quote }},
+            "password": {{.Values.image.pullSecret.password | quote }}
+        }
+    }
+}
diff --git a/deploy/helm/templates/deployment.yml b/deploy/helm/templates/deployment.yml
new file mode 100644
index 0000000..0347e85
--- /dev/null
+++ b/deploy/helm/templates/deployment.yml
@@ -0,0 +1,41 @@
+---
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: {{ .Values.bot.name }}
+  annotations:
+    {{- .Values.annotations | toYaml | nindent 4 }}
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: {{ .Values.bot.name }}
+  template:
+    metadata:
+      labels:
+        app: {{ .Values.bot.name }}
+      annotations:
+        deployed_at: {{ now | quote }}
+    spec:
+      {{- with .Values.image.pullSecret }}
+      imagePullSecrets:
+        - name: {{ .name | quote }}
+      {{- end }}
+      containers:
+        - name: bot
+          image: {{ .Values.image.name }}
+          imagePullPolicy: Always
+          {{- with .Values.resources }}
+          resources:
+            {{- . | toYaml | nindent 12 }}
+          {{- end }}
+          {{- with .Values.env }}
+          env:
+            {{- range $key, $val := . }}
+              - name: {{$key | quote }}
+                value: {{$val | quote }}
+            {{- end }}
+          {{- end }}
+          ports:
+            - containerPort: {{ .Values.env.BOT_FLASK_PORT | default 8080 }}
+              name: "botport"
diff --git a/deploy/helm/templates/ingress.yml b/deploy/helm/templates/ingress.yml
new file mode 100644
index 0000000..9138267
--- /dev/null
+++ b/deploy/helm/templates/ingress.yml
@@ -0,0 +1,7 @@
+---
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: {{ .Values.bot.name }}
+spec:
+  {{- .Values.ingress | toYaml | nindent 2 }}
\ No newline at end of file
diff --git a/deploy/helm/templates/pull_secret.yml b/deploy/helm/templates/pull_secret.yml
new file mode 100644
index 0000000..16a7e44
--- /dev/null
+++ b/deploy/helm/templates/pull_secret.yml
@@ -0,0 +1,9 @@
+{{- if .Values.image.pullSecret }}
+apiVersion: v1
+kind: Secret
+metadata:
+  name: {{ .Values.image.pullSecret.name }}
+data:
+  .dockerconfigjson: {{tpl (.Files.Get "files/dockerauth.json") . | b64enc | quote}}
+type: kubernetes.io/dockerconfigjson
+{{- end }}
diff --git a/deploy/helm/templates/service.yml b/deploy/helm/templates/service.yml
new file mode 100644
index 0000000..6d80120
--- /dev/null
+++ b/deploy/helm/templates/service.yml
@@ -0,0 +1,12 @@
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.bot.name }}
+spec:
+  selector:
+    app: {{ .Values.bot.name }}
+  ports:
+  - port: 80
+    name: bot
+    targetPort: "botport"
diff --git a/deploy/helm/values.yaml b/deploy/helm/values.yaml
new file mode 100644
index 0000000..ba5b1f9
--- /dev/null
+++ b/deploy/helm/values.yaml
@@ -0,0 +1,46 @@
+# Default values for s3rius_bot.
+# This is a YAML-formatted file.
+# Declare variables to be passed into your templates.
+
+bot:
+  name: s3rius
+
+image:
+  name: some-image
+  pullSecret: {}
+    # name
+    # secretName:
+    # registry
+    # username
+    # password
+
+env: {}
+# TELEGRAM_ACCOUNT_PHONE:
+# TELEGRAM_API_HASH:
+# TELEGRAM_APP_ID:
+
+annotations:
+    app.gitlab.com/env: $CI_ENVIRONMENT_SLUG
+    app.gitlab.com/app: $CI_PROJECT_PATH_SLUG
+
+
+imagePullSecrets: []
+nameOverride: ""
+fullnameOverride: ""
+
+ingress: {}
+  #  - secretName: chart-example-tls
+  #    hosts:
+  #      - chart-example.local
+
+resources: {}
+  # We usually recommend not to specify default resources and to leave this as a conscious
+  # choice for the user. This also increases chances charts run on environments with little
+  # resources, such as Minikube. If you do want to specify resources, uncomment the following
+  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
+  # limits:
+  #   cpu: 100m
+  #   memory: 128Mi
+  # requests:
+  #   cpu: 100m
+  #   memory: 128Mi
diff --git a/deploy/kube/deployment.yml b/deploy/kube/deployment.yml
deleted file mode 100644
index 18f943e..0000000
--- a/deploy/kube/deployment.yml
+++ /dev/null
@@ -1,35 +0,0 @@
----
-apiVersion: apps/v1
-kind: Deployment
-metadata:
-  name: s3riusbot
-  annotations:
-    app.gitlab.com/env: $CI_ENVIRONMENT_SLUG
-    app.gitlab.com/app: $CI_PROJECT_PATH_SLUG
-spec:
-  replicas: 1
-  selector:
-    matchLabels:
-      app: s3rius-bot
-  template:
-    metadata:
-      annotations:
-        app.gitlab.com/env: $CI_ENVIRONMENT_SLUG
-        app.gitlab.com/app: $CI_PROJECT_PATH_SLUG
-      labels:
-        app: s3rius-bot
-    spec:
-      imagePullSecrets:
-        - name: "$PULL_SECRET"
-      containers:
-        - name: s3rius-bot
-          image: docker.le-memese.com/bots/s3rius-bot:$APP_VERSION
-          resources:
-            limits:
-              cpu: "$K8S_CPU_LIMITS"
-              memory: "$K8S_MEM_LIMITS"
-          envFrom:
-            - configMapRef:
-                name: "$CONFIG_MAP"
-          ports:
-            - containerPort: $BOT_FLASK_PORT
diff --git a/deploy/kube/ingress.yml b/deploy/kube/ingress.yml
deleted file mode 100644
index d6bc67d..0000000
--- a/deploy/kube/ingress.yml
+++ /dev/null
@@ -1,19 +0,0 @@
----
-apiVersion: networking.k8s.io/v1
-kind: Ingress
-metadata:
-  name: s3rius-bot-ui
-spec:
-  tls:
-    - secretName: $TLS_SECRET
-  rules:
-    - host: $BOT_HOST
-      http:
-        paths:
-          - path: /
-            pathType: Prefix
-            backend:
-              service:
-                name: s3rius-bot
-                port:
-                  name: web
diff --git a/deploy/kube/service.yml b/deploy/kube/service.yml
deleted file mode 100644
index 50f71df..0000000
--- a/deploy/kube/service.yml
+++ /dev/null
@@ -1,12 +0,0 @@
----
-apiVersion: v1
-kind: Service
-metadata:
-  name: s3rius-bot
-spec:
-  selector:
-    app: s3rius-bot
-  ports:
-  - port: 80
-    name: web
-    targetPort: $BOT_FLASK_PORT
-- 
GitLab