-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support cleanPolicy for backup CR #3002
Changes from 5 commits
26b1eb8
0bae349
aedf29b
f2d7cc7
69d2cad
31217f9
7d896d9
e484693
a6c7b80
fe9c627
d177a55
32d164f
4e1d38f
c5e77dc
6341abc
337934b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"fmt" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
|
||
"k8s.io/klog" | ||
|
||
|
@@ -67,14 +68,14 @@ func (bo *Options) cleanRemoteBackupData(bucket string, opts []string) error { | |
destBucket := util.NormalizeBucketURI(bucket) | ||
args := util.ConstructArgs(constants.RcloneConfigArg, opts, "deletefile", destBucket, "") | ||
output, err := exec.Command("rclone", args...).CombinedOutput() | ||
if err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
if code := exitError.ExitCode(); code == 3 || code == 4 { | ||
klog.Infof("cluster %s backup %s has already been deleted before", bo, bucket) | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("cluster %s, execute rclone deletefile command failed, output: %s, err: %v", bo, string(output), err) | ||
if err != nil && !strings.Contains(string(output), "doesn't exist") { | ||
return fmt.Errorf("cluster %s, execute rclone deletefile command to delete archive failed, output: %s, err: %v", bo, string(output), err) | ||
} | ||
|
||
args = util.ConstructArgs(constants.RcloneConfigArg, opts, "deletefile", fmt.Sprintf("%s.tmp", destBucket), "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when will this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After file is archived and uploaded to S3.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this is for backup with |
||
output, err = exec.Command("rclone", args...).CombinedOutput() | ||
if err != nil && !strings.Contains(string(output), "doesn't exist") { | ||
return fmt.Errorf("cluster %s, execute rclone deletefile command to delete tmp file failed, output: %s, err: %v", bo, string(output), err) | ||
} | ||
|
||
klog.Infof("cluster %s backup %s was deleted successfully", bo, bucket) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1111,6 +1111,19 @@ type TiDBAccessConfig struct { | |||||
TLSClientSecretName *string `json:"tlsClientSecretName,omitempty"` | ||||||
} | ||||||
|
||||||
// +k8s:openapi-gen=true | ||||||
// CleanPolicyType represents the specific delete cloud data policy | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
type CleanPolicyType string | ||||||
|
||||||
const ( | ||||||
// CleanPolicyTypeRetain represents the clean policy is retain | ||||||
CleanPolicyTypeRetain CleanPolicyType = "Retain" | ||||||
// CleanPolicyTypeOnFailure represents the clean policy is on failure | ||||||
CleanPolicyTypeOnFailure CleanPolicyType = "OnFailure" | ||||||
// CleanPolicyTypeIfFailed represents the clean policy is delete | ||||||
CleanPolicyTypeDelete CleanPolicyType = "Delete" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please describe the behavior clearly for each policy. |
||||||
) | ||||||
|
||||||
// +k8s:openapi-gen=true | ||||||
// BackupSpec contains the backup specification for a tidb cluster. | ||||||
type BackupSpec struct { | ||||||
|
@@ -1148,8 +1161,8 @@ type BackupSpec struct { | |||||
UseKMS bool `json:"useKMS,omitempty"` | ||||||
// Specify service account of backup | ||||||
ServiceAccount string `json:"serviceAccount,omitempty"` | ||||||
// CleanData denotes whether to clean backup data before the object is deleted from the cluster | ||||||
CleanData bool `json:"cleanData,omitempty"` | ||||||
// CleanPolicy denotes whether to clean backup data before the object is deleted from the cluster | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the default behavior? |
||||||
CleanPolicy CleanPolicyType `json:"cleanPolicy,omitempty"` | ||||||
} | ||||||
|
||||||
// +k8s:openapi-gen=true | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,8 +57,9 @@ func NewBackupCleaner( | |
} | ||
|
||
func (bc *backupCleaner) Clean(backup *v1alpha1.Backup) error { | ||
if backup.DeletionTimestamp == nil || !backup.Spec.CleanData { | ||
// The backup object has not been deleted,do nothing | ||
if backup.DeletionTimestamp == nil || | ||
backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeRetain || backup.Spec.CleanPolicy == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !shouldCleanData()? |
||
// The backup object has not been deleted or we need to retain backup data,do nothing | ||
return nil | ||
} | ||
ns := backup.GetNamespace() | ||
|
@@ -80,6 +81,15 @@ func (bc *backupCleaner) Clean(backup *v1alpha1.Backup) error { | |
Status: corev1.ConditionTrue, | ||
}) | ||
} | ||
|
||
if backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should be in L60 and we should not set BackupClean condition for this case as the data is not cleaned but retained for the successful backup. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The finalizer handling logic should also be updated accordingly. |
||
// the backup policy is on failure but backup CR succeeds, skip clean-up. | ||
return bc.statusUpdater.Update(backup, &v1alpha1.BackupCondition{ | ||
Type: v1alpha1.BackupClean, | ||
Status: corev1.ConditionTrue, | ||
}) | ||
} | ||
|
||
// not found clean job, create it | ||
job, reason, err := bc.makeCleanJob(backup) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,8 @@ func (bc *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backu | |
ns := backup.GetNamespace() | ||
name := backup.GetName() | ||
|
||
if backup.Spec.CleanData && isDeletionCandidate(backup) && v1alpha1.IsBackupClean(backup) { | ||
if backup.Spec.CleanPolicy != v1alpha1.CleanPolicyTypeRetain && backup.Spec.CleanPolicy != "" && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v1alpha1.ShouldCleanData(backup) |
||
isDeletionCandidate(backup) && v1alpha1.IsBackupClean(backup) { | ||
backup.Finalizers = slice.RemoveString(backup.Finalizers, label.BackupProtectionFinalizer, nil) | ||
_, err := bc.cli.PingcapV1alpha1().Backups(ns).Update(backup) | ||
if err != nil { | ||
|
@@ -98,7 +99,18 @@ func (bc *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backu | |
} | ||
|
||
func needToAddFinalizer(backup *v1alpha1.Backup) bool { | ||
return backup.DeletionTimestamp == nil && backup.Spec.CleanData && !slice.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil) | ||
return backup.DeletionTimestamp == nil && shouldCleanData(backup) && !slice.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil) | ||
} | ||
|
||
func shouldCleanData(backup *v1alpha1.Backup) bool { | ||
switch backup.Spec.CleanPolicy { | ||
case v1alpha1.CleanPolicyTypeDelete, v1alpha1.CleanPolicyTypeOnFailure: | ||
return true | ||
case v1alpha1.CleanPolicyTypeRetain: | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed? |
||
default: | ||
return false | ||
} | ||
} | ||
|
||
func isDeletionCandidate(backup *v1alpha1.Backup) bool { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,7 +288,7 @@ func newBackup() *v1alpha1.Backup { | |
}, | ||
StorageClassName: pointer.StringPtr("local-storage"), | ||
StorageSize: "1Gi", | ||
CleanData: true, | ||
CleanPolicy: v1alpha1.CleanPolicyTypeDelete, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add tests for other values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will add tests later |
||
}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why changing this back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because according to test, this error code is not accurate. When
rclone
returnsdir doesn't exist
error the exit code is 1. May the last time I forgot to usehack/local-up-operator.sh
to update operator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
rclone delete
is a better command in this scenariohttps://rclone.org/commands/rclone_delete/
in my testing, it succeeds if no file found at the specified path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It won't even return error. Should we use this command?
C.C. @DanielZhangQD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's OK if the file can be removed if exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revise to
rclone delete
in 4e1d38f