Skip to content

Commit

Permalink
add interface
Browse files Browse the repository at this point in the history
  • Loading branch information
RidRisR committed Sep 20, 2024
1 parent 0307526 commit 921957a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type BackupManager interface {
Sync(backup *v1alpha1.Backup) error
// UpdateStatus updates the status for a Backup, include condition and status info.
UpdateStatus(backup *v1alpha1.Backup, condition *v1alpha1.BackupCondition, newStatus *controller.BackupUpdateStatus) error
// StopLogBackup stops the log backup, it will be called when the log backup is deleted.
StopLogBackup(backup *v1alpha1.Backup) error
}

// RestoreManager implements the logic for manage restore.
Expand Down
14 changes: 6 additions & 8 deletions pkg/backup/backup/backup_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var _ BackupCleaner = &backupCleaner{}
// BackupCleaner implements the logic for cleaning backup
type BackupCleaner interface {
Clean(backup *v1alpha1.Backup) error
StopLogBackup(backup *v1alpha1.Backup) error
}

type backupCleaner struct {
Expand All @@ -53,29 +54,26 @@ 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 backup.Spec.BR == nil {
return fmt.Errorf("backup %s/%s spec.BR shouldn't be nil", backup.GetNamespace(), backup.GetName())
}
if !v1alpha1.IsLogBackupAlreadyStart(backup) {
return nil
}
if v1alpha1.IsLogBackupAlreadyStop(backup) {
return nil
}
if backup.Spec.BR == nil {
return fmt.Errorf("backup %s/%s spec.BR shouldn't be nil", backup.GetNamespace(), backup.GetName())
}

ns := backup.GetNamespace()
name := backup.GetName()
backupJobName := backup.GetBackupJobName()

// make backup job
// make stop job
var err error
var job *batchv1.Job
var reason string
if job, reason, err = bc.makeStopLogBackupJob(backup); err != nil {
klog.Errorf("backup %s/%s create job %s failed, reason is %s, error %v.", ns, name, backupJobName, reason, err)
klog.Errorf("backup %s/%s stop job %s failed, reason is %s, error %v.", ns, name, backupJobName, reason, err)
return err
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/backup/backup/backup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (bm *backupManager) Sync(backup *v1alpha1.Backup) error {
return bm.syncBackupJob(backup)
}

func (bm *backupManager) StopLogBackup(backup *v1alpha1.Backup) error {
return bm.backupCleaner.StopLogBackup(backup)
}

// UpdateStatus updates the status for a Backup, include condition and status info.
func (bm *backupManager) UpdateStatus(backup *v1alpha1.Backup, condition *v1alpha1.BackupCondition, newStatus *controller.BackupUpdateStatus) error {
return bm.statusUpdater.Update(backup, condition, newStatus)
Expand Down Expand Up @@ -1292,4 +1296,9 @@ func (m *FakeBackupManager) UpdateStatus(_ *v1alpha1.Backup, _ *v1alpha1.BackupC
return nil
}

// StopLogBackup stops the log backup, it should only be used in cleanning up.
func (m *FakeBackupManager) StopLogBackup(_ *v1alpha1.Backup) error {
return nil
}

var _ backup.BackupManager = &FakeBackupManager{}
6 changes: 6 additions & 0 deletions pkg/controller/backup/backup_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (c *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backup
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
}
}
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 921957a

Please sign in to comment.