Skip to content

Commit 1093f39

Browse files
committed
feat(backups): warn on the BackupJob when the Redis snapshot is empty
A valid RDB snapshot always carries a header, so a 0-byte object means the backup is broken. sizeBytes is omitempty, though, so such a Backup read exactly like one whose size was never measured. The controller still records the artifact, and now raises a Warning EmptySnapshot Event on the BackupJob once the Backup is created. A test now also pins that a retry over an existing Backup returns it without calling Create. Assisted-by: LLM Signed-off-by: Andrey Kolkov <[email protected]>
1 parent 2013e62 commit 1093f39

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎internal/backupcontroller/redis_artifact_size_test.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/client-go/tools/record"
1818
"sigs.k8s.io/controller-runtime/pkg/client"
1919
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
20+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2021

2122
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
2223
)
@@ -367,16 +368,39 @@ func TestCreateRedisBackupArtifact_Artifact(t *testing.T) {
367368
}
368369
})
369370

370-
t.Run("an existing Backup is returned without probing", func(t *testing.T) {
371+
t.Run("an existing Backup is returned without probing or creating", func(t *testing.T) {
371372
existing := &backupsv1alpha1.Backup{ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-1"}}
372373
l := &fakeS3Lister{statSize: 777}
373374
r := redisReconciler(t, l, j, existing, projectedCredsSecret(nil))
375+
creates := 0
376+
r.Client = interceptor.NewClient(r.Client.(client.WithWatch), interceptor.Funcs{
377+
Create: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
378+
creates++
379+
return cl.Create(ctx, obj, opts...)
380+
},
381+
})
374382
if _, err := r.createRedisBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, redisTestKey, nil); err != nil {
375383
t.Fatalf("createRedisBackupArtifact: %v", err)
376384
}
377385
if l.calls != 0 {
378386
t.Errorf("S3 probed %d times for a Backup that already exists", l.calls)
379387
}
388+
if creates != 0 {
389+
t.Errorf("Create called %d times for a Backup that already exists", creates)
390+
}
391+
})
392+
393+
t.Run("an empty snapshot is recorded and warned about", func(t *testing.T) {
394+
r := redisReconciler(t, &fakeS3Lister{statSize: 0}, j, projectedCredsSecret(nil))
395+
rec := record.NewFakeRecorder(4)
396+
r.Recorder = rec
397+
got, err := r.createRedisBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, redisTestKey, nil)
398+
if err != nil || got == nil || got.Status.Artifact == nil || got.Status.Artifact.URI != "s3://cozy-backups/"+redisTestKey {
399+
t.Fatalf("createRedisBackupArtifact = %#v, %v; want the artifact recorded", got, err)
400+
}
401+
if ev := nextEvent(rec); !strings.HasPrefix(ev, "Warning EmptySnapshot ") {
402+
t.Errorf("event = %q, want Warning EmptySnapshot", ev)
403+
}
380404
})
381405
}
382406

‎internal/backupcontroller/redisstrategy_controller.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,15 @@ func (r *BackupJobReconciler) createRedisBackupArtifact(
526526
}
527527
// Only once the Backup exists: a terminal Create error fails the
528528
// BackupJob, and a collision returns a Backup another pass measured.
529-
if unmeasured != "" && r.Recorder != nil {
530-
r.Recorder.Eventf(j, corev1.EventTypeNormal, "ArtifactNotMeasured", "The backup succeeded, but its location and size were not recorded: %s", unmeasured)
529+
if r.Recorder != nil {
530+
switch {
531+
case unmeasured != "":
532+
r.Recorder.Eventf(j, corev1.EventTypeNormal, "ArtifactNotMeasured", "The backup succeeded, but its location and size were not recorded: %s", unmeasured)
533+
case backup.Status.Artifact != nil && backup.Status.Artifact.SizeBytes == 0:
534+
// sizeBytes is omitempty, so a 0 reads as "not measured" on the
535+
// Backup; a valid RDB snapshot always carries a header.
536+
r.Recorder.Event(j, corev1.EventTypeWarning, "EmptySnapshot", "The backup's RDB snapshot is empty (0 bytes); a valid snapshot is never empty, so this backup is likely broken.")
537+
}
531538
}
532539
return backup, nil
533540
}

0 commit comments

Comments
 (0)