Skip to content

Commit cf0b759

Browse files
committed
Fix expire not running when target PVC is deleted before backup expires
Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
1 parent 4718877 commit cf0b759

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

internal/controller/mantlebackup_controller.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,32 +288,34 @@ func (r *MantleBackupReconciler) getSnapshotTarget(ctx context.Context, backup *
288288
return &snapshotTarget{&pvc, &pv, imageName, poolName}, ctrl.Result{}, nil
289289
}
290290

291-
// expire deletes the backup if it is already expired. Otherwise it schedules deletion.
292-
// Note that this function does not use requeue to scheduled deletion because the caller
293-
// will do other tasks after this function returns.
294-
func (r *MantleBackupReconciler) expire(ctx context.Context, backup *mantlev1.MantleBackup) error {
291+
// expire deletes the backup if expired, or schedules deletion via expireQueueCh if not yet.
292+
// Returns true when DeletionTimestamp was set, signaling the caller to stop further processing.
293+
func (r *MantleBackupReconciler) expire(ctx context.Context, backup *mantlev1.MantleBackup) (bool, error) {
295294
logger := log.FromContext(ctx)
296295
if backup.Status.CreatedAt.IsZero() {
297296
// the RBD snapshot has not be taken yet, do nothing.
298-
return nil
297+
return false, nil
299298
}
300299

301300
if v, ok := backup.Annotations[annotRetainIfExpired]; ok && v == "true" {
302301
// retain this backup.
303302
// If the annotation is deleted, reconciliation will run, so no need to schedule.
304-
return nil
303+
return false, nil
305304
}
306305

307306
expire, err := strfmt.ParseDuration(backup.Spec.Expire)
308307
if err != nil {
309-
return err
308+
return false, err
310309
}
311310
expireAt := backup.Status.CreatedAt.Add(expire)
312311
if time.Now().UTC().After(expireAt) {
313312
// already expired, delete it immediately.
314313
logger.Info("delete expired backup", "createdAt", backup.Status.CreatedAt, "expire", expire)
315314

316-
return r.Delete(ctx, backup)
315+
if err := r.Delete(ctx, backup); err != nil {
316+
return false, err
317+
}
318+
return true, nil
317319
}
318320

319321
// not expired yet. schedule deletion.
@@ -323,7 +325,7 @@ func (r *MantleBackupReconciler) expire(ctx context.Context, backup *mantlev1.Ma
323325
Object: backup,
324326
}
325327

326-
return nil
328+
return false, nil
327329
}
328330

329331
//+kubebuilder:rbac:groups=mantle.cybozu.io,resources=mantlebackups,verbs=get;list;watch;create;update;patch;delete
@@ -384,6 +386,10 @@ func (r *MantleBackupReconciler) reconcileLocalBackup(ctx context.Context, backu
384386
return ctrl.Result{}, nil
385387
}
386388

389+
if updated, err := r.expire(ctx, backup); updated || err != nil {
390+
return ctrl.Result{}, err
391+
}
392+
387393
target, result, err := r.getSnapshotTarget(ctx, backup)
388394
notFound := aerrors.IsNotFound(err)
389395
switch {
@@ -428,10 +434,6 @@ func (r *MantleBackupReconciler) reconcileLocalBackup(ctx context.Context, backu
428434
return ctrl.Result{}, nil
429435
}
430436

431-
if err := r.expire(ctx, backup); err != nil {
432-
return ctrl.Result{}, err
433-
}
434-
435437
if !backup.IsSnapshotCaptured() {
436438
if err := r.provisionRBDSnapshot(ctx, backup, target); err != nil {
437439
return ctrl.Result{}, err
@@ -499,6 +501,10 @@ func (r *MantleBackupReconciler) reconcileAsSecondary(ctx context.Context, backu
499501
return ctrl.Result{}, nil
500502
}
501503

504+
if updated, err := r.expire(ctx, backup); updated || err != nil {
505+
return ctrl.Result{}, err
506+
}
507+
502508
target, result, err := r.getSnapshotTarget(ctx, backup)
503509
notFound := aerrors.IsNotFound(err)
504510
switch {
@@ -518,10 +524,6 @@ func (r *MantleBackupReconciler) reconcileAsSecondary(ctx context.Context, backu
518524
return ctrl.Result{}, err
519525
}
520526

521-
if err := r.expire(ctx, backup); err != nil {
522-
return ctrl.Result{}, err
523-
}
524-
525527
if !backup.IsSnapshotCaptured() {
526528
result, err := r.startImport(ctx, backup, target)
527529
if err != nil || !result.IsZero() {

0 commit comments

Comments
 (0)