@@ -3,16 +3,37 @@ Reusable snippets shared across Vela Helm templates.
33*/ }}
44
55{{/*
6- Renders a Postgres init container that waits for the target database to accept connections.
6+ Returns the plaintext password for a given DB credential key, preserving it across upgrades
7+ via lookup of the existing vela-controller-secret. On fresh installs the password is derived
8+ deterministically from the release name, namespace, and key so that all templates in the same
9+ render produce the same value.
10+
11+ Usage: {{ include "vela.dbPassword" (list "controller-db-password" .) }}
12+ */ }}
13+ {{- define " vela.dbPassword" -}}
14+ {{- $key := index . 0 -}}
15+ {{- $ctx := index . 1 -}}
16+ {{- $existingSecret := lookup " v1" " Secret" $ctx .Release.Namespace " vela-controller-secret" -}}
17+ {{- if and $existingSecret (index $existingSecret .data $key ) -}}
18+ {{- index $existingSecret .data $key | b64dec -}}
19+ {{- else -}}
20+ {{- printf " %s -%s -%s " $ctx .Release.Name $ctx .Release.Namespace $key | sha256sum | trunc 32 -}}
21+ {{- end -}}
22+ {{- end -}}
23+
24+ {{/*
25+ Renders a Postgres init container that waits for the server to be ready.
26+ When `database` is provided it also waits until that specific database accepts connections.
727The helper accepts a dictionary with the following optional keys:
828 - name : init container name (default: wait-for-database)
929 - image : container image (default: postgres:17-alpine)
1030 - imagePullPolicy : pull policy (default: IfNotPresent)
1131 - host : database hostname (default: database)
1232 - port : database port (default: 5432)
13- - secretName : Kubernetes secret with credentials (default: database)
14- - usernameKey : Secret key used for DB username (default: superuser-username)
15- - passwordKey : Secret key used for DB password (default: superuser-password)
33+ - database : if set, block until this database accepts connections
34+ - secretName : secret containing credentials for the psql check (default: database)
35+ - usernameKey : key for the DB username in secretName (default: superuser-username)
36+ - passwordKey : key for the DB password in secretName (default: superuser-password)
1637 - securityContext : optional security context applied to the init container
1738*/ }}
1839{{- define " vela.waitForPostgresInitContainer" -}}
@@ -21,6 +42,7 @@ The helper accepts a dictionary with the following optional keys:
2142{{- $imagePullPolicy := default " IfNotPresent" .imagePullPolicy -}}
2243{{- $host := default " database" .host -}}
2344{{- $port := default " 5432" .port -}}
45+ {{- $database := .database -}}
2446{{- $secretName := default " database" .secretName -}}
2547{{- $usernameKey := default " superuser-username" .usernameKey -}}
2648{{- $passwordKey := default " superuser-password" .passwordKey -}}
@@ -32,6 +54,7 @@ The helper accepts a dictionary with the following optional keys:
3254 value: {{ $host | quote }}
3355 - name: DB_PORT
3456 value: {{ $port | quote }}
57+ {{- if $database }}
3558 - name: DB_USER
3659 valueFrom:
3760 secretKeyRef:
@@ -42,20 +65,22 @@ The helper accepts a dictionary with the following optional keys:
4265 secretKeyRef:
4366 name: {{ $secretName }}
4467 key: {{ $passwordKey }}
68+ {{- end }}
4569 command: [" /bin/sh" , " -c" ]
4670 args:
4771 - |
4872 echo " Waiting for database..."
49- until pg_isready -h " $DB_HOST" -p " $DB_PORT" -U " $DB_USER " ; do
73+ until pg_isready -h " $DB_HOST" -p " $DB_PORT" ; do
5074 sleep 2
5175 done
5276 echo " Database is ready"
77+ {{- if $database }}
5378
54- # Ensure postgres user can connect
55- until psql -h " $DB_HOST" -U " $DB_USER" -d postgres -c '\q' 2>/dev/null; do
56- echo " Waiting for Postgres superuser connection..."
79+ until psql -h " $DB_HOST" -U " $DB_USER" -d {{ $database | quote }} -c '\q' 2>/dev/null; do
80+ echo " Waiting for Postgres connection to {{ $database }}..."
5781 sleep 2
5882 done
83+ {{- end }}
5984{{- with .securityContext }}
6085 securityContext:
6186{{ toYaml . | nindent 4 }}
0 commit comments