Skip to content

Agent Mesh

charts: non-root postgres needs a data directory it owns

Antonio Ojea843a4b813495
diff --git a/charts/sam-mesh/templates/db-statefulset.yaml b/charts/sam-mesh/templates/db-statefulset.yaml
index 18c6b7a4..ae18ea11 100644
--- a/charts/sam-mesh/templates/db-statefulset.yaml
+++ b/charts/sam-mesh/templates/db-statefulset.yaml
@@ -42,6 +42,11 @@ spec:
{{- toYaml . | nindent 10 }}
{{- end }}
env:
+ # initdb chmods its data directory; running as uid 70 it cannot do
+ # that to the PVC root (owned by root, group-shared via fsGroup), so
+ # the cluster lives in a subdirectory postgres creates and owns.
+ - name: PGDATA
+ value: /var/lib/postgresql/data/pgdata
- name: POSTGRES_DB
value: {{ .Values.database.postgres.database | quote }}
- name: POSTGRES_USER
diff --git a/charts/sam-mesh/tests/db-statefulset_test.yaml b/charts/sam-mesh/tests/db-statefulset_test.yaml
index ab3b936f..bc632cbd 100644
--- a/charts/sam-mesh/tests/db-statefulset_test.yaml
+++ b/charts/sam-mesh/tests/db-statefulset_test.yaml
@@ -36,6 +36,12 @@ tests:
- contains:
path: spec.template.spec.containers[0].securityContext.capabilities.drop
content: ALL
+ # Non-root initdb needs a data directory it owns, below the PVC root.
+ - contains:
+ path: spec.template.spec.containers[0].env
+ content:
+ name: PGDATA
+ value: /var/lib/postgresql/data/pgdata
- it: scheduling knobs pass through to the pod spec
set:
diff --git a/charts/sam-mesh/values.yaml b/charts/sam-mesh/values.yaml
index b4e06b36..c67b9d53 100644
--- a/charts/sam-mesh/values.yaml
+++ b/charts/sam-mesh/values.yaml
@@ -83,8 +83,12 @@ database:
port: 5432
sslmode: disable
storageSize: 1Gi
- # postgres:16-alpine runs as uid 70 (postgres) and needs the data dir
- # owned by it; fsGroup does that on the PVC.
+ # postgres:16-alpine runs as uid 70 (postgres); the data directory is
+ # the pgdata/ subdirectory of the PVC (initdb must own what it chmods,
+ # and the PVC root belongs to root). fsGroup gives uid 70 group access
+ # to create it. Upgrading a release whose postgres ran as root with
+ # data at the PVC root: move that data into pgdata/ first, or the new
+ # pod initialises an empty cluster beside it.
podSecurityContext:
runAsNonRoot: true
runAsUser: 70