diff --git a/internal/backupcontroller/cnpgstrategy_controller.go b/internal/backupcontroller/cnpgstrategy_controller.go index 37597678d..ac58fa4bc 100644 --- a/internal/backupcontroller/cnpgstrategy_controller.go +++ b/internal/backupcontroller/cnpgstrategy_controller.go @@ -499,6 +499,10 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp attachedAt := rollout.LastTransitionTime if attachment.attachedAt != "" { recorded, err := time.Parse(time.RFC3339, attachment.attachedAt) + if recorded.After(time.Now()) { + // Every instance would predate it, and the deadline would count from it. + err = fmt.Errorf("%s is in the future", attachment.attachedAt) + } if err != nil { return r.markBackupJobFailed(ctx, j, fmt.Sprintf( "cannot tell when the barman-cloud plugin was attached: the %s annotation of cnpg.io/Cluster %s/%s: %v", diff --git a/internal/backupcontroller/cnpgstrategy_controller_test.go b/internal/backupcontroller/cnpgstrategy_controller_test.go index 4753dda68..2f116734b 100644 --- a/internal/backupcontroller/cnpgstrategy_controller_test.go +++ b/internal/backupcontroller/cnpgstrategy_controller_test.go @@ -3083,23 +3083,32 @@ func TestReconcileCNPG_LaterWriteKeepsTheAttachRecord(t *testing.T) { } // An attach record that does not parse cannot tell which instances rolled -// with the plugin, so the job fails naming it instead of guessing. +// with the plugin, so the job fails naming it instead of guessing. One in the +// future would hold every instance as older than the plugin and push the +// rollout deadline out by as far as it lies ahead, so it fails the same way. func TestReconcileCNPG_UnreadableAttachRecordFailsTheJob(t *testing.T) { - f := newCNPGRolloutFixture(t, newRolloutBackupJob(time.Now().Add(-time.Hour)), - newRolloutCluster(cnpgClusterHealthyPhase), newRolloutInstancePod(time.Now().Add(-time.Hour))) - if j, _ := f.pass(); apimeta.FindStatusCondition(j.Status.Conditions, backupCondPluginRolledOut) == nil { - t.Fatalf("no %s latch: %+v", backupCondPluginRolledOut, j.Status.Conditions) - } - live := newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, rolloutCluster)) - live.Annotations = map[string]string{cnpgPluginAttachedAtAnnotation: "yesterday"} - f.observeCNPG(live) + for _, tc := range []struct{ name, record string }{ + {name: "does not parse", record: "yesterday"}, + {name: "in the future", record: time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}, + } { + t.Run(tc.name, func(t *testing.T) { + f := newCNPGRolloutFixture(t, newRolloutBackupJob(time.Now().Add(-time.Hour)), + newRolloutCluster(cnpgClusterHealthyPhase), newRolloutInstancePod(time.Now().Add(-time.Hour))) + if j, _ := f.pass(); apimeta.FindStatusCondition(j.Status.Conditions, backupCondPluginRolledOut) == nil { + t.Fatalf("no %s latch: %+v", backupCondPluginRolledOut, j.Status.Conditions) + } + live := newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, rolloutCluster)) + live.Annotations = map[string]string{cnpgPluginAttachedAtAnnotation: tc.record} + f.observeCNPG(live) - j, backups := f.pass() - if len(backups) != 0 { - t.Fatalf("a cnpg.io/Backup was created with an unreadable attach record: %+v", backups) - } - if j.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed || !strings.Contains(j.Status.Message, cnpgPluginAttachedAtAnnotation) { - t.Errorf("phase %q, message %q; want Failed naming %s", j.Status.Phase, j.Status.Message, cnpgPluginAttachedAtAnnotation) + j, backups := f.pass() + if len(backups) != 0 { + t.Fatalf("a cnpg.io/Backup was created with an unreadable attach record: %+v", backups) + } + if j.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed || !strings.Contains(j.Status.Message, cnpgPluginAttachedAtAnnotation) { + t.Errorf("phase %q, message %q; want Failed naming %s", j.Status.Phase, j.Status.Message, cnpgPluginAttachedAtAnnotation) + } + }) } }