Init container to run sql file

This commit is contained in:
Jansen Fuller
2026-05-01 16:40:27 -06:00
committed by Dustin J. Mitchell
parent a8f7281786
commit 27c3f954c9
4 changed files with 70 additions and 0 deletions

View File

@ -66,6 +66,14 @@ app.kubernetes.io/instance: {{ .Release.Name }}
{{- $uri -}} {{- $uri -}}
{{- end -}} {{- end -}}
{{- define "taskchampion-sync-server.schema-url" -}}
{{- if .Values.postgres.initContainer.schemaUrl -}}
{{- .Values.postgres.initContainer.schemaUrl -}}
{{- else -}}
{{- printf "https://raw.githubusercontent.com/GothenburgBitFactory/taskchampion-sync-server/v%s/postgres/schema.sql" .Chart.AppVersion -}}
{{- end -}}
{{- end -}}
{{- define "taskchampion-sync-server.postgres-secret-name" -}} {{- define "taskchampion-sync-server.postgres-secret-name" -}}
{{- printf "%s-connection" .Release.Name -}} {{- printf "%s-connection" .Release.Name -}}
{{- end -}} {{- end -}}

View File

@ -25,6 +25,56 @@ spec:
{{- end }} {{- end }}
securityContext: securityContext:
{{- toYaml .Values.securityContext | nindent 8 }} {{- toYaml .Values.securityContext | nindent 8 }}
{{- if and .Values.postgres.enabled .Values.postgres.initContainer.enabled }}
initContainers:
- name: postgres-init
image: "{{ .Values.postgres.initContainer.image }}"
imagePullPolicy: {{ .Values.postgres.initContainer.imagePullPolicy }}
env:
- name: PGHOST
valueFrom:
secretKeyRef:
name: {{ include "taskchampion-sync-server.postgres-secret-name" . }}
key: host
- name: PGPORT
value: "{{ .Values.postgres.port }}"
- name: PGUSER
valueFrom:
secretKeyRef:
name: {{ include "taskchampion-sync-server.postgres-secret-name" . }}
key: username
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: {{ include "taskchampion-sync-server.postgres-secret-name" . }}
key: password
- name: PGDATABASE
valueFrom:
secretKeyRef:
name: {{ include "taskchampion-sync-server.postgres-secret-name" . }}
key: database
- name: SCHEMA_URL
value: {{ include "taskchampion-sync-server.schema-url" . | quote }}
command:
- sh
- -c
- |
set -e
until pg_isready -h "$PGHOST" -p "$PGPORT" -U "$PGUSER"; do
echo 'Waiting for PostgreSQL...'
sleep 2
done
echo "Downloading schema from ${SCHEMA_URL}..."
wget -qO /tmp/schema.sql "$SCHEMA_URL" || {
echo 'Failed to download schema - continuing with main container'
exit 0
}
psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -f /tmp/schema.sql || {
echo 'Schema execution failed (SQL error) - continuing with main container'
exit 0
}
echo 'Schema executed successfully'
{{- end }}
containers: containers:
- name: taskchampion-sync-server - name: taskchampion-sync-server
{{- if eq .Values.postgres.enabled true }} {{- if eq .Values.postgres.enabled true }}

View File

@ -7,5 +7,10 @@ metadata:
{{- include "taskchampion-sync-server.labels" . | nindent 4 }} {{- include "taskchampion-sync-server.labels" . | nindent 4 }}
type: Opaque type: Opaque
data: data:
host: {{ .Values.postgres.host | b64enc }}
port: {{ .Values.postgres.port | b64enc }}
username: {{ .Values.postgres.username | b64enc }}
password: {{ .Values.postgres.password | b64enc }}
database: {{ .Values.postgres.database | b64enc }}
connection: {{ include "taskchampion-sync-server.postgres-connection" . | b64enc }} connection: {{ include "taskchampion-sync-server.postgres-connection" . | b64enc }}
{{- end }} {{- end }}

View File

@ -95,3 +95,10 @@ postgres:
port: 5432 port: 5432
username: user username: user
password: "" password: ""
initContainer:
enabled: true
image: postgres:15-alpine
imagePullPolicy: IfNotPresent
# Override the schema URL. Defaults to the official schema for this chart's appVersion.
# e.g. https://raw.githubusercontent.com/GothenburgBitFactory/taskchampion-sync-server/v0.7.0/postgres/schema.sql
schemaUrl: ""