Skip to content

Commit c396e03

Browse files
mantissahzmergify[bot]
authored andcommitted
feat(backup): delete snapshot after backup completed
acoording to the global setting 'auto-cleanup-snapshot-after-on-demand-backup-completed' in the backup controller. ref: longhorn/longhorn 9213 Signed-off-by: James Lu <[email protected]>
1 parent 5609e98 commit c396e03

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

controller/backup_controller.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,27 @@ func (bc *BackupController) reconcile(backupName string) (err error) {
499499
return nil
500500
}
501501

502+
defer func() {
503+
// If the backup is in the final state, delete the snapshot if needed
504+
if _, ok := backupInfo.Labels[types.RecurringJobLabel]; ok {
505+
// leave the snapshot management for recurring jobs to the `SettingNameAutoCleanupRecurringJobBackupSnapshot` setting.
506+
return
507+
}
508+
509+
cleanupSnap, err := bc.ds.GetSettingAsBool(types.SettingNameAutoCleanupSnapshotAfterOnDemandBackupCompleted)
510+
if err != nil {
511+
log.WithError(err).Errorf("Failed to get the setting %v", types.SettingNameAutoCleanupSnapshotAfterOnDemandBackupCompleted)
512+
return
513+
}
514+
if cleanupSnap && backup.Spec.SnapshotName != "" {
515+
if errDel := bc.ds.DeleteSnapshot(backup.Spec.SnapshotName); errDel != nil && !apierrors.IsNotFound(errDel) {
516+
err = errDel
517+
log.WithError(err).Error("Failed to delete the snapshot")
518+
return
519+
}
520+
}
521+
}()
522+
502523
// Remove the Backup Volume recurring jobs/groups information.
503524
// Only record the latest recurring jobs/groups information in backup volume CR and volume.cfg on remote backup target.
504525
delete(backupInfo.Labels, types.VolumeRecurringJobInfoLabel)
@@ -521,7 +542,7 @@ func (bc *BackupController) reconcile(backupName string) (err error) {
521542
backup.Status.LastSyncedAt = syncTime
522543
backup.Status.NewlyUploadedDataSize = backupInfo.NewlyUploadedDataSize
523544
backup.Status.ReUploadedDataSize = backupInfo.ReUploadedDataSize
524-
return nil
545+
return err
525546
}
526547

527548
// handleAttachmentTicketDeletion check and delete attachment so that the source volume is detached if needed

0 commit comments

Comments
 (0)