Add feature to support multiple databases and users at startup.
Showing
5 changed files
with
82 additions
and
17 deletions
... | @@ -4,15 +4,12 @@ kind: ConfigMap | ... | @@ -4,15 +4,12 @@ kind: ConfigMap |
4 | metadata: | 4 | metadata: |
5 | name: postgresql-config | 5 | name: postgresql-config |
6 | data: | 6 | data: |
7 | POSTGRESQL_DATABASE: "" | ||
8 | POSTGRESQL_USER: "" | ||
9 | --- | 7 | --- |
10 | apiVersion: v1 | 8 | apiVersion: v1 |
11 | kind: Secret | 9 | kind: Secret |
12 | metadata: | 10 | metadata: |
13 | name: postgresql-secret | 11 | name: postgresql-secret |
14 | stringData: | 12 | stringData: |
15 | POSTGRESQL_PASSWORD: "CHANGEME" | ||
16 | POSTGRESQL_POSTGRES_PASSWORD: "CHANGEME" | 13 | POSTGRESQL_POSTGRES_PASSWORD: "CHANGEME" |
17 | --- | 14 | --- |
18 | apiVersion: v1 | 15 | apiVersion: v1 | ... | ... |
... | @@ -9,9 +9,13 @@ environments: | ... | @@ -9,9 +9,13 @@ environments: |
9 | jsonPatches: [] | 9 | jsonPatches: [] |
10 | strategicMergePatches: [] | 10 | strategicMergePatches: [] |
11 | postgresPassword: CHANGEME | 11 | postgresPassword: CHANGEME |
12 | password: CHANGEME | 12 | users: |
13 | database: CHANGEME | 13 | - name: CHANGEME |
14 | user: CHANGEME | 14 | password: CHANGEME |
15 | databases: | ||
16 | - name: CHANGEME | ||
17 | users: | ||
18 | - CHANGEME | ||
15 | images: | 19 | images: |
16 | debian: "debian:bullseye-20211220" | 20 | debian: "debian:bullseye-20211220" |
17 | postgresql: "bitnami/postgresql:14.1.0-debian-10-r66" | 21 | postgresql: "bitnami/postgresql:14.1.0-debian-10-r66" |
... | @@ -76,6 +80,9 @@ releases: | ... | @@ -76,6 +80,9 @@ releases: |
76 | - name: postgresql-scripts | 80 | - name: postgresql-scripts |
77 | configMap: | 81 | configMap: |
78 | name: {{ .Values.namePrefix }}postgresql-scripts | 82 | name: {{ .Values.namePrefix }}postgresql-scripts |
83 | - name: postgresql-preinit-scripts | ||
84 | configMap: | ||
85 | name: {{ .Values.namePrefix }}postgresql-preinit-scripts | ||
79 | - name: postgresql-secret | 86 | - name: postgresql-secret |
80 | secret: | 87 | secret: |
81 | secretName: {{ .Values.namePrefix }}postgresql-secret | 88 | secretName: {{ .Values.namePrefix }}postgresql-secret |
... | @@ -93,16 +100,25 @@ releases: | ... | @@ -93,16 +100,25 @@ releases: |
93 | name: {{ .Values.namePrefix }}postgresql-config | 100 | name: {{ .Values.namePrefix }}postgresql-config |
94 | namespace: {{ .Values.namespace }} | 101 | namespace: {{ .Values.namespace }} |
95 | data: | 102 | data: |
96 | POSTGRESQL_DATABASE: {{ .Values.postgresqlServer.database }} | 103 | {{- range $user_index, $user := .Values.postgresqlServer.users }} |
97 | POSTGRESQL_USER: {{ .Values.postgresqlServer.user }} | 104 | POSTGRESQL_USER_{{ $user_index }}_NAME: {{ $user.name | quote }} |
105 | {{- end }} | ||
106 | {{- range $db_index, $database := .Values.postgresqlServer.databases }} | ||
107 | POSTGRESQL_DATABASE_{{ $db_index }}_NAME: {{ $database.name | quote }} | ||
108 | {{- range $user_index, $user := $database.users }} | ||
109 | POSTGRESQL_DATABASE_{{ $db_index }}_USER_{{ $user_index }}: {{ $user | quote }} | ||
110 | {{- end }} | ||
111 | {{- end }} | ||
98 | - apiVersion: v1 | 112 | - apiVersion: v1 |
99 | kind: Secret | 113 | kind: Secret |
100 | metadata: | 114 | metadata: |
101 | name: {{ .Values.namePrefix }}postgresql-secret | 115 | name: {{ .Values.namePrefix }}postgresql-secret |
102 | namespace: {{ .Values.namespace }} | 116 | namespace: {{ .Values.namespace }} |
103 | stringData: | 117 | stringData: |
104 | POSTGRESQL_PASSWORD: {{ .Values.postgresqlServer.password }} | ||
105 | POSTGRESQL_POSTGRES_PASSWORD: {{ .Values.postgresqlServer.postgresPassword }} | 118 | POSTGRESQL_POSTGRES_PASSWORD: {{ .Values.postgresqlServer.postgresPassword }} |
119 | {{- range $user_index, $user := .Values.postgresqlServer.users }} | ||
120 | POSTGRESQL_USER_{{ $user_index }}_PASSWORD: {{ $user.password | quote }} | ||
121 | {{- end }} | ||
106 | {{- if not (empty (.Values.postgresqlServer.strategicMergePatches)) }} | 122 | {{- if not (empty (.Values.postgresqlServer.strategicMergePatches)) }} |
107 | {{- .Values.postgresqlServer.strategicMergePatches | toYaml | indent 6 }} | 123 | {{- .Values.postgresqlServer.strategicMergePatches | toYaml | indent 6 }} |
108 | {{- end }} | 124 | {{- end }} | ... | ... |
... | @@ -14,4 +14,8 @@ configMapGenerator: | ... | @@ -14,4 +14,8 @@ configMapGenerator: |
14 | - ./scripts/restore-from-empty | 14 | - ./scripts/restore-from-empty |
15 | - ./scripts/bitnami-postgresql-entrypoint-container.sh | 15 | - ./scripts/bitnami-postgresql-entrypoint-container.sh |
16 | - ./scripts/bitnami-postgresql-entrypoint-initContainer.sh | 16 | - ./scripts/bitnami-postgresql-entrypoint-initContainer.sh |
17 | - ./scripts/bitnami-create-extra-users.sh | ||
18 | - name: postgresql-preinit-scripts | ||
19 | files: | ||
20 | - ./scripts/bitnami-create-extra-users.sh | ||
17 | 21 | ... | ... |
... | @@ -45,6 +45,10 @@ spec: | ... | @@ -45,6 +45,10 @@ spec: |
45 | configMap: | 45 | configMap: |
46 | name: postgresql-scripts | 46 | name: postgresql-scripts |
47 | defaultMode: 0755 | 47 | defaultMode: 0755 |
48 | - name: postgresql-preinit-scripts | ||
49 | configMap: | ||
50 | name: postgresql-scripts | ||
51 | defaultMode: 0755 | ||
48 | - name: postgresql-config | 52 | - name: postgresql-config |
49 | configMap: | 53 | configMap: |
50 | name: postgresql-config | 54 | name: postgresql-config |
... | @@ -83,17 +87,13 @@ spec: | ... | @@ -83,17 +87,13 @@ spec: |
83 | mountPath: /var/postgresql-secret | 87 | mountPath: /var/postgresql-secret |
84 | - name: postgresql-entrypoint-initdb | 88 | - name: postgresql-entrypoint-initdb |
85 | mountPath: /docker-entrypoint-initdb.d | 89 | mountPath: /docker-entrypoint-initdb.d |
90 | - name: postgresql-preinit-scripts | ||
91 | mountPath: /docker-entrypoint-preinitdb.d | ||
86 | - name: etc | 92 | - name: etc |
87 | mountPath: /runtime-etc | 93 | mountPath: /runtime-etc |
88 | env: | 94 | env: |
89 | - name: POSTGRESQL_DATABASE_FILE | 95 | - name: POSTGRESQL_PASSWORD |
90 | value: /var/postgresql-config/POSTGRESQL_DATABASE | 96 | value: "foobar" |
91 | - name: POSTGRESQL_USER_FILE | ||
92 | value: /var/postgresql-config/POSTGRESQL_USER | ||
93 | - name: POSTGRESQL_PASSWORD_FILE | ||
94 | value: /var/postgresql-secret/POSTGRESQL_PASSWORD | ||
95 | - name: POSTGRESQL_POSTGRES_PASSWORD_FILE | ||
96 | value: /var/postgresql-secret/POSTGRESQL_POSTGRES_PASSWORD | ||
97 | 97 | ||
98 | containers: | 98 | containers: |
99 | - name: postgresql | 99 | - name: postgresql | ... | ... |
1 | #!/bin/bash | ||
2 | |||
3 | |||
4 | . /opt/bitnami/scripts/libpostgresql.sh | ||
5 | |||
6 | set -x | ||
7 | ls -al /var/postgresql-config /var/postgresql-secret | ||
8 | declare -i user_index=0 db_index=0 | ||
9 | while :; do | ||
10 | user_name="$(< /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME)" | ||
11 | user_password="$(< /var/postgresql-secret/POSTGRESQL_USER_${user_index}_PASSWORD)" | ||
12 | if [[ -z $user_name ]]; then | ||
13 | break | ||
14 | fi | ||
15 | postgresql_ensure_user_exists "$user_name" --password "$user_password" | ||
16 | user_index=$((user_index + 1)) | ||
17 | if [[ ! -e /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME ]]; then | ||
18 | break | ||
19 | fi | ||
20 | done | ||
21 | first_user="" | ||
22 | while :; do | ||
23 | database_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME)" | ||
24 | postgresql_ensure_database_exists "$database_name" | ||
25 | user_index=0 | ||
26 | while :; do | ||
27 | user_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index})" | ||
28 | if [[ -z $user_name ]]; then | ||
29 | break | ||
30 | elif [[ -z $first_user ]]; then | ||
31 | first_user="$user_name" | ||
32 | else | ||
33 | postgresql_ensure_user_has_database_privileges "$user_name" "$database_name" | ||
34 | fi | ||
35 | user_index=$((user_index + 1)) | ||
36 | if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index} ]]; then | ||
37 | break | ||
38 | fi | ||
39 | done | ||
40 | if [[ $first_user ]]; then | ||
41 | postgresql_ensure_user_has_database_privileges "$first_user" "$database_name" | ||
42 | fi | ||
43 | db_index=$((db_index + 1)) | ||
44 | if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME ]]; then | ||
45 | break | ||
46 | fi | ||
47 | done | ||
48 |
-
Please register or sign in to post a comment