-
Notifications
You must be signed in to change notification settings - Fork 91
helm: add db-init pre-install hook for external PostgreSQL #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| {{- if and .Values.dbInit.enabled (not .Values.postgresql.enabled) }} | ||
| apiVersion: batch/v1 | ||
| kind: Job | ||
| metadata: | ||
| name: {{ printf "%s-db-init" (include "codex-lb.fullname" . | trunc 52 | trimSuffix "-") }} | ||
| namespace: {{ .Release.Namespace | quote }} | ||
| labels: | ||
| {{- include "codex-lb.labels" . | nindent 4 }} | ||
| annotations: | ||
| "helm.sh/hook": pre-install | ||
| "helm.sh/hook-weight": "-10" | ||
| "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded | ||
| spec: | ||
| template: | ||
| spec: | ||
| restartPolicy: OnFailure | ||
| {{- with .Values.nodeSelector }} | ||
| nodeSelector: | ||
| {{- toYaml . | nindent 8 }} | ||
|
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new db-init Job reads only Useful? React with 👍 / 👎. |
||
| {{- end }} | ||
| containers: | ||
| - name: db-init | ||
| image: {{ printf "%s/bitnami/postgresql:16" (.Values.global.imageRegistry | default "docker.io") }} | ||
| command: ["sh", "-ec"] | ||
| args: | ||
| - | | ||
| PGPASSWORD="$ADMIN_PASSWORD" psql \ | ||
| -h "$DB_HOST" -p "$DB_PORT" -U "$ADMIN_USER" -d postgres <<'SQL' | ||
| {{- range .Values.dbInit.databases }} | ||
| DO $$ BEGIN | ||
| IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '{{ .user }}') THEN | ||
| CREATE ROLE {{ .user }} WITH LOGIN PASSWORD '{{ .password }}'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This statement injects Helm values directly into SQL without identifier/literal escaping, so valid inputs like a username containing Useful? React with 👍 / 👎. |
||
| END IF; | ||
| END $$; | ||
| SELECT format('CREATE DATABASE %I OWNER %I', '{{ .name }}', '{{ .user }}') | ||
| WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '{{ .name }}')\gexec | ||
| GRANT ALL PRIVILEGES ON DATABASE {{ .name }} TO {{ .user }}; | ||
| {{- end }} | ||
| SQL | ||
| env: | ||
| - name: DB_HOST | ||
| value: {{ .Values.dbInit.host | quote }} | ||
| - name: DB_PORT | ||
| value: {{ .Values.dbInit.port | default "5432" | quote }} | ||
| - name: ADMIN_USER | ||
| value: {{ .Values.dbInit.adminUser | quote }} | ||
| - name: ADMIN_PASSWORD | ||
| {{- if .Values.dbInit.adminPasswordSecret }} | ||
| valueFrom: | ||
| secretKeyRef: | ||
| name: {{ .Values.dbInit.adminPasswordSecret.name }} | ||
| key: {{ .Values.dbInit.adminPasswordSecret.key }} | ||
| {{- else }} | ||
| value: {{ .Values.dbInit.adminPassword | quote }} | ||
| {{- end }} | ||
| backoffLimit: 3 | ||
| {{- end }} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,12 @@ spec: | |
| {{- range $pullSecrets }} | ||
| - name: {{ . }} | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- if .Values.postgresql.enabled }} | ||
| {{- end }} | ||
| {{- with (include "codex-lb.nodeSelector" .) }} | ||
| nodeSelector: | ||
| {{- . | nindent 8 }} | ||
|
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The migration Job has the same over-indented Useful? React with 👍 / 👎. |
||
| {{- end }} | ||
| {{- if .Values.postgresql.enabled }} | ||
| initContainers: | ||
| - name: wait-for-db | ||
| image: postgres:16-alpine | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
nodeSelectorkey is indented one space deeper than sibling pod-spec fields, so whennodeSelector/global.nodeSelectoris set and this block renders, Helm emits invalid YAML for the Deployment and install/template fails with a parse error. The key needs to align with other fields underspec.template.spec.Useful? React with 👍 / 👎.