Skip to content

Commit cf988bf

Browse files
committed
Compare backup completion time to time in pvc annotation.
1 parent 29c5473 commit cf988bf

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

internal/controller/postgrescluster/snapshots.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,21 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
219219
return pvc, nil
220220
}
221221

222-
// If pvc has been annotated with latest backup job's completion time, then restore was
223-
// successful and there's nothing left to do.
224-
backupJobCompletionTime := backupJob.Status.CompletionTime.String()
225-
// TODO: change this to check whether backupJobCompletionTime is more recent than pvc annotation.
226-
if pvc.GetAnnotations()[naming.PGBackRestBackupJobCompletion] == backupJobCompletionTime {
227-
return pvc, nil
222+
// If the pvc has not been annotated, it has not been restored and we want to proceed
223+
// with a restore.
224+
pvcAnnotationTimestampString, pvcAnnotationExists := pvc.GetAnnotations()[naming.PGBackRestBackupJobCompletion]
225+
if pvcAnnotationExists {
226+
// If pvc has been annotated, we want to compare its timestamp to the latest backup job's
227+
// timestamp. If the pvc's timestamp is older than the latest backup job's
228+
// completion time, then we want to do a restore.
229+
backupJobCompletionTime := backupJob.Status.CompletionTime.Time
230+
pvcAnnotationTime, err := time.Parse(time.RFC3339, pvcAnnotationTimestampString)
231+
if err != nil {
232+
return pvc, err
233+
}
234+
if backupJobCompletionTime.Compare(pvcAnnotationTime) <= 0 {
235+
return pvc, nil
236+
}
228237
}
229238

230239
// If we've made it here, the pvc has not been restored with latest backup.
@@ -409,7 +418,7 @@ func (r *Reconciler) dedicatedSnapshotVolumeRestore(ctx context.Context,
409418

410419
addTMPEmptyDir(&restoreJob.Spec.Template)
411420

412-
restoreJob.Annotations[naming.PGBackRestBackupJobCompletion] = backupJob.Status.CompletionTime.String()
421+
restoreJob.Annotations[naming.PGBackRestBackupJobCompletion] = backupJob.Status.CompletionTime.Format(time.RFC3339)
413422
return errors.WithStack(r.apply(ctx, restoreJob))
414423
}
415424

internal/controller/postgrescluster/snapshots_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package postgrescluster
77
import (
88
"context"
99
"testing"
10+
"time"
1011

1112
"github.com/pkg/errors"
1213
"gotest.tools/v3/assert"
@@ -561,7 +562,7 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
561562
// Create successful restore job
562563
restoreJob := testRestoreJob(cluster)
563564
restoreJob.Annotations = map[string]string{
564-
naming.PGBackRestBackupJobCompletion: backupJob.Status.CompletionTime.String(),
565+
naming.PGBackRestBackupJobCompletion: backupJob.Status.CompletionTime.Format(time.RFC3339),
565566
}
566567
err = errors.WithStack(r.setControllerReference(cluster, restoreJob))
567568
assert.NilError(t, err)
@@ -603,7 +604,7 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
603604
assert.Equal(t, len(restoreJobs.Items), 0)
604605

605606
// Assert pvc was annotated
606-
assert.Equal(t, pvc.GetAnnotations()[naming.PGBackRestBackupJobCompletion], backupJob.Status.CompletionTime.String())
607+
assert.Equal(t, pvc.GetAnnotations()[naming.PGBackRestBackupJobCompletion], backupJob.Status.CompletionTime.Format(time.RFC3339))
607608
})
608609

609610
t.Run("SnapshotsEnabledFailedRestoreExists", func(t *testing.T) {
@@ -646,7 +647,7 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
646647
// Create failed restore job
647648
restoreJob := testRestoreJob(cluster)
648649
restoreJob.Annotations = map[string]string{
649-
naming.PGBackRestBackupJobCompletion: backupJob.Status.CompletionTime.String(),
650+
naming.PGBackRestBackupJobCompletion: backupJob.Status.CompletionTime.Format(time.RFC3339),
650651
}
651652
err = errors.WithStack(r.setControllerReference(cluster, restoreJob))
652653
assert.NilError(t, err)
@@ -758,7 +759,7 @@ func TestDedicatedSnapshotVolumeRestore(t *testing.T) {
758759
assert.NilError(t, err)
759760
assert.Equal(t, len(jobs.Items), 1)
760761
assert.Equal(t, jobs.Items[0].Annotations[naming.PGBackRestBackupJobCompletion],
761-
backupJob.Status.CompletionTime.String())
762+
backupJob.Status.CompletionTime.Format(time.RFC3339))
762763
}
763764

764765
func TestGenerateSnapshotOfDedicatedSnapshotVolume(t *testing.T) {

internal/naming/annotations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ const (
2323

2424
// PGBackRestBackupJobCompletion is the annotation that is added to restore jobs, pvcs, and
2525
// VolumeSnapshots that are involved in the volume snapshot creation process. The annotation
26-
// holds a timestamp that corresponds to the completion time of the associated backup job. A
27-
// backup is always taken right before a delta restore is performed on the dedicated snapshot
28-
// volume, and then a VolumeSnapshot is taken of the dedicated volume. The value is a timestamp.
26+
// holds a RFC3339 formatted timestamp that corresponds to the completion time of the associated
27+
// backup job. A backup is always taken right before a delta restore is performed on the
28+
// dedicated snapshot volume, and then a VolumeSnapshot is taken of the dedicated volume.
2929
PGBackRestBackupJobCompletion = annotationPrefix + "pgbackrest-backup-job-completion"
3030

3131
// PGBackRestConfigHash is an annotation used to specify the hash value associated with a

0 commit comments

Comments
 (0)