Skip to content

K8SPG-1010 Fix secrets cleanup#1685

Open
oksana-grishchenko wants to merge 11 commits into
mainfrom
K8SPG-1010-upd-finalizers-order
Open

K8SPG-1010 Fix secrets cleanup#1685
oksana-grishchenko wants to merge 11 commits into
mainfrom
K8SPG-1010-upd-finalizers-order

Conversation

@oksana-grishchenko

Copy link
Copy Markdown
Contributor

Fix secrets cleanup

Problem:
K8SPG-1010
When a PerconaPGCluster with delete-pvc, delete-ssl, and delete-backups finalizers is deleted, the secrets removed by those finalizers get recreated and left behind after the cluster is fully gone.

Cause:
The root cause is an ordering issue in the deletion flow. The Percona reconciler runs its finalizers (which delete user and TLS secrets) before it issues a delete on the underlying PostgresCluster
object. At that point, the crunchy PostgresCluster is still fully alive and its reconciler is operational. Since the crunchy reconciler watches owned Secrets, each secret deletion triggers a reconcile
event. Because no DeletionTimestamp is set on the PostgresCluster yet, the crunchy reconciler treats this as normal operation and recreates the missing secrets.

The delete-backups finalizer makes this consistently reproducible because it triggers finishBackup, which performs multiple writes to the crunchy PostgresCluster (clearing ManualBackup status, modifying Jobs), repeatedly re-enqueuing the crunchy reconciler and widening the race window.

Solution:
Split finalizers into to two parts (Pre- and Post- deletion) so that the secret deletion finalizers would be processed after the upstream cluster if deleted.

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported PG version?
  • Does the change support oldest and newest supported Kubernetes version?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a deletion-ordering race in the PerconaPGCluster controller where Percona finalizers delete secrets before the upstream Crunchy PostgresCluster is marked for deletion, allowing the Crunchy reconciler to recreate those secrets.

Changes:

  • Split Percona finalizers into “pre-PostgresCluster deletion” and “post-PostgresCluster deletion” groups to ensure secret deletion happens after the upstream cluster is gone.
  • Update the controller deletion flow to delete the upstream PostgresCluster first and only then run post-deletion finalizers.
  • Extend the finalizers E2E scenario to include the delete-backups finalizer and assert secrets are not recreated after deletion.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
percona/controller/pgcluster/finalizer.go Refactors finalizer execution to support pre/post upstream deletion finalizer groups.
percona/controller/pgcluster/controller.go Updates deletion reconcile flow to wait for PostgresCluster deletion before running secret/PVC finalizers.
e2e-tests/tests/finalizers/01-create-cluster.yaml Adds percona.com/delete-backups to the test cluster finalizers.
e2e-tests/tests/finalizers/01-assert.yaml Updates expected finalizers to include delete-backups.
e2e-tests/tests/finalizers/02-delete-cluster.yaml Adds waits and assertions to detect recreated secrets after cluster deletion.
e2e-tests/tests/finalizers/03-create-cluster.yaml Adds percona.com/delete-backups to the second test cluster finalizers.
e2e-tests/tests/finalizers/03-assert.yaml Updates expected finalizers to include delete-backups.

Comment thread percona/controller/pgcluster/controller.go
Comment thread e2e-tests/tests/finalizers/02-delete-cluster.yaml
type finalizerEntry struct {
name string
fn controller.FinalizerFunc[*v2.PerconaPGCluster]
func (r *PGClusterReconciler) runFinalizers(ctx context.Context, cr *v2.PerconaPGCluster, finalizers []finalizerEntry) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after k8sClient.Delete(cr) + one Reconcile pass, but before the PostgresCluster is gone, maybe we can assert on a test that on the fetched CR that FinalizerStopWatchers and FinalizerDeleteBackups are already removed while FinalizerDeletePVC / FinalizerDeleteSSL are still present

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, thanks for the suggestion, added the "pre-deletion finalizers removed before PostgresCluster is gone" unit test

@oksana-grishchenko
oksana-grishchenko requested a review from gkech July 15, 2026 14:30
fn controller.FinalizerFunc[*v2.PerconaPGCluster]
}

func (r *PGClusterReconciler) prePostgresClusterDeletionFinalizers() []finalizerEntry {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment describing what needs to go into pre finalizers list, i think it'll be confusing in the future

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, done, thanks

egegunes
egegunes previously approved these changes Jul 16, 2026
nmarukovich
nmarukovich previously approved these changes Jul 16, 2026
@oksana-grishchenko
oksana-grishchenko dismissed stale reviews from nmarukovich and egegunes via add1675 July 16, 2026 12:09
@hors
hors requested review from egegunes and nmarukovich July 16, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants