diff --git a/docs/api-references/docs.md b/docs/api-references/docs.md index d250dcfada6..1cb235af5b4 100644 --- a/docs/api-references/docs.md +++ b/docs/api-references/docs.md @@ -275,7 +275,7 @@ CleanPolicyType -

CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster

+

CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster, if not set, the backup data will be retained

@@ -2590,7 +2590,7 @@ CleanPolicyType -

CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster

+

CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster, if not set, the backup data will be retained

diff --git a/pkg/apis/pingcap/v1alpha1/backup.go b/pkg/apis/pingcap/v1alpha1/backup.go index 86f37258841..ac8da4d24e3 100644 --- a/pkg/apis/pingcap/v1alpha1/backup.go +++ b/pkg/apis/pingcap/v1alpha1/backup.go @@ -122,8 +122,8 @@ func IsBackupClean(backup *Backup) bool { return condition != nil && condition.Status == corev1.ConditionTrue } -// ShouldCleanData returns true if a Backup should be cleaned up according to cleanPolicy -func ShouldCleanData(backup *Backup) bool { +// IsCleanCandidate returns true if a Backup should be added to clean candidate according to cleanPolicy +func IsCleanCandidate(backup *Backup) bool { switch backup.Spec.CleanPolicy { case CleanPolicyTypeDelete, CleanPolicyTypeOnFailure: return true @@ -131,3 +131,8 @@ func ShouldCleanData(backup *Backup) bool { return false } } + +// NeedNotClean returns true if a Backup need not to be cleaned up according to cleanPolicy +func NeedNotClean(backup *Backup) bool { + return backup.Spec.CleanPolicy == CleanPolicyTypeOnFailure && !IsBackupFailed(backup) +} diff --git a/pkg/apis/pingcap/v1alpha1/openapi_generated.go b/pkg/apis/pingcap/v1alpha1/openapi_generated.go index a01b158b731..62d48fedbac 100644 --- a/pkg/apis/pingcap/v1alpha1/openapi_generated.go +++ b/pkg/apis/pingcap/v1alpha1/openapi_generated.go @@ -858,7 +858,7 @@ func schema_pkg_apis_pingcap_v1alpha1_BackupSpec(ref common.ReferenceCallback) c }, "cleanPolicy": { SchemaProps: spec.SchemaProps{ - Description: "CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster", + Description: "CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster, if not set, the backup data will be retained", Type: []string{"string"}, Format: "", }, diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 65fc6a742b5..6b132d6bbda 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -1161,7 +1161,7 @@ type BackupSpec struct { UseKMS bool `json:"useKMS,omitempty"` // Specify service account of backup ServiceAccount string `json:"serviceAccount,omitempty"` - // CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster + // CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster, if not set, the backup data will be retained CleanPolicy CleanPolicyType `json:"cleanPolicy,omitempty"` } diff --git a/pkg/backup/backup/backup_cleaner.go b/pkg/backup/backup/backup_cleaner.go index f0101947546..a872c09e311 100644 --- a/pkg/backup/backup/backup_cleaner.go +++ b/pkg/backup/backup/backup_cleaner.go @@ -57,8 +57,7 @@ func NewBackupCleaner( } func (bc *backupCleaner) Clean(backup *v1alpha1.Backup) error { - if backup.DeletionTimestamp == nil || !v1alpha1.ShouldCleanData(backup) || - backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup) { + if backup.DeletionTimestamp == nil || !v1alpha1.IsCleanCandidate(backup) || v1alpha1.NeedNotClean(backup) { // The backup object has not been deleted or we need to retain backup dataļ¼Œdo nothing return nil } diff --git a/pkg/controller/backup/backup_control.go b/pkg/controller/backup/backup_control.go index 46968889d4f..d97b050fdab 100644 --- a/pkg/controller/backup/backup_control.go +++ b/pkg/controller/backup/backup_control.go @@ -86,8 +86,7 @@ func (bc *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backu ns := backup.GetNamespace() name := backup.GetName() - if v1alpha1.ShouldCleanData(backup) && isDeletionCandidate(backup) && - (v1alpha1.IsBackupClean(backup) || (backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup))) { + if needToRemoveFinalizer(backup) { backup.Finalizers = slice.RemoveString(backup.Finalizers, label.BackupProtectionFinalizer, nil) _, err := bc.cli.PingcapV1alpha1().Backups(ns).Update(backup) if err != nil { @@ -99,7 +98,12 @@ func (bc *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backu } func needToAddFinalizer(backup *v1alpha1.Backup) bool { - return backup.DeletionTimestamp == nil && v1alpha1.ShouldCleanData(backup) && !slice.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil) + return backup.DeletionTimestamp == nil && v1alpha1.IsCleanCandidate(backup) && !slice.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil) +} + +func needToRemoveFinalizer(backup *v1alpha1.Backup) bool { + return v1alpha1.IsCleanCandidate(backup) && isDeletionCandidate(backup) && + (v1alpha1.IsBackupClean(backup) || v1alpha1.NeedNotClean(backup)) } func isDeletionCandidate(backup *v1alpha1.Backup) bool {