diff --git a/helm/taskchampion-sync-server/templates/_helpers.tpl b/helm/taskchampion-sync-server/templates/_helpers.tpl index 9e97bf3..c0f0792 100644 --- a/helm/taskchampion-sync-server/templates/_helpers.tpl +++ b/helm/taskchampion-sync-server/templates/_helpers.tpl @@ -66,6 +66,14 @@ app.kubernetes.io/instance: {{ .Release.Name }} {{- $uri -}} {{- 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" -}} {{- printf "%s-connection" .Release.Name -}} {{- end -}} \ No newline at end of file diff --git a/helm/taskchampion-sync-server/templates/deployment.yaml b/helm/taskchampion-sync-server/templates/deployment.yaml index f059489..8002152 100644 --- a/helm/taskchampion-sync-server/templates/deployment.yaml +++ b/helm/taskchampion-sync-server/templates/deployment.yaml @@ -25,6 +25,56 @@ spec: {{- end }} securityContext: {{- 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: - name: taskchampion-sync-server {{- if eq .Values.postgres.enabled true }} diff --git a/helm/taskchampion-sync-server/templates/secrets/postgres-secret.yaml b/helm/taskchampion-sync-server/templates/secrets/postgres-secret.yaml index 8364a23..07b13d9 100644 --- a/helm/taskchampion-sync-server/templates/secrets/postgres-secret.yaml +++ b/helm/taskchampion-sync-server/templates/secrets/postgres-secret.yaml @@ -7,5 +7,10 @@ metadata: {{- include "taskchampion-sync-server.labels" . | nindent 4 }} type: Opaque 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 }} {{- end }} \ No newline at end of file diff --git a/helm/taskchampion-sync-server/values.yaml b/helm/taskchampion-sync-server/values.yaml index 9ffac05..c7afab1 100644 --- a/helm/taskchampion-sync-server/values.yaml +++ b/helm/taskchampion-sync-server/values.yaml @@ -95,3 +95,10 @@ postgres: port: 5432 username: user 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: ""