9bf694f2 by Adam Heath

Add feature to support multiple databases and users at startup.

1 parent 0699ffe5
......@@ -4,15 +4,12 @@ kind: ConfigMap
metadata:
name: postgresql-config
data:
POSTGRESQL_DATABASE: ""
POSTGRESQL_USER: ""
---
apiVersion: v1
kind: Secret
metadata:
name: postgresql-secret
stringData:
POSTGRESQL_PASSWORD: "CHANGEME"
POSTGRESQL_POSTGRES_PASSWORD: "CHANGEME"
---
apiVersion: v1
......
......@@ -9,9 +9,13 @@ environments:
jsonPatches: []
strategicMergePatches: []
postgresPassword: CHANGEME
password: CHANGEME
database: CHANGEME
user: CHANGEME
users:
- name: CHANGEME
password: CHANGEME
databases:
- name: CHANGEME
users:
- CHANGEME
images:
debian: "debian:bullseye-20211220"
postgresql: "bitnami/postgresql:14.1.0-debian-10-r66"
......@@ -76,6 +80,9 @@ releases:
- name: postgresql-scripts
configMap:
name: {{ .Values.namePrefix }}postgresql-scripts
- name: postgresql-preinit-scripts
configMap:
name: {{ .Values.namePrefix }}postgresql-preinit-scripts
- name: postgresql-secret
secret:
secretName: {{ .Values.namePrefix }}postgresql-secret
......@@ -93,16 +100,25 @@ releases:
name: {{ .Values.namePrefix }}postgresql-config
namespace: {{ .Values.namespace }}
data:
POSTGRESQL_DATABASE: {{ .Values.postgresqlServer.database }}
POSTGRESQL_USER: {{ .Values.postgresqlServer.user }}
{{- range $user_index, $user := .Values.postgresqlServer.users }}
POSTGRESQL_USER_{{ $user_index }}_NAME: {{ $user.name | quote }}
{{- end }}
{{- range $db_index, $database := .Values.postgresqlServer.databases }}
POSTGRESQL_DATABASE_{{ $db_index }}_NAME: {{ $database.name | quote }}
{{- range $user_index, $user := $database.users }}
POSTGRESQL_DATABASE_{{ $db_index }}_USER_{{ $user_index }}: {{ $user | quote }}
{{- end }}
{{- end }}
- apiVersion: v1
kind: Secret
metadata:
name: {{ .Values.namePrefix }}postgresql-secret
namespace: {{ .Values.namespace }}
stringData:
POSTGRESQL_PASSWORD: {{ .Values.postgresqlServer.password }}
POSTGRESQL_POSTGRES_PASSWORD: {{ .Values.postgresqlServer.postgresPassword }}
{{- range $user_index, $user := .Values.postgresqlServer.users }}
POSTGRESQL_USER_{{ $user_index }}_PASSWORD: {{ $user.password | quote }}
{{- end }}
{{- if not (empty (.Values.postgresqlServer.strategicMergePatches)) }}
{{- .Values.postgresqlServer.strategicMergePatches | toYaml | indent 6 }}
{{- end }}
......
......@@ -14,4 +14,8 @@ configMapGenerator:
- ./scripts/restore-from-empty
- ./scripts/bitnami-postgresql-entrypoint-container.sh
- ./scripts/bitnami-postgresql-entrypoint-initContainer.sh
- ./scripts/bitnami-create-extra-users.sh
- name: postgresql-preinit-scripts
files:
- ./scripts/bitnami-create-extra-users.sh
......
......@@ -45,6 +45,10 @@ spec:
configMap:
name: postgresql-scripts
defaultMode: 0755
- name: postgresql-preinit-scripts
configMap:
name: postgresql-scripts
defaultMode: 0755
- name: postgresql-config
configMap:
name: postgresql-config
......@@ -83,17 +87,13 @@ spec:
mountPath: /var/postgresql-secret
- name: postgresql-entrypoint-initdb
mountPath: /docker-entrypoint-initdb.d
- name: postgresql-preinit-scripts
mountPath: /docker-entrypoint-preinitdb.d
- name: etc
mountPath: /runtime-etc
env:
- name: POSTGRESQL_DATABASE_FILE
value: /var/postgresql-config/POSTGRESQL_DATABASE
- name: POSTGRESQL_USER_FILE
value: /var/postgresql-config/POSTGRESQL_USER
- name: POSTGRESQL_PASSWORD_FILE
value: /var/postgresql-secret/POSTGRESQL_PASSWORD
- name: POSTGRESQL_POSTGRES_PASSWORD_FILE
value: /var/postgresql-secret/POSTGRESQL_POSTGRES_PASSWORD
- name: POSTGRESQL_PASSWORD
value: "foobar"
containers:
- name: postgresql
......
#!/bin/bash
. /opt/bitnami/scripts/libpostgresql.sh
set -x
ls -al /var/postgresql-config /var/postgresql-secret
declare -i user_index=0 db_index=0
while :; do
user_name="$(< /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME)"
user_password="$(< /var/postgresql-secret/POSTGRESQL_USER_${user_index}_PASSWORD)"
if [[ -z $user_name ]]; then
break
fi
postgresql_ensure_user_exists "$user_name" --password "$user_password"
user_index=$((user_index + 1))
if [[ ! -e /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME ]]; then
break
fi
done
first_user=""
while :; do
database_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME)"
postgresql_ensure_database_exists "$database_name"
user_index=0
while :; do
user_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index})"
if [[ -z $user_name ]]; then
break
elif [[ -z $first_user ]]; then
first_user="$user_name"
else
postgresql_ensure_user_has_database_privileges "$user_name" "$database_name"
fi
user_index=$((user_index + 1))
if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index} ]]; then
break
fi
done
if [[ $first_user ]]; then
postgresql_ensure_user_has_database_privileges "$first_user" "$database_name"
fi
db_index=$((db_index + 1))
if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME ]]; then
break
fi
done