Skip to content

K8SPG-992: remove keep-job finalizer if cluster is deleted - #1724

Open
pooknull wants to merge 7 commits into
mainfrom
K8SPG-992-fix
Open

K8SPG-992: remove keep-job finalizer if cluster is deleted#1724
pooknull wants to merge 7 commits into
mainfrom
K8SPG-992-fix

Conversation

@pooknull

@pooknull pooknull commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

https://perconadev.atlassian.net/browse/K8SPG-992

DESCRIPTION

Problem:
When cluster is deleted while a backup is running, the backup job can keep the keep-job finalizer, preventing the job from being deleted.

Solution:
delete-backup finalizer should find the backup job if the cluster is unavailable and remove the keep-job finalizer.

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?

@hors
hors requested a review from Copilot August 4, 2026 20:16

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 updates the PGBackup controller finalization flow to ensure backup Jobs don’t remain stuck with the keep-job finalizer when the owning cluster is gone (or being deleted), allowing those Jobs to be cleaned up properly.

Changes:

  • Remove the keep-job finalizer from the backup Job during backup finalization when the cluster is unavailable (pg == nil).
  • Extend unit test coverage to create a Job with keep-job and assert the finalizer gets removed when the cluster is unavailable.

Reviewed changes

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

File Description
percona/controller/pgbackup/controller.go Adds logic to remove the Job’s keep-job finalizer when the cluster is unavailable during backup finalization.
percona/controller/pgbackup/controller_test.go Updates tests to include a Job object and verifies the keep-job finalizer is removed in the cluster-unavailable scenario.

Comment thread percona/controller/pgbackup/controller.go
Comment thread percona/controller/pgbackup/controller.go
@pooknull
pooknull marked this pull request as ready for review August 5, 2026 11:24
Comment on lines +406 to +408
// If the cluster was previously deleted, we cannot call finishBackup to remove
// annotations from the PGCluster. In that case, we no longer need the job to
// exist and we can remove the keep-job finalizer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand why the removal of keep-job is tied to the execution of delete-backup finalizer. Doesn't this mean we will still have this issue if the delete-backup finalizer is not present?

Shouldn't we instead set a watch on the Job and Cluster, requeue events for corresponding PGBackup and at the top of Reconcile remove the finalizer on the Job when pgCluster == nil (or if backup is in a terminal state)?

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.

The delete-backup finalizer is added before the backup job is created. So a backup job with keep-job cannot exist without the corresponding pgbackup having the delete-backup finalizer.

The reason for tying keep-job removal to the delete-backup finalizer is the idea that delete-backup owns the backup cleanup sequence: it removes the backup labels and annotations from the cluster and then releases keep-job in the required order. If the cluster no longer exists, releasing keep-job is still the remaining part of the same cleanup operation, so it belongs in this finalizer.

Moving this logic to the top of Reconcile would not change the behavior unless someone manually removed the delete-backup finalizer, which users should not touch.

@pooknull
pooknull requested a review from mayankshah1607 August 6, 2026 13:03
@JNKPercona

Copy link
Copy Markdown
Collaborator
Test Name Result Time
backup-enable-disable passed 00:18:42
builtin-extensions passed 00:06:21
custom-extensions passed 00:21:21
cert-manager-tls passed 00:11:14
cert-management-policy failure 00:05:17
custom-envs passed 00:20:17
custom-tls passed 00:07:23
database-init-sql passed 00:03:15
demand-backup passed 00:25:25
demand-backup-offline-snapshot passed 00:13:57
dynamic-configuration passed 00:03:43
extra-volumes passed 00:04:23
finalizers passed 00:04:44
init-deploy passed 00:03:11
huge-pages passed 00:03:17
major-upgrade-14-to-15 passed 00:11:48
major-upgrade-15-to-16 passed 00:12:18
major-upgrade-16-to-17 passed 00:10:45
major-upgrade-17-to-18 passed 00:14:21
ldap passed 00:03:54
ldap-tls passed 00:08:09
logcollection passed 00:05:52
monitoring passed 00:09:44
one-pod passed 00:05:52
repo-host-autogrow passed 00:02:01
operator-self-healing passed 00:10:39
pgbouncer-mtls passed 00:03:38
pg-tde passed 00:11:56
pg-tde-wal-encrypt passed 00:16:35
pitr passed 00:11:51
scaling passed 00:05:31
scheduled-backup passed 00:31:22
self-healing passed 00:09:25
sidecars passed 00:03:04
standby-pgbackrest passed 00:17:58
standby-streaming passed 00:13:16
start-from-backup passed 00:12:24
tablespaces passed 00:07:17
telemetry-transfer passed 00:04:34
upgrade-consistency passed 00:06:03
upgrade-minor passed 00:06:51
users passed 00:04:42
migration-from-crunchy-standby passed 00:19:04
migration-from-crunchy-pv passed 00:08:13
migration-from-crunchy-backup-restore passed 00:16:39
Summary Value
Tests Run 45/45
Job Duration 01:45:36
Total Test Time 07:38:38

commit: 9924e38
image: perconalab/percona-postgresql-operator:PR-1724-9924e38a1

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