From ad7045339693a0c7352a3a9c27458c09f69fd2ef Mon Sep 17 00:00:00 2001
From: groundhog2k <10630407+groundhog2k@users.noreply.github.com>
Date: Sun, 24 Jan 2021 12:15:52 +0100
Subject: [PATCH] Fixed issues #192, #169 for Postgres (#195)

* Fixed issues #192, #191, #169 forRabbitMQ

* Fixed issues #192, #191, #169 for Redis

* Fixed issues #192, #169 for Postgres
---
 charts/postgres/Chart.yaml                    |  2 +-
 charts/postgres/README.md                     |  4 ++-
 charts/postgres/templates/customconfig.yaml   | 11 ++++++
 .../{initdbconfig.yaml => inituserdb.yaml}    | 12 +++----
 charts/postgres/templates/secureconfig.yaml   | 27 +++++++++++----
 charts/postgres/templates/statefulset.yaml    | 34 +++++++++++++++----
 charts/postgres/values.yaml                   |  8 ++++-
 7 files changed, 73 insertions(+), 25 deletions(-)
 create mode 100644 charts/postgres/templates/customconfig.yaml
 rename charts/postgres/templates/{initdbconfig.yaml => inituserdb.yaml} (71%)

diff --git a/charts/postgres/Chart.yaml b/charts/postgres/Chart.yaml
index 6647b6a2..3dc08313 100644
--- a/charts/postgres/Chart.yaml
+++ b/charts/postgres/Chart.yaml
@@ -7,6 +7,6 @@ type: application
 maintainers:
   - name: groundhog2k
 
-version: 0.2.5
+version: 0.2.6
 
 appVersion: "13.1"
diff --git a/charts/postgres/README.md b/charts/postgres/README.md
index d5e99e83..56bf286f 100644
--- a/charts/postgres/README.md
+++ b/charts/postgres/README.md
@@ -1,6 +1,6 @@
 # PostgreSQL
 
-![Version: 0.2.5](https://img.shields.io/badge/Version-0.2.5-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 13.1](https://img.shields.io/badge/AppVersion-13.1-informational?style=flat-square)
+![Version: 0.2.6](https://img.shields.io/badge/Version-0.2.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 13.1](https://img.shields.io/badge/AppVersion-13.1-informational?style=flat-square)
 
 A Helm chart for PostgreSQL on Kubernetes
 
@@ -57,6 +57,8 @@ $ helm uninstall my-release
 | imagePullSecrets | list | `[]` | Image pull secrets |
 | livenessProbe | object | `see values.yaml` | Liveness probe configuration |
 | readinessProbe | object | `see values.yaml` | Readiness probe configuration |
+| customLivenessProbe | object | `{}` | Custom liveness probe (overwrites default liveness probe configuration) |
+| customReadinessProbe | object | `{}` | Custom readiness probe (overwrites default readiness probe configuration) |
 | resources | object | `{}` | Resource limits and requests |
 | nodeSelector | object | `{}` | Deployment node selector |
 | podAnnotations | object | `{}` | Additional pod annotations |
diff --git a/charts/postgres/templates/customconfig.yaml b/charts/postgres/templates/customconfig.yaml
new file mode 100644
index 00000000..2b12c0ab
--- /dev/null
+++ b/charts/postgres/templates/customconfig.yaml
@@ -0,0 +1,11 @@
+{{- if .Values.customConfig }}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "postgres.fullname" . }}-customconfig
+  labels:
+    {{- include "postgres.labels" . | nindent 4 }}
+data:
+  custom.cnf: |-
+    {{- .Values.customConfig | nindent 4 }}
+{{- end }}
diff --git a/charts/postgres/templates/initdbconfig.yaml b/charts/postgres/templates/inituserdb.yaml
similarity index 71%
rename from charts/postgres/templates/initdbconfig.yaml
rename to charts/postgres/templates/inituserdb.yaml
index cd8ffc77..eabd0c64 100644
--- a/charts/postgres/templates/initdbconfig.yaml
+++ b/charts/postgres/templates/inituserdb.yaml
@@ -1,15 +1,11 @@
-{{- if or (.Values.customConfig) (.Values.userDatabase) }}
-kind: ConfigMap
+{{- if .Values.userDatabase }}
 apiVersion: v1
