You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Restoring a Pod along with its PVC can let a stale one-shot init container overwrite the data directory #11505
I have searched for an existing issue, and could not find anything. I believe this is a new bug.
I have read the troubleshooting guide
I have read the troubleshooting guide and I think this is a new bug.
I am running a supported version of CloudNativePG
I have read the troubleshooting guide and I think this is a new bug.
Contact Details
No response
Version
trunk (main)
What version of Kubernetes are you using?
1.37
What is your Kubernetes environment?
Self-managed: kind (evaluation)
How did you install the operator?
YAML manifest
What happened?
The bootstrap-instance init container runs once per instance to initialize the data directory with initdb, pg_basebackup, or restore, whichever role the controller picked when the Pod was first created.
Kubernetes only tracks whether an init container ran on a Pod object, not whether it already did its job on the volume. So when a backup/restore tool restores a Pod together with its PVC, the new Pod reruns bootstrap-instance on a volume that already holds a database.
If that role is initdb, EnsureTargetDirectoriesDoNotExist tries to guard against this: it renames the existing data directory aside if pg_controldata can read it, or deletes it otherwise. Either way, the running instance loses the restored data: renamed data sits on disk under a different path, and deleted data is gone.
Reproduced locally: data written before the backup is no longer in the cluster after the restore, and the instance comes up on a fresh, empty data directory.
Expected: initdb never runs against a volume that already has data.
Related: #11503 is the join-variant of the same bug.
Is it just a race in a fresh cluster of initdb or it can prevent stuff like restore from a backup?
@redbaron neither. A fresh cluster and CNPG's own recovery from a backup are not affected: bootstrap-instance runs once, on an empty volume.
The bug needs an external tool (Velero, for example) that restores a Pod object together with its PVC. Kubernetes then runs the init container again, on a volume that already has data.
The same failure shows up without an external restore tool. In our nightly e2e on OpenShift 4.22 (E2E Drain Node, "can drain the primary pod's node with 3 pods on 2 nodes"), bootstrap-instance ran twice on the same pod and PVC.
The first run, at 22:51:03, created the cluster and stopped cleanly. At 22:52:06 kubelet logged SandboxChanged for the same pod (same uid). At 22:52:08 bootstrap-instance ran again. It found a valid data directory (pg_controldata check on existing directory succeeded, renaming the folders), renamed pgdata and pg_wal with a timestamp suffix, and started a new initdb. The container was killed one second later. The recreated pod then looped on pg_rewind: error: source and target clusters are from different systems.
Activity
redbaron commentedon Sep 18, 2026
Is it just a race in a fresh cluster of initdb or it can prevent stuff like restore from a backup?
revert(restore): break certificate-wait deadlock in orphan-PVC restore
revert(restore): break certificate-wait deadlock in orphan-PVC restore (
revert(restore): break certificate-wait deadlock in orphan-PVC restore (
revert(restore): break certificate-wait deadlock in orphan-PVC restore (
revert(restore): break certificate-wait deadlock in orphan-PVC restore (
mnencia commentedon Oct 2, 2026
@redbaron neither. A fresh cluster and CNPG's own recovery from a backup are not affected:
bootstrap-instanceruns once, on an empty volume.The bug needs an external tool (Velero, for example) that restores a Pod object together with its PVC. Kubernetes then runs the init container again, on a volume that already has data.
fix(bootstrap): leave volumes that already hold the instance untouched
mnencia commentedon Oct 19, 2026
The same failure shows up without an external restore tool. In our nightly e2e on OpenShift 4.22 (
E2E Drain Node, "can drain the primary pod's node with 3 pods on 2 nodes"),bootstrap-instanceran twice on the same pod and PVC.The first run, at 22:51:03, created the cluster and stopped cleanly. At 22:52:06 kubelet logged
SandboxChangedfor the same pod (same uid). At 22:52:08bootstrap-instanceran again. It found a valid data directory (pg_controldata check on existing directory succeeded, renaming the folders), renamedpgdataandpg_walwith a timestamp suffix, and started a newinitdb. The container was killed one second later. The recreated pod then looped onpg_rewind: error: source and target clusters are from different systems.We don't know why the sandbox changed. Kubernetes documents that replacing the pod infrastructure container reruns the init containers, and says init containers must be idempotent (https://kubernetes.io/docs/concepts/workloads/pods/init-containers/).
bootstrap-instanceis not: on a validPGDATAit moves the data aside and runsinitdb. So a Pod restore is only one way to reach this. Any rerun of the init container on an existing volume does it.fix(bootstrap): leave volumes that already hold the instance untouched
fix(bootstrap): leave volumes that already hold the instance untouched (