From 082d0b9a59ba99c08f654c34397b93a253262c4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=B6ran=20P=C3=B6hner?=
 <10630407+groundhog2k@users.noreply.github.com>
Date: Wed, 28 Sep 2022 20:23:27 +0200
Subject: [PATCH] Support inline scripts in postgres chart (#1163)

* Support inline scripts in postgres chart
Fixes #1162

* Support inline scripts in postgres chart
Fixes #1162

* Reverted back to customScripts
---
 charts/postgres/Chart.yaml                   |  2 +-
 charts/postgres/README.md                    |  3 ++-
 charts/postgres/RELEASENOTES.md              |  1 +
 charts/postgres/templates/customscripts.yaml | 13 +++++++++++++
 charts/postgres/templates/scripts.yaml       |  4 ++++
 charts/postgres/templates/statefulset.yaml   | 11 +++++++++++
 charts/postgres/values.yaml                  |  7 +++++++
 7 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 charts/postgres/templates/customscripts.yaml

diff --git a/charts/postgres/Chart.yaml b/charts/postgres/Chart.yaml
index 1853f490..4cfe061e 100644
--- a/charts/postgres/Chart.yaml
+++ b/charts/postgres/Chart.yaml
@@ -7,6 +7,6 @@ type: application
 maintainers:
   - name: groundhog2k
 
-version: 0.3.11
+version: 0.4.0
 
 appVersion: "14.5"
diff --git a/charts/postgres/README.md b/charts/postgres/README.md
index 674f668a..c8f4e2a7 100644
--- a/charts/postgres/README.md
+++ b/charts/postgres/README.md
@@ -1,6 +1,6 @@
 # PostgreSQL
 
-![Version: 0.3.11](https://img.shields.io/badge/Version-0.3.11-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 14.5](https://img.shields.io/badge/AppVersion-14.5-informational?style=flat-square)
+![Version: 0.4.0](https://img.shields.io/badge/Version-0.4.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 14.5](https://img.shields.io/badge/AppVersion-14.5-informational?style=flat-square)
 
 ## Changelog
 
@@ -121,6 +121,7 @@ helm uninstall my-release
 | customConfig | string | `nil` | Optional custom configuration block that will be mounted as file in `/etc/postgresql/postgresql.conf` |
 | extraEnvSecrets | list | `[]` | A list of existing secrets that will be mounted into the container as environment variables |
 | extraSecretConfigs | string | `nil` | An existing secret with files that will be added to the postgres configuration in addition to `/etc/postgresql/postgresql.conf` |
+| customScripts | object | `nil` | Optional custom scripts that can be defined inline and will be mounted as files in `/docker-entrypoint-initdb.d` |
 | extraScripts | string | `nil` | An existing configMap with files that will be mounted into the container as script files (`*.sql`, `*.sh`) in `/docker-entrypoint-initdb.d` |
 | extraSecrets | list | `[]` | A list of additional existing secrets that will be mounted into the container |
 | extraSecrets[].name | string | `nil` | Name of the existing K8s secret |
diff --git a/charts/postgres/RELEASENOTES.md b/charts/postgres/RELEASENOTES.md
index ab182fc2..4c71a430 100644
--- a/charts/postgres/RELEASENOTES.md
+++ b/charts/postgres/RELEASENOTES.md
@@ -16,4 +16,5 @@
 | 0.3.9 | 14.3 | Upgraded to Postgres 14.3 |
 | 0.3.10 | 14.4 | Upgraded to Postgres 14.4 |
 | 0.3.11 | 14.5 | Upgraded to Postgres 14.5 |
+| 0.4.0 | 14.5 | Implemented support for custom inline init scripts |
 | | | |
diff --git a/charts/postgres/templates/customscripts.yaml b/charts/postgres/templates/customscripts.yaml
new file mode 100644
index 00000000..ef1cfa1e
--- /dev/null
+++ b/charts/postgres/templates/customscripts.yaml
@@ -0,0 +1,13 @@
+{{- if .Values.customScripts }}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "postgres.fullname" . }}-customscripts
+  labels:
+    {{- include "postgres.labels" . | nindent 4 }}
+data:
+  {{- range $name, $value := .Values.customScripts }}
+  {{- $name | nindent 2 }}: |
+    {{- $value | nindent 4 }}
+  {{- end }}
+{{- end }}
diff --git a/charts/postgres/templates/scripts.yaml b/charts/postgres/templates/scripts.yaml
index f266349f..8b95b790 100644
--- a/charts/postgres/templates/scripts.yaml
+++ b/charts/postgres/templates/scripts.yaml
@@ -27,6 +27,10 @@ data:
       echo "Copy extra scripts"
       cp /extrascripts/* /scripts
     fi
+    if [ -d /customscripts ]; then
+      echo "Copy custom scripts"
+      cp /customscripts/* /scripts
+    fi
     if [ -d /customconfig ]; then
       echo "Create postgres config"
       cat /customconfig/* >>/configs/postgresql.conf
diff --git a/charts/postgres/templates/statefulset.yaml b/charts/postgres/templates/statefulset.yaml
index 690f68e0..44762f62 100644
--- a/charts/postgres/templates/statefulset.yaml
+++ b/charts/postgres/templates/statefulset.yaml
@@ -21,6 +21,7 @@ spec:
       annotations:
         checksum/customconfig: {{ include (print $.Template.BasePath "/customconfig.yaml") . | sha256sum }}
         checksum/secureconfig: {{ include (print $.Template.BasePath "/secureconfig.yaml") . | sha256sum }}
+        checksum/customscripts: {{ include (print $.Template.BasePath "/customscripts.yaml") . | sha256sum }}
         checksum/scripts: {{ include (print $.Template.BasePath "/scripts.yaml") . | sha256sum }}
       {{- with .Values.podAnnotations }}
         {{- toYaml . | nindent 8 }}
@@ -50,6 +51,10 @@ spec:
             - mountPath: /extrascripts
               name: extrascripts-volume
             {{- end }}
+            {{- if .Values.customScripts }}
+            - mountPath: /customscripts
+              name: customscripts-volume
+            {{- end }}
             {{- if .Values.extraSecretConfigs }}
             - mountPath: /extraconfigs
               name: extraconfigs-volume
@@ -222,6 +227,12 @@ spec:
             name: {{ .Values.extraScripts }}
             defaultMode: 0555
         {{- end }}
+        {{- if .Values.customScripts }}
+        - name: customscripts-volume
+          configMap:
+            name: {{ include "postgres.fullname" . }}-customscripts
+            defaultMode: 0555
+        {{- end }}
         {{- if .Values.extraSecretConfigs }}
         - name: extraconfigs-volume
           secret:
diff --git a/charts/postgres/values.yaml b/charts/postgres/values.yaml
index 26fd9887..b80c8f62 100644
--- a/charts/postgres/values.yaml
+++ b/charts/postgres/values.yaml
@@ -154,6 +154,13 @@ userDatabase: {}
 ## For more flexible options see extraSecretConfigs: section
 customConfig: |
 
+## Optional custom scripts that can be defined inline and will be mounted as files in /docker-entrypoint-initdb.d
+customScripts: {}
+#  01-a-script.sh: |
+#    echo "hello"
+#  02-another-script.sh: |
+#    echo "hello 2"
+
 ## A list of existing secrets that will be mounted into the container as environment variables
 ## As an alternative these secrets can set the database superuser password or other options when settings.superuserPassword was not specified
 ## For example: Setting POSTGRES_DB, USERDB_USER, USERDB_PASSWORD will allow creating a user database and grant access for the given user
-- 
GitLab