+kind: ConfigMap
 metadata:
-  name: {{ include "postgres.fullname" . }}
+  name: {{ include "postgres.fullname" . }}-inituserdb
   labels:
     {{- include "postgres.labels" . | nindent 4 }}
 data:
-{{- if .Values.customConfig }}
-  custom.cnf: |-
-    {{- .Values.database.customConfig | nindent 4 }}
-{{- end }}
 {{- with .Values.userDatabase }}
   init-user-db.sh: |-
     #!/bin/bash
@@ -20,4 +16,4 @@ data:
       GRANT ALL PRIVILEGES ON DATABASE :USERDBNAME TO :USERDBUSER;
     EOSQL
 {{- end }}
-{{- end }}
\ No newline at end of file
+{{- end }}
diff --git a/charts/postgres/templates/secureconfig.yaml b/charts/postgres/templates/secureconfig.yaml
index 8ed3b8dc..895b90e0 100644
--- a/charts/postgres/templates/secureconfig.yaml
+++ b/charts/postgres/templates/secureconfig.yaml
@@ -1,17 +1,30 @@
+{{- $existingSuperuserPassword := "" }}
+{{- $existingUserDbName := "" }}
+{{- $existingUserDbUser := "" }}
+{{- $existingUserDbPassword := "" }}
+{{- $secret := (lookup "v1" "Secret" .Release.Namespace (include "postgres.fullname" .) ) }}
+{{- if $secret }}
+{{- $existingSuperuserPassword = index $secret.data "POSTGRES_PASSWORD" }}
+{{- $existingUserDbName = index $secret.data "USERDB_NAME" }}
+{{- $existingUserDbUser = index $secret.data "USERDB_USER" }}
+{{- $existingUserDbPassword = index $secret.data "USERDB_PASSWORD" }}
+{{- end -}}
 apiVersion: v1
 kind: Secret
 metadata:
   name: {{ include "postgres.fullname" . }}
   labels:
     {{- include "postgres.labels" . | nindent 4 }}
-stringData:
+type: Opaque
+data:
 {{- with .Values.settings }}
-  POSTGRES_PASSWORD: {{ .superuserPassword | default (randAlphaNum 10) }}
-  POSTGRES_USER: "postgres"
-  POSTGRES_HOST_AUTH_METHOD: {{ .authMethod }}
+  POSTGRES_PASSWORD: {{ coalesce (.superuserPassword | default "" | b64enc) $existingSuperuserPassword (randAlphaNum 10 | b64enc) }}
+  POSTGRES_USER: {{ "postgres" | b64enc }}
 {{- end }}
 {{- with .Values.userDatabase }}
-  USERDB_PASSWORD: {{ .password | default (randAlphaNum 10) }}
-  USERDB_USER: {{ .user }}
-  USERDB_NAME: {{ .name }}
+  {{- $userDbName := coalesce (.name | default "" | b64enc) $existingUserDbName }}
+  {{- $userDbUser := coalesce (.user | default "" | b64enc) $existingUserDbUser }}
+  USERDB_NAME: {{ required "Values: userDatabase.name is mandatory if userDatabase is specified." $userDbName }}
+  USERDB_USER: {{ required "Values: userDatabase.user is mandatory if userDatabase is specified." $userDbUser }}
+  USERDB_PASSWORD: {{ coalesce (.password | default "" | b64enc) $existingUserDbPassword (randAlphaNum 10 | b64enc) }}
 {{- end }}
diff --git a/charts/postgres/templates/statefulset.yaml b/charts/postgres/templates/statefulset.yaml
index 3bd25ab7..3a005442 100644
--- a/charts/postgres/templates/statefulset.yaml
+++ b/charts/postgres/templates/statefulset.yaml
@@ -12,10 +12,11 @@ spec:
       {{- include "postgres.selectorLabels" . | nindent 6 }}
   template:
     metadata:
-    {{- with .Values.podAnnotations }}
       annotations:
