From 8d92aee796edaa1d4347b964e1d06de9d8fef655 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 9 Nov 2026 14:40:44 +0300 Subject: [PATCH] test(postgres): pin that an older attach record keeps the latch The rollout check takes the later of the latch and the Cluster's attach record. A record left by an earlier attach of a plugin since removed is older than the latch when someone who keeps no record attaches the plugin again, and taking it would accept an instance recreated before the latch as one that rolled with the plugin. The record is absent not only when no backup job attached the plugin but also when the chart did, or a controller that kept no record, so the comment on the field says that. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../cnpgstrategy_controller.go | 3 +- .../cnpgstrategy_controller_test.go | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/internal/backupcontroller/cnpgstrategy_controller.go b/internal/backupcontroller/cnpgstrategy_controller.go index 5869103ca..37597678d 100644 --- a/internal/backupcontroller/cnpgstrategy_controller.go +++ b/internal/backupcontroller/cnpgstrategy_controller.go @@ -835,7 +835,8 @@ type cnpgPluginAttachment struct { // missing reports a Cluster that carried no barman-cloud plugin. missing bool // attachedAt is the cnpgPluginAttachedAtAnnotation the Cluster carried, - // "" when no backup job attached the plugin. + // "" when it carries no record: the plugin was attached by the chart, or + // before the record existed. attachedAt string clusterStatus cnpgtypes.ClusterStatus } diff --git a/internal/backupcontroller/cnpgstrategy_controller_test.go b/internal/backupcontroller/cnpgstrategy_controller_test.go index 255be9a27..b9a10b56e 100644 --- a/internal/backupcontroller/cnpgstrategy_controller_test.go +++ b/internal/backupcontroller/cnpgstrategy_controller_test.go @@ -2985,6 +2985,44 @@ func TestReconcileCNPG_LostAttachRecordStillWaitsForTheRollout(t *testing.T) { } } +// A record older than the latch is left from an earlier attach of a plugin +// since removed; the plugin found now was attached by someone who keeps no +// record, after the latch, so an instance recreated between the record and +// the latch still has to roll. +func TestReconcileCNPG_AttachRecordOlderThanTheLatchKeepsTheLatch(t *testing.T) { + latchedAt := time.Now().Add(-10 * time.Minute).Truncate(time.Second) + oldPod := newRolloutInstancePod(latchedAt.Add(-2 * time.Hour)) + f := newCNPGRolloutFixture(t, newRolloutBackupJob(latchedAt.Add(-2*time.Hour)), + newRolloutCluster(cnpgClusterHealthyPhase), oldPod) + + j, _ := f.pass() // latch + latch := apimeta.FindStatusCondition(j.Status.Conditions, backupCondPluginRolledOut) + if latch == nil { + t.Fatalf("no %s latch: %+v", backupCondPluginRolledOut, j.Status.Conditions) + } + latch.LastTransitionTime = metav1.NewTime(latchedAt) + if err := f.c.Status().Update(context.Background(), j); err != nil { + t.Fatalf("age the latch: %v", err) + } + f.replacePod(oldPod, newRolloutInstancePod(latchedAt.Add(-time.Minute))) + live := newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, rolloutCluster)) + live.Annotations = map[string]string{cnpgPluginAttachedAtAnnotation: latchedAt.Add(-time.Hour).UTC().Format(time.RFC3339)} + f.observeCNPG(live) + + var backups []cnpgtypes.Backup + for range 3 { + if j, backups = f.pass(); len(backups) != 0 || j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed { + break + } + } + if len(backups) != 0 { + t.Fatalf("a cnpg.io/Backup was created against an instance recreated before the latch: %+v", backups) + } + if j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed { + t.Fatalf("the job failed instead of waiting for the rollout: %s", j.Status.Message) + } +} + // A later write to a Cluster that already carries the plugin re-sends the // attach record as it is: moving it would make the instances that rolled // with the plugin look older than the attach.