Skip to content

[Bug]: Restoring a Pod along with its PVC can let a stale one-shot init container overwrite the data directory #11505

Description

@mnencia

Is there an existing issue already for this bug?

  • 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.

Cluster resource

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added this to the 1.31.0 milestone on Sep 18, 2026
  2. self-assigned this
    on Sep 18, 2026
  3. added theissue type on Sep 18, 2026
  4. added
    bug 🐛
    on Sep 18, 2026
  5. redbaron commented on Sep 18, 2026

    @redbaron
    Contributor

    Is it just a race in a fresh cluster of initdb or it can prevent stuff like restore from a backup?

  6. mnencia commented on Oct 2, 2026

    @mnencia
    MemberAuthor

    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.

  7. added a commit that references this issue on Oct 12, 2026
    8ec62db
  8. mnencia commented on Oct 19, 2026

    @mnencia
    MemberAuthor

    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.

    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-instance is not: on a valid PGDATA it moves the data aside and runs initdb. So a Pod restore is only one way to reach this. Any rerun of the init container on an existing volume does it.

  9. added a commit that references this issue on Oct 30, 2026
    54960d0
  10. added a commit that references this issue on Nov 1, 2026
    ed8eed8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions