Skip to content

Commit

Permalink
feat: remove lh snapshots when users delete backup
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <[email protected]>
  • Loading branch information
FrankYang0529 authored and innobead committed Jun 24, 2024
1 parent b191610 commit 83cd31c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions controller/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ func (bc *BackupController) reconcile(backupName string) (err error) {
bc.eventRecorder.Eventf(backup, corev1.EventTypeWarning, string(backup.Status.State), "Failed backup %s has been deleted: %s", backup.Name, backup.Status.Error)
}

autocleanup, err := bc.ds.GetSettingAsBool(types.SettingNameAutoCleanupSnapshotWhenDeleteBackup)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"name": types.SettingNameAutoCleanupSnapshotWhenDeleteBackup,
}).Warn("Failed to get the setting")
}
if autocleanup {
// do the best effort to delete the snapshot
snapshot, err := bc.ds.GetSnapshotRO(backup.Spec.SnapshotName)
if err != nil {
if !apierrors.IsNotFound(err) {
logrus.WithError(err).WithFields(logrus.Fields{
"backup": backup.Name,
"snapshot": snapshot.Name,
}).Warn("Failed to get snapshot")
}
return nil
}
if err = bc.ds.DeleteSnapshot(snapshot.Name); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"backup": backup.Name,
"snapshot": snapshot.Name,
}).Warn("Failed to delete snapshot")
}
}
return bc.ds.RemoveFinalizerForBackup(backup)
}

Expand Down
13 changes: 13 additions & 0 deletions types/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const (
SettingNameV2DataEngineLogLevel = SettingName("v2-data-engine-log-level")
SettingNameV2DataEngineLogFlags = SettingName("v2-data-engine-log-flags")
SettingNameFreezeFilesystemForSnapshot = SettingName("freeze-filesystem-for-snapshot")
SettingNameAutoCleanupSnapshotWhenDeleteBackup = SettingName("auto-cleanup-when-delete-backup")
)

var (
Expand Down Expand Up @@ -218,6 +219,7 @@ var (
SettingNameAllowEmptyDiskSelectorVolume,
SettingNameDisableSnapshotPurge,
SettingNameFreezeFilesystemForSnapshot,
SettingNameAutoCleanupSnapshotWhenDeleteBackup,
}
)

Expand Down Expand Up @@ -333,6 +335,7 @@ var (
SettingNameAllowEmptyDiskSelectorVolume: SettingDefinitionAllowEmptyDiskSelectorVolume,
SettingNameDisableSnapshotPurge: SettingDefinitionDisableSnapshotPurge,
SettingNameFreezeFilesystemForSnapshot: SettingDefinitionFreezeFilesystemForSnapshot,
SettingNameAutoCleanupSnapshotWhenDeleteBackup: SettingDefinitionAutoCleanupSnapshotWhenDeleteBackup,
}

SettingDefinitionBackupTarget = SettingDefinition{
Expand Down Expand Up @@ -1399,6 +1402,16 @@ var (
ReadOnly: false,
Default: "",
}

SettingDefinitionAutoCleanupSnapshotWhenDeleteBackup = SettingDefinition{
DisplayName: "Automatically Cleanup Snapshot When Deleting Backup",
Description: "This setting enables Longhorn to automatically cleanup snapshots when removing backup.",
Category: SettingCategorySnapshot,
Type: SettingTypeBool,
Required: true,
ReadOnly: false,
Default: "false",
}
)

type NodeDownPodDeletionPolicy string
Expand Down

0 comments on commit 83cd31c

Please sign in to comment.