Skip to content

Commit

Permalink
add finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
RidRisR committed Sep 24, 2024
1 parent 921957a commit 5ed8bd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/pingcap/v1alpha1/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ func IsBackupCleanFailed(backup *Backup) bool {

// IsCleanCandidate returns true if a Backup should be added to clean candidate according to cleanPolicy
func IsCleanCandidate(backup *Backup) bool {
if backup.Spec.Mode == BackupModeLog {
return true
}
switch backup.Spec.CleanPolicy {
case CleanPolicyTypeDelete, CleanPolicyTypeOnFailure:
return true
Expand Down
3 changes: 3 additions & 0 deletions pkg/backup/backup/backup_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func NewBackupCleaner(deps *controller.Dependencies, statusUpdater controller.Ba
}

func (bc *backupCleaner) StopLogBackup(backup *v1alpha1.Backup) error {
if backup.Spec.Mode != v1alpha1.BackupModeLog {
return nil
}
if !v1alpha1.IsLogBackupAlreadyStart(backup) {
return nil
}
Expand Down
23 changes: 16 additions & 7 deletions pkg/controller/backup/backup_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ func (c *defaultBackupControl) UpdateBackup(backup *v1alpha1.Backup) error {
return err
}

if err := c.removeProtectionFinalizer(backup); err != nil {
stopLogBackup := func () error {
// for log backup, we need stop the log backup before clean up
if backup.Spec.Mode == v1alpha1.BackupModeLog {
if err := c.backupManager.StopLogBackup(backup); err != nil {
return err
}
}
return nil
}

if err := c.removeProtectionFinalizer(backup,stopLogBackup); err != nil {
return err
}

Expand Down Expand Up @@ -93,17 +103,16 @@ func (c *defaultBackupControl) addProtectionFinalizer(backup *v1alpha1.Backup) e
return nil
}

func (c *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backup) error {
func (c *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backup, preDelete func() error) error {
ns := backup.GetNamespace()
name := backup.GetName()

if needToRemoveFinalizer(backup) {
// for log backup, we need stop the log backup before clean up
if backup.Spec.Mode == v1alpha1.BackupModeLog {
if err := c.backupManager.StopLogBackup(backup); err != nil {
return err
if preDelete != nil {
if err := preDelete(); err != nil {
return fmt.Errorf("stop log backup for backup %s/%s failed in deleting, err: %v", ns, name, err)
}
}
}
backup.Finalizers = k8s.RemoveString(backup.Finalizers, label.BackupProtectionFinalizer, nil)
_, err := c.cli.PingcapV1alpha1().Backups(ns).Update(context.TODO(), backup, metav1.UpdateOptions{})
if err != nil {
Expand Down

0 comments on commit 5ed8bd7

Please sign in to comment.