fix(postgres): fail a backup job whose plugin attach record is in the future

The rollout check takes the later of the latch and the Cluster's attach
record, and the record is never rewritten while the plugin stays
attached. A record that parses but lies in the future made every
instance look older than the plugin and counted the rollout deadline
from that time, so the job waited past the deadline by as far as the
record lay ahead, without end for one far enough out. Such a record
says no more about when the plugin was attached than one that does not
parse, and fails the job the same way.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov committed 2026-11-10 00:08:41 +03:00
1 parent fe03589d91
commit 8a631311a7
2 files changed
+28 -15

No files matched your search

@@ -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",
@@ -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)
}
})
}
}