Skip to content

Commit ce62f5d

Browse files
committed
fix(backups): keep the CNPG purge for unmarked Clusters this restore did not render
A recovery Cluster without the restored-server-name annotation fell back to the creation-time comparison. On the current chart the annotation is absent when bootstrap.newServerName is empty, i.e. a manually configured recovery: re-rendered during the WAL-archive wait it is created after StartedAt, the purge was skipped and the restore could report success on the manual recovery source. Use the creation-time fallback only when the application's bootstrap.newServerName already carries this restore's server name, which keeps it for Clusters rendered by a chart that predates the annotation. Assisted-By: LLM Signed-off-by: Andrey Kolkov <[email protected]>
1 parent b1def5f commit ce62f5d

2 files changed

Lines changed: 65 additions & 12 deletions

File tree

‎internal/backupcontroller/cnpgstrategy_controller.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,24 @@ func cnpgPurgeNeeded(purgedCondition, liveClusterFreshlyRecovered bool) bool {
449449
// restore that rendered the Cluster. When the annotation is present, the
450450
// Cluster is fresh only if it carries wantMark.
451451
//
452-
// A Cluster rendered by a chart that predates the annotation has none; it then
453-
// falls back to the creation-time comparison (created strictly after
454-
// StartedAt), which still separates a Cluster from an earlier completed restore
455-
// (created before StartedAt) from this restore's own re-render.
452+
// A Cluster without the annotation was rendered either by a chart that
453+
// predates it or from values with no bootstrap.newServerName (a manually
454+
// configured recovery). It is fresh only if appMark, the application's current
455+
// bootstrap.newServerName, is wantMark - this restore has patched the values -
456+
// and it was created strictly after StartedAt, which separates a Cluster from
457+
// an earlier completed restore from this restore's own re-render.
456458
//
457459
// Returns false when freshness cannot be determined (no recovery bootstrap, or
458460
// a missing timestamp on the fallback path): the caller then purges, which is
459461
// the safe default.
460-
func cnpgClusterFreshlyRecovered(state recoveryClusterState, wantMark string, restoreStartedAt *metav1.Time) bool {
462+
func cnpgClusterFreshlyRecovered(state recoveryClusterState, wantMark, appMark string, restoreStartedAt *metav1.Time) bool {
461463
if !state.hasRecovery {
462464
return false
463465
}
464466
if state.restoreMark != "" {
465467
return state.restoreMark == wantMark
466468
}
467-
if state.createdAt == nil || restoreStartedAt == nil {
469+
if appMark != wantMark || state.createdAt == nil || restoreStartedAt == nil {
468470
return false
469471
}
470472
return state.createdAt.After(restoreStartedAt.Time)
@@ -864,7 +866,7 @@ func (r *RestoreJobReconciler) reconcileCNPGRestore(ctx context.Context, restore
864866
// produced. A recovery Cluster rendered for any other restore still holds
865867
// that restore's data, so it must be purged - otherwise a repeat in-place
866868
// restore silently no-ops.
867-
freshlyRecovered := cnpgClusterFreshlyRecovered(clusterState, restoredServerName(clusterName, restoreJob.UID), restoreJob.Status.StartedAt)
869+
freshlyRecovered := cnpgClusterFreshlyRecovered(clusterState, restoredServerName(clusterName, restoreJob.UID), targetApp.Spec.Bootstrap.NewServerName, restoreJob.Status.StartedAt)
868870
if cnpgPurgeNeeded(purgedCondition, freshlyRecovered) {
869871
// A physical restore only opens on the major version that wrote the
870872
// data files. Across majors the recovery pods loop on "database files

‎internal/backupcontroller/cnpgstrategy_controller_test.go‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ func TestCNPGClusterFreshlyRecovered(t *testing.T) {
654654
cases := []struct {
655655
name string
656656
state recoveryClusterState
657+
appMark string
657658
startedAt *metav1.Time
658659
want bool
659660
}{
@@ -690,37 +691,56 @@ func TestCNPGClusterFreshlyRecovered(t *testing.T) {
690691
{
691692
name: "unmarked, created after start (our own re-render by an older chart): fresh",
692693
state: recoveryClusterState{hasRecovery: true, createdAt: &after},
694+
appMark: ours,
693695
startedAt: &started,
694696
want: true,
695697
},
698+
{
699+
name: "unmarked, created after start, values not patched by this restore (manual recovery re-rendered): not fresh",
700+
state: recoveryClusterState{hasRecovery: true, createdAt: &after},
701+
appMark: "",
702+
startedAt: &started,
703+
want: false,
704+
},
705+
{
706+
name: "unmarked, created after start, values patched by another restore: not fresh",
707+
state: recoveryClusterState{hasRecovery: true, createdAt: &after},
708+
appMark: other,
709+
startedAt: &started,
710+
want: false,
711+
},
696712
{
697713
name: "unmarked, created before start (leftover from previous restore): not fresh",
698714
state: recoveryClusterState{hasRecovery: true, createdAt: &before},
715+
appMark: ours,
699716
startedAt: &started,
700717
want: false,
701718
},
702719
{
703720
name: "unmarked, created exactly at start: not fresh (conservative tie -> purge)",
704721
state: recoveryClusterState{hasRecovery: true, createdAt: &started},
722+
appMark: ours,
705723
startedAt: &started,
706724
want: false,
707725
},
708726
{
709727
name: "unmarked, missing cluster creation timestamp: not fresh",
710728
state: recoveryClusterState{hasRecovery: true},
729+
appMark: ours,
711730
startedAt: &started,
712731
want: false,
713732
},
714733
{
715734
name: "unmarked, missing job start timestamp: not fresh",
716735
state: recoveryClusterState{hasRecovery: true, createdAt: &after},
736+
appMark: ours,
717737
startedAt: nil,
718738
want: false,
719739
},
720740
}
721741
for _, tc := range cases {
722742
t.Run(tc.name, func(t *testing.T) {
723-
got := cnpgClusterFreshlyRecovered(tc.state, ours, tc.startedAt)
743+
got := cnpgClusterFreshlyRecovered(tc.state, ours, tc.appMark, tc.startedAt)
724744
if got != tc.want {
725745
t.Errorf("got %v want %v", got, tc.want)
726746
}
@@ -2388,12 +2408,16 @@ func TestReconcileCNPGRestore_RepeatInPlacePurgesStaleRecoveryCluster(t *testing
23882408
}
23892409
})
23902410

2391-
t.Run("freshly-recovered cluster from this restore is not re-purged", func(t *testing.T) {
2411+
t.Run("unmarked cluster this restore's values rendered is not re-purged", func(t *testing.T) {
23922412
backup := mkBackupArtifact(t)
2393-
// creationTimestamp a minute after StartedAt: this restore's own re-render.
2413+
// creationTimestamp a minute after StartedAt: this restore's own
2414+
// re-render by a chart that does not annotate the Cluster.
23942415
fresh := metav1.NewTime(startedAt.Add(time.Minute))
2395-
c := newCNPGStrategyTestClient(t, backup, mkRestoreJob(), strategy, cnpgBackup,
2396-
newPostgresApp(appName, ns), mkRecoveryCluster(fresh))
2416+
rjSeed := mkRestoreJob()
2417+
app := newPostgresApp(appName, ns)
2418+
app.Spec.Bootstrap.NewServerName = restoredServerName(clusterName, rjSeed.UID)
2419+
c := newCNPGStrategyTestClient(t, backup, rjSeed, strategy, cnpgBackup,
2420+
app, mkRecoveryCluster(fresh))
23972421
r := &RestoreJobReconciler{Client: c, Interface: dynamicfake.NewSimpleDynamicClient(testCNPGScheme(t)), Recorder: record.NewFakeRecorder(10)}
23982422

23992423
rj := &backupsv1alpha1.RestoreJob{}
@@ -2416,6 +2440,33 @@ func TestReconcileCNPGRestore_RepeatInPlacePurgesStaleRecoveryCluster(t *testing
24162440
}
24172441
})
24182442

2443+
// A manually configured recovery (bootstrap.enabled without
2444+
// newServerName) renders no annotation. Re-rendered after StartedAt but
2445+
// before this restore patches the values, it holds the manual recovery
2446+
// source, not this restore's backup.
2447+
t.Run("unmarked cluster from values this restore has not patched is purged even when created after start", func(t *testing.T) {
2448+
backup := mkBackupArtifact(t)
2449+
fresh := metav1.NewTime(startedAt.Add(time.Minute))
2450+
app := newPostgresApp(appName, ns)
2451+
app.Spec.Bootstrap.Enabled = true
2452+
app.Spec.Bootstrap.OldName = "manual-source"
2453+
c := newCNPGStrategyTestClient(t, backup, mkRestoreJob(), strategy, cnpgBackup,
2454+
app, mkRecoveryCluster(fresh), mkClusterPVC())
2455+
r := &RestoreJobReconciler{Client: c, Interface: dynamicfake.NewSimpleDynamicClient(testCNPGScheme(t)), Recorder: record.NewFakeRecorder(10)}
2456+
2457+
rj := &backupsv1alpha1.RestoreJob{}
2458+
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: "rj"}, rj); err != nil {
2459+
t.Fatalf("get seeded RestoreJob: %v", err)
2460+
}
2461+
if _, err := r.reconcileCNPGRestore(ctx, rj, backup); err != nil {
2462+
t.Fatalf("reconcileCNPGRestore: %v", err)
2463+
}
2464+
err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: clusterName}, &cnpgtypes.Cluster{})
2465+
if !apierrors.IsNotFound(err) {
2466+
t.Fatalf("expected the manual recovery Cluster to be purged (NotFound), got err=%v", err)
2467+
}
2468+
})
2469+
24192470
t.Run("recovery cluster marked by another restore is purged even when created after start", func(t *testing.T) {
24202471
backup := mkBackupArtifact(t)
24212472
fresh := metav1.NewTime(startedAt.Add(time.Minute))

0 commit comments

Comments
 (0)