+        checksum/customconfig: {{ include (print $.Template.BasePath "/customconfig.yaml") . | sha256sum }}
+      {{- with .Values.podAnnotations }}
         {{- toYaml . | nindent 8 }}
-    {{- end }}
+      {{- end }}
       labels:
         {{- include "postgres.selectorLabels" . | nindent 8 }}
     spec:
@@ -43,6 +44,10 @@ spec:
           env:
             - name: PGDATA
               value: /var/lib/postgresql/data/pg
+            {{- if .Values.settings.authMethod }}
+            - name: POSTGRES_HOST_AUTH_METHOD
+              value: {{ .Values.settings.authMethod }}
+            {{- end }}
             {{- if .Values.settings.initDbArgs }}
             - name: POSTGRES_INITDB_ARGS
               value: {{ .Values.settings.initDbArgs }}
@@ -53,6 +58,10 @@ spec:
           envFrom:
             - secretRef:
                 name: {{ include "postgres.fullname" . }}
+          {{- if .Values.customLivenessProbe }}
+          livenessProbe:
+            {{- toYaml .Values.customLivenessProbe | nindent 12 }}
+          {{- else }}
           {{- if .Values.livenessProbe.enabled }}
           livenessProbe:
             exec:
@@ -68,6 +77,11 @@ spec:
             periodSeconds: {{ .periodSeconds }}                
           {{- end }}
           {{- end }}
+          {{- end }}
+          {{- if .Values.customReadinessProbe }}
+          readinessProbe:
+            {{- toYaml .Values.customReadinessProbe | nindent 12 }}
+          {{- else }}
           {{- if .Values.readinessProbe.enabled }}
           readinessProbe:
             exec:
@@ -83,6 +97,7 @@ spec:
             periodSeconds: {{ .periodSeconds }}                
           {{- end }}
           {{- end }}
+          {{- end }}
           {{- with .Values.resources }}
           resources:
             {{- toYaml . | nindent 12 }}
@@ -103,12 +118,12 @@ spec:
             {{- if .Values.customConfig }}
             - mountPath: /etc/postgresql/postgresql.conf
               subPath: postgresql.conf
-              name: initdbconfig
+              name: customconfig
             {{- end }}
             {{- if .Values.userDatabase }}
             - mountPath: /docker-entrypoint-initdb.d/init-user-db.sh
               subPath: init-user-db.sh
-              name: initdbconfig
+              name: inituserdb
             {{- end }}
       {{- with .Values.nodeSelector }}
       nodeSelector:
@@ -127,10 +142,15 @@ spec:
           emptyDir: {}
         - name: tmp
           emptyDir: {}
-        {{- if or (.Values.customConfig) (.Values.userDatabase) }}
-        - name: initdbconfig
+        {{- if .Values.customConfig }}
+        - name: customconfig
+          configMap:
+            name: {{ include "postgres.fullname" . }}-customconfig
+        {{- end }}
+        {{- if .Values.userDatabase }}
+        - name: inituserdb
           configMap:
-            name: {{ include "postgres.fullname" . }}
+            name: {{ include "postgres.fullname" . }}-inituserdb
             defaultMode: 0555
         {{- end }}
 {{- with .Values.storage }}
diff --git a/charts/postgres/values.yaml b/charts/postgres/values.yaml
index 654bba75..35e2b931 100644
--- a/charts/postgres/values.yaml
+++ b/charts/postgres/values.yaml
@@ -64,6 +64,9 @@ tolerations: []
 
 affinity: {}
 
+## Custom liveness probe (overwrites default liveness probe)
+customLivenessProbe: {}
+
 ## Default health check
 livenessProbe:
   enabled: true
@@ -73,6 +76,9 @@ livenessProbe:
   successThreshold: 1
   periodSeconds: 10
 
+## Custom readiness probe (overwrites default readiness probe)
+customReadinessProbe: {}
+
 ## Default readiness probe
 readinessProbe:
   enabled: true
@@ -94,7 +100,7 @@ settings:
   superuserPassword:
 
   ## Postgres database authentication method (default: md5)
-  authMethod: md5
+  authMethod: "md5"
 
   ## Optional init database arguments
   initDbArgs:
-- 
GitLab