Skip to content

Commit ece00e0

Browse files
committed
fix: wait for Postgres before starting backend
The CNPG Cluster takes ~25-30s to bootstrap initdb, but the backend Deployment is applied at the same time as the Cluster. The new Backstage backend attempts its database connection once at startup; on failure it stays up in a degraded state (liveness 200, readiness 503) instead of crash-looping, so Kubernetes never restarts it and the rollout never completes. This failed the kind e2e and would equally strand the pod in production, where Flux applies the Cluster and Deployment together. Add a wait-for-postgres initContainer to the database-cnpg component so the backend only starts once Postgres accepts connections. It reuses the CNPG image (pg_isready, already cached on the nodes) and mirrors the hardened container securityContext.
1 parent 1fd6787 commit ece00e0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

config/components/database-cnpg/patch-deployment.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@ metadata:
55
spec:
66
template:
77
spec:
8+
initContainers:
9+
- name: wait-for-postgres
10+
image: ghcr.io/cloudnative-pg/postgresql:17
11+
command:
12+
- sh
13+
- -c
14+
- until pg_isready -h "$POSTGRES_HOST" -p "$POSTGRES_PORT" -U "$POSTGRES_USER"; do echo "waiting for postgres at $POSTGRES_HOST:$POSTGRES_PORT"; sleep 2; done
15+
env:
16+
- name: POSTGRES_HOST
17+
valueFrom:
18+
secretKeyRef:
19+
name: backstage-postgres-app
20+
key: host
21+
- name: POSTGRES_PORT
22+
valueFrom:
23+
secretKeyRef:
24+
name: backstage-postgres-app
25+
key: port
26+
- name: POSTGRES_USER
27+
valueFrom:
28+
secretKeyRef:
29+
name: backstage-postgres-app
30+
key: username
31+
securityContext:
32+
allowPrivilegeEscalation: false
33+
readOnlyRootFilesystem: true
34+
capabilities:
35+
drop:
36+
- ALL
837
containers:
938
- name: backstage
1039
env:

0 commit comments

Comments
 (0)