From 66f596f15c5d179865fee97c7faf4f935af0c57e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gyulai=20D=C3=A1niel?= <gydani2@gmail.com>
Date: Sun, 22 Nov 2020 11:30:22 -0500
Subject: [PATCH] Applied templating in helm chart

---
 helm/covidok/templates/_helpers.tpl     | 16 ++++++++++++++++
 helm/covidok/templates/appsettings.yaml | 17 +++++++++++++++++
 helm/covidok/templates/deployment.yaml  | 10 +++++++++-
 helm/covidok/templates/minio.yaml       | 19 ++++++++++++++-----
 helm/covidok/templates/mysql.yaml       | 24 ++++++++++++++++--------
 helm/covidok/templates/redis.yaml       | 23 ++++++++++++++++-------
 helm/covidok/values.yaml                | 24 ++++++++++++++++++------
 7 files changed, 106 insertions(+), 27 deletions(-)
 create mode 100644 helm/covidok/templates/appsettings.yaml

diff --git a/helm/covidok/templates/_helpers.tpl b/helm/covidok/templates/_helpers.tpl
index 06817ec..028efba 100644
--- a/helm/covidok/templates/_helpers.tpl
+++ b/helm/covidok/templates/_helpers.tpl
@@ -5,6 +5,22 @@ Expand the name of the chart.
 {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
 {{- end }}
 
+{{- define "covidok.minio" -}}
+{{ .Release.Name }}-minio
+{{- end }}
+
+{{- define "covidok.mysql" -}}
+{{ .Release.Name -}}-mysql
+{{- end }}
+
+{{- define "covidok.redis" -}}
+{{ .Release.Name -}}-redis
+{{- end }}
+
+{{- define "covidok.appsettings" -}}
+{{- .Release.Name -}}-appsettings
+{{- end }}
+
 {{/*
 Create a default fully qualified app name.
 We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
diff --git a/helm/covidok/templates/appsettings.yaml b/helm/covidok/templates/appsettings.yaml
new file mode 100644
index 0000000..c74d48d
--- /dev/null
+++ b/helm/covidok/templates/appsettings.yaml
@@ -0,0 +1,17 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "covidok.appsettings" . }}
+data: 
+  appsettings.k8s.json: |
+    {
+      "ConnectionStrings": {
+        "MySQLDatabase": "server={{- include "covidok.mysql" . -}};database={{ .Values.config.mysql.database }};user={{ .Values.config.mysql.user }};password={{ .Values.config.mysql.password }}",
+        "RedisHost": "{{ include "covidok.redis" . }}:{{ .Values.config.redis.port }}"
+      },
+      "MinioSettings": {
+        "HostName": "{{ include "covidok.minio" . }}",
+        "AccessKey": "{{ .Values.config.minio.accesskey }}",
+        "SecretKey":  "{{ .Values.config.minio.secretkey }}"
+      }
+    }
diff --git a/helm/covidok/templates/deployment.yaml b/helm/covidok/templates/deployment.yaml
index 998b32b..dd4789a 100644
--- a/helm/covidok/templates/deployment.yaml
+++ b/helm/covidok/templates/deployment.yaml
@@ -26,7 +26,7 @@ spec:
         - name: mysql
           image: mysql:8.0
           command: ["/bin/bash"]
-          args: ["-c", "while ! mysqladmin ping -h mysql --silent; do sleep 1; done"]
+          args: ["-c", "while ! mysqladmin ping -h {{ include "covidok.mysql" . }} --silent; do sleep 1; done"]
       containers:
         - name: {{ .Chart.Name }}
           image: "{{ .Values.images.covidok.name }}:{{ .Values.images.covidok.tag }}"
@@ -35,6 +35,14 @@ spec:
             - name: http
               containerPort: 80
               protocol: TCP
+          volumeMounts:
+          - name: appsettings
+            mountPath: /app/settings/appsettings.k8s.json
+            subPath: appsettings.k8s.json
+      volumes:
+        - name: appsettings
+          configMap:
+            name: {{- include "covidok.appsettings" . }}
       {{- with .Values.nodeSelector }}
       nodeSelector:
         {{- toYaml . | nindent 8 }}
diff --git a/helm/covidok/templates/minio.yaml b/helm/covidok/templates/minio.yaml
index dc3a5a2..8312f72 100644
--- a/helm/covidok/templates/minio.yaml
+++ b/helm/covidok/templates/minio.yaml
@@ -1,36 +1,45 @@
 apiVersion: v1
 kind: Service
 metadata:
-  name: minio
+  name: {{ include "covidok.minio" . }}
+  labels:
+    {{- include "covidok.labels" . | nindent 4 }}
 spec:
   ports:
   - port: 9000
   selector:
+{{- include "covidok.selectorLabels" . | nindent 4 }}
     app: minio-covidok
 ---
 apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
 kind: Deployment
 metadata:
-  name: minio-covidok
+  name: {{ include "covidok.minio" . }}
 spec:
   selector:
     matchLabels:
+{{- include "covidok.selectorLabels" . | nindent 6 }}
       app: minio-covidok
   strategy:
     type: Recreate
   template:
     metadata:
+    {{- with .Values.podAnnotations }}
+      annotations:
+        {{- toYaml . | nindent 8 }}
+    {{- end }}
       labels:
+{{- include "covidok.selectorLabels" . | nindent 8 }}
         app: minio-covidok
     spec:
       containers:
-      - image: minio/minio
+      - image: "{{ .Values.images.minio.name }}:{{ .Values.images.minio.tag }}"
         name: minio
         env:
         - name: MINIO_ACCESS_KEY
-          value: "secretaccesskey"
+          value: "{{ .Values.config.minio.accesskey }}"
         - name: MINIO_SECRET_KEY
-          value: "secretsecretkey"
+          value: "{{ .Values.config.minio.secretkey }}"
         ports:
         - containerPort: 9000
           name: minio
diff --git a/helm/covidok/templates/mysql.yaml b/helm/covidok/templates/mysql.yaml
index 062500a..e8db323 100644
--- a/helm/covidok/templates/mysql.yaml
+++ b/helm/covidok/templates/mysql.yaml
@@ -1,42 +1,50 @@
-# TODO: Add helm templating
 apiVersion: v1
 kind: Service
 metadata:
-  name: mysql
+  name: {{ include "covidok.mysql" . }}
+  labels:
+    {{- include "covidok.labels" . | nindent 4 }}
 spec:
   ports:
   - port: 3306
   selector:
+{{- include "covidok.selectorLabels" . | nindent 4 }}
     app: mysql
 ---
-apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+apiVersion: apps/v1
 kind: Deployment
 metadata:
-  name: mysql
+  name: {{ include "covidok.mysql" . }}
 spec:
   selector:
     matchLabels:
+{{- include "covidok.selectorLabels" . | nindent 6 }}
       app: mysql
   strategy:
     type: Recreate
   template:
     metadata:
+    {{- with .Values.podAnnotations }}
+      annotations:
+        {{- toYaml . | nindent 8 }}
+    {{- end }}
       labels:
+{{- include "covidok.selectorLabels" . | nindent 8 }}
         app: mysql
     spec:
       containers:
-      - image: mysql:8.0
+      - image: "{{ .Values.images.mysql.name }}:{{ .Values.images.mysql.tag }}"
         name: mysql
         env:
           # Use secret in real usage
         - name: MYSQL_ROOT_PASSWORD
           value: dev-pass1
         - name: MYSQL_DATABASE
-          value: "covidok"
+          value: "{{ .Values.config.mysql.database }}"
         - name: MYSQL_USER
-          value: "covidok"
+          value: "{{ .Values.config.mysql.user }}"
         - name: MYSQL_PASSWORD
-          value: "covidok"
+          value: "{{ .Values.config.mysql.password }}"
         ports:
         - containerPort: 3306
           name: mysql
diff --git a/helm/covidok/templates/redis.yaml b/helm/covidok/templates/redis.yaml
index 06fe626..0ada8b6 100644
--- a/helm/covidok/templates/redis.yaml
+++ b/helm/covidok/templates/redis.yaml
@@ -1,30 +1,39 @@
 apiVersion: v1
 kind: Service
 metadata:
-  name: redis
+  name: {{ include "covidok.redis" . }}
+  labels:
+    {{- include "covidok.labels" . | nindent 4 }}
 spec:
   ports:
   - port: 6379
   selector:
-    app: redis-dev
+{{- include "covidok.selectorLabels" . | nindent 4 }}
+    app: redis
 ---
-apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+apiVersion: apps/v1
 kind: Deployment
 metadata:
-  name: redis-dev
+  name: {{ include "covidok.redis" . }}
 spec:
   selector:
     matchLabels:
-      app: redis-dev
+{{- include "covidok.selectorLabels" . | nindent 6 }}
+      app: redis
   strategy:
     type: Recreate
   template:
     metadata:
+    {{- with .Values.podAnnotations }}
+      annotations:
+        {{- toYaml . | nindent 8 }}
+    {{- end }}
       labels:
-        app: redis-dev
+        {{- include "covidok.selectorLabels" . | nindent 8 }}
+        app: redis
     spec:
       containers:
-      - image: redis:6
+      - image: "{{ .Values.images.redis.name }}:{{ .Values.images.redis.tag }}"
         name: redis
         ports:
         - containerPort: 6379
diff --git a/helm/covidok/values.yaml b/helm/covidok/values.yaml
index e8a7e2a..f1a7126 100644
--- a/helm/covidok/values.yaml
+++ b/helm/covidok/values.yaml
@@ -9,9 +9,27 @@ images:
   mysql:
     name: mysql
     tag: 8.0
+  minio:
+    name: minio/minio
+    tag: latest
+  redis:
+    name: redis
+    tag: 6
   pullPolicy: Always
   # Overrides the image tag whose default is the chart appVersion.
 
+config:
+  minio:
+    accesskey: "accesskey"
+    secretkey: "secretkey"
+  mysql:
+    database: "covidok"
+    user: "covdiok"
+    password: "covidok"
+  redis:
+    port: "6379"
+
+
 imagePullSecrets: []
 nameOverride: ""
 fullnameOverride: ""
@@ -21,9 +39,3 @@ podAnnotations: {}
 service:
   type: ClusterIP
   port: 80
-
-nodeSelector: {}
-
-tolerations: []
-
-affinity: {}