Files
cozystack/packages/apps/postgres/templates/init-job.yaml
T
Mattia EleuteriandMatthieu dc2971264d feat(postgres): add a postgis image flavor
Tenants migrating spatial data need PostGIS, but the chart always runs
the CloudNativePG PostgreSQL image, which does not ship it, and exposes
no way to pick another one. CloudNativePG publishes a PostGIS operand
image; a closed `flavor` enum selects it without handing tenants a free
image field.

The flavor pins the same PostgreSQL minor as the default one, on the
standard-trixie variant: backups go through the barman-cloud plugin,
so the deprecated system variant is not needed. update-versions.sh now
derives the postgis map from the minors it picks, and a bats check
fails if the two maps drift apart.

The default images are built on bullseye and the postgis ones on
trixie. Switching a live cluster between them would swap glibc under
an initialised data directory and silently break every collatable
index, and CNPG would roll it out as a plain image update. The chart
therefore refuses a flavor change on an existing Cluster.

Co-authored-by: Matthieu <matthieu@hidora.com>
Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-10-01 18:30:37 +02:00

124 lines
5.0 KiB
YAML

{{/*
Skip the post-install/post-upgrade init Job entirely while the chart
renders bootstrap.recovery. The platform gives hooks the HelmRelease
upgrade timeout (10m by default, the cozystack-api
--helmrelease-upgrade-timeout flag, overridable per-Application), but the
Job's pg_isready loop can't drain that budget while CNPG is still
streaming the restore from S3 - the hook then times out and
Helm rolls the upgrade back to the previous bootstrap.initdb spec,
which makes every subsequent recovery pod panic in CNPG's
InitInfo.loadBackup (Recovery == nil after the rollback).
Recovered postgres carries source's roles/databases at the on-disk
level, so the chart's role/db management can re-converge on the next
user-driven helm upgrade after recovery completes.
*/}}
{{- if not .Values.bootstrap.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Release.Name }}-init-job
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
{{- /*
This Job is a post-install,post-upgrade Helm hook Helm blocks on. Bound it under
the 10m hook budget so its own DeadlineExceeded - or the readiness loop's
diagnostic exit - surfaces first, instead of Helm's generic 10m timeout that
says nothing about why. Both contexts get a value well below 600s but with real
headroom for the work the Job does after the pod is running: the "grant
privileges" step below runs a per-schema DO block (GRANT ALL + ALTER DEFAULT
PRIVILEGES over every schema of every managed database), so a large existing
database needs room, not just the seconds pg_isready takes. On UPGRADE the
Cluster is already up (the loop answers in seconds) and the hazard is a
concurrent restore purging the target and its <release>-superuser Secret, whose
pod then FailedMounts forever; 540s bounds that while leaving the grant loop
~9m. On INSTALL nothing is running yet - the same release is provisioning the
Cluster (PVC, image pull, initdb), Helm's wait does not cover a CR, and there is
no purge hazard (a restore-to-copy installs with bootstrap.enabled=true, which
skips this Job) - so the cold start gets 570s rather than a tight deadline that
would fail a slow-but-healthy first boot. A database with a pathological number
of schemas can still outrun even this; that is the documented ceiling, the same
one Helm's own hook budget imposes. Do not retry the pod either way: the script
is idempotent and the purged pod stays Pending on the missing mount, so a retry
buys nothing.
*/}}
activeDeadlineSeconds: {{ if .Release.IsInstall }}570{{ else }}540{{ end }}
backoffLimit: 0
ttlSecondsAfterFinished: 3600
template:
metadata:
name: {{ .Release.Name }}-init-job
annotations:
checksum/config: {{ include (print $.Template.BasePath "/init-script.yaml") . | sha256sum }}
spec:
restartPolicy: Never
containers:
- name: postgres
image: {{ include "postgres.imageName" $ }}
command:
- bash
- /scripts/init.sh
env:
- name: PGUSER
valueFrom:
secretKeyRef:
name: {{ .Release.Name }}-superuser
key: username
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: {{ .Release.Name }}-superuser
key: password
- name: PGHOST
value: {{ .Release.Name }}-rw
- name: PGPORT
value: "5432"
- name: PGDATABASE
value: postgres
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
readOnlyRootFilesystem: true
runAsNonRoot: true
volumeMounts:
- mountPath: /etc/secret
name: secret
- mountPath: /scripts
name: scripts
{{- if .Values.users }}
# The init script reads each user's password from this mount at run time
# so the credential is never rendered into the init-script Secret. Gated
# on .Values.users to match the <release>-credentials Secret, which is
# only rendered when there are users: mounting it unconditionally would
# FailedMount a userless release and hang the post-upgrade hook.
- mountPath: /credentials
name: credentials
readOnly: true
{{- end }}
{{- include "postgresjobs.resources" . | nindent 8 }}
securityContext:
fsGroup: 26
runAsGroup: 26
runAsNonRoot: true
runAsUser: 26
seccompProfile:
type: RuntimeDefault
volumes:
- name: secret
secret:
secretName: {{ .Release.Name }}-superuser
- name: scripts
secret:
secretName: {{ .Release.Name }}-init-script
{{- if .Values.users }}
- name: credentials
secret:
secretName: {{ .Release.Name }}-credentials
{{- end }}
{{- end }}