Skip to content

Commit 6a75883

Browse files
androndolexfrei
authored andcommitted
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 35ca217 commit 6a75883

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
@@ -541,22 +541,24 @@ func cnpgPurgeNeeded(purgedCondition, liveClusterFreshlyRecovered bool) bool {
541541
// restore that rendered the Cluster. When the annotation is present, the
542542
// Cluster is fresh only if it carries wantMark.
543543
//
544-
// A Cluster rendered by a chart that predates the annotation has none; it then
545-
// falls back to the creation-time comparison (created strictly after
546-
// StartedAt), which still separates a Cluster from an earlier completed restore
547-
// (created before StartedAt) from this restore's own re-render.
544+
// A Cluster without the annotation was rendered either by a chart that
545+
// predates it or from values with no bootstrap.newServerName (a manually
546+
// configured recovery). It is fresh only if appMark, the application's current
547+
// bootstrap.newServerName, is wantMark - this restore has patched the values -
548+
// and it was created strictly after StartedAt, which separates a Cluster from
549+
// an earlier completed restore from this restore's own re-render.
548550
//
549551
// Returns false when freshness cannot be determined (no recovery bootstrap, or
550552
// a missing timestamp on the fallback path): the caller then purges, which is
551553
// the safe default.
552-
func cnpgClusterFreshlyRecovered(state recoveryClusterState, wantMark string, restoreStartedAt *metav1.Time) bool {
554+
func cnpgClusterFreshlyRecovered(state recoveryClusterState, wantMark, appMark string, restoreStartedAt *metav1.Time) bool {
553555
if !state.hasRecovery {
554556
return false
555557
}
556558
if state.restoreMark != "" {
557559
return state.restoreMark == wantMark
558560
}
559-
if state.createdAt == nil || restoreStartedAt == nil {
561+
if appMark != wantMark || state.createdAt == nil || restoreStartedAt == nil {
560562
return false
561563
}
562564
return state.createdAt.After(restoreStartedAt.Time)
@@ -1035,7 +1037,7 @@ func (r *RestoreJobReconciler) reconcileCNPGRestore(ctx context.Context, restore
10351037
// produced. A recovery Cluster rendered for any other restore still holds
10361038
// that restore's data, so it must be purged - otherwise a repeat in-place
10371039
// restore silently no-ops.
1038-
freshlyRecovered := cnpgClusterFreshlyRecovered(clusterState, restoredServerName(clusterName, restoreJob.UID), restoreJob.Status.StartedAt)
1040+
freshlyRecovered := cnpgClusterFreshlyRecovered(clusterState, restoredServerName(clusterName, restoreJob.UID), targetApp.Spec.Bootstrap.NewServerName, restoreJob.Status.StartedAt)
10391041
if cnpgPurgeNeeded(purgedCondition, freshlyRecovered) {
10401042
// A physical restore only opens on the major version that wrote the
10411043
// 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
}
@@ -2751,12 +2771,16 @@ func TestReconcileCNPGRestore_RepeatInPlacePurgesStaleRecoveryCluster(t *testing
27512771
}
27522772
})
27532773

2754-
t.Run("freshly-recovered cluster from this restore is not re-purged", func(t *testing.T) {
2774+
t.Run("unmarked cluster this restore's values rendered is not re-purged", func(t *testing.T) {
27552775
backup := mkBackupArtifact(t)
2756-
// creationTimestamp a minute after StartedAt: this restore's own re-render.
2776+
// creationTimestamp a minute after StartedAt: this restore's own
2777+
// re-render by a chart that does not annotate the Cluster.
27572778
fresh := metav1.NewTime(startedAt.Add(time.Minute))
2758-
c := newCNPGStrategyTestClient(t, backup, mkRestoreJob(), strategy, cnpgBackup,
2759-
newPostgresApp(appName, ns), mkRecoveryCluster(fresh))
2779+
rjSeed := mkRestoreJob()
2780+
app := newPostgresApp(appName, ns)
2781+
app.Spec.Bootstrap.NewServerName = restoredServerName(clusterName, rjSeed.UID)
2782+
c := newCNPGStrategyTestClient(t, backup, rjSeed, strategy, cnpgBackup,
2783+
app, mkRecoveryCluster(fresh))
27602784
r := &RestoreJobReconciler{Client: c, Interface: dynamicfake.NewSimpleDynamicClient(testCNPGScheme(t)), Recorder: record.NewFakeRecorder(10)}
27612785

27622786
rj := &backupsv1alpha1.RestoreJob{}
@@ -2779,6 +2803,33 @@ func TestReconcileCNPGRestore_RepeatInPlacePurgesStaleRecoveryCluster(t *testing
27792803
}
27802804
})
27812805

2806+
// A manually configured recovery (bootstrap.enabled without
2807+
// newServerName) renders no annotation. Re-rendered after StartedAt but
2808+
// before this restore patches the values, it holds the manual recovery
2809+
// source, not this restore's backup.
2810+
t.Run("unmarked cluster from values this restore has not patched is purged even when created after start", func(t *testing.T) {
2811+
backup := mkBackupArtifact(t)
2812+
fresh := metav1.NewTime(startedAt.Add(time.Minute))
2813+
app := newPostgresApp(appName, ns)
2814+
app.Spec.Bootstrap.Enabled = true
2815+
app.Spec.Bootstrap.OldName = "manual-source"
2816+
c := newCNPGStrategyTestClient(t, backup, mkRestoreJob(), strategy, cnpgBackup,
2817+
app, mkRecoveryCluster(fresh), mkClusterPVC())
2818+
r := &RestoreJobReconciler{Client: c, Interface: dynamicfake.NewSimpleDynamicClient(testCNPGScheme(t)), Recorder: record.NewFakeRecorder(10)}
2819+
2820+
rj := &backupsv1alpha1.RestoreJob{}
2821+
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: "rj"}, rj); err != nil {
2822+
t.Fatalf("get seeded RestoreJob: %v", err)
2823+
}
2824+
if _, err := r.reconcileCNPGRestore(ctx, rj, backup); err != nil {
2825+
t.Fatalf("reconcileCNPGRestore: %v", err)
2826+
}
2827+
err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: clusterName}, &cnpgtypes.Cluster{})
2828+
if !apierrors.IsNotFound(err) {
2829+
t.Fatalf("expected the manual recovery Cluster to be purged (NotFound), got err=%v", err)
2830+
}
2831+
})
2832+
27822833
t.Run("recovery cluster marked by another restore is purged even when created after start", func(t *testing.T) {
27832834
backup := mkBackupArtifact(t)
27842835
fresh := metav1.NewTime(startedAt.Add(time.Minute))

0 commit comments

Comments
 (0)