-
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3002 +/- ##
==========================================
- Coverage 42.00% 41.94% -0.06%
==========================================
Files 155 156 +1
Lines 16744 16773 +29
==========================================
+ Hits 7033 7036 +3
- Misses 9144 9170 +26
Partials 567 567
Flags with carried forward coverage won't be shown. Click here to find out more. |
pkg/apis/pingcap/v1alpha1/types.go
Outdated
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe the behavior clearly for each policy.
pkg/apis/pingcap/v1alpha1/types.go
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
// CleanPolicy denotes whether to clean backup data before the object is deleted from the cluster | |
// CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster |
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.
What's the default behavior?
case v1alpha1.CleanPolicyTypeRetain: | ||
return false |
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.
This can be removed?
pkg/backup/backup/backup_cleaner.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
!shouldCleanData()?
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
v1alpha1.ShouldCleanData(backup)
pkg/backup/backup/backup_cleaner.go
Outdated
@@ -80,6 +80,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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The finalizer handling logic should also be updated accordingly.
pkg/apis/pingcap/v1alpha1/types.go
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
// CleanPolicyType represents the specific delete cloud data policy | |
// CleanPolicyType represents the clean policy of backup data in remote storage |
pkg/apis/pingcap/v1alpha1/types.go
Outdated
type CleanPolicyType string | ||
|
||
const ( | ||
// CleanPolicyTypeRetain represents the clean policy is to retain S3 backup files at any time |
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.
// CleanPolicyTypeRetain represents the clean policy is to retain S3 backup files at any time | |
// CleanPolicyTypeRetain represents that the backup data in remote storage will be retained when the Backup CR is deleted |
pkg/apis/pingcap/v1alpha1/types.go
Outdated
const ( | ||
// CleanPolicyTypeRetain represents the clean policy is to retain S3 backup files at any time | ||
CleanPolicyTypeRetain CleanPolicyType = "Retain" | ||
// CleanPolicyTypeOnFailure represents the clean policy is to clean S3 backup files on failure |
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.
// CleanPolicyTypeOnFailure represents the clean policy is to clean S3 backup files on failure | |
// CleanPolicyTypeOnFailure represents that the backup data in remote storage will be cleaned only for the failed backups when the Backup CR is deleted |
pkg/apis/pingcap/v1alpha1/types.go
Outdated
CleanPolicyTypeRetain CleanPolicyType = "Retain" | ||
// CleanPolicyTypeOnFailure represents the clean policy is to clean S3 backup files on failure | ||
CleanPolicyTypeOnFailure CleanPolicyType = "OnFailure" | ||
// CleanPolicyTypeIfFailed represents the clean policy is to clean S3 backup files at any time |
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.
// CleanPolicyTypeIfFailed represents the clean policy is to clean S3 backup files at any time | |
// CleanPolicyTypeIfFailed represents that the backup data in remote storage will be cleaned when the Backup CR is deleted |
/test pull-e2e-kind |
pkg/backup/backup/backup_cleaner.go
Outdated
if backup.DeletionTimestamp == nil || !backup.Spec.CleanData { | ||
// The backup object has not been deleted,do nothing | ||
if backup.DeletionTimestamp == nil || !v1alpha1.ShouldCleanData(backup) || | ||
backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup) { |
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.
backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup) { | |
(backup.Spec.CleanPolicy == v1alpha1.CleanPolicyTypeOnFailure && !v1alpha1.IsBackupFailed(backup)) { |
pkg/apis/pingcap/v1alpha1/types.go
Outdated
@@ -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 when the object is deleted from the cluster |
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.
// 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 |
@@ -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 v1alpha1.ShouldCleanData(backup) && isDeletionCandidate(backup) && |
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.
We can define a function for these conditions.
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.
LGTM
@cofyc PTAL |
} | ||
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) |
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
returns dir doesn't exist
error the exit code is 1. May the last time I forgot to use hack/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 scenario
https://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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
will add tests later
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 comment
The reason will be displayed to describe this comment to others. Learn more.
when will this *.tmp
file be created?
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.
After file is archived and uploaded to S3.
- upload archive file to
*.tgz.tmp
- move
*.tgz.tmp
to*.tgz
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.
I see. Thanks!
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.
And this is for backup with dumpling
only.
/test pull-e2e-kind |
…db-operator into removeCloudTempData
@cofyc addressed, PTAL again |
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.
LGTM
return nil | ||
} | ||
} | ||
return fmt.Errorf("cluster %s, execute rclone deletefile command failed, output: %s, err: %v", bo, string(output), err) |
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.
s/deletefile/delete
|
||
args = util.ConstructArgs(constants.RcloneConfigArg, opts, "delete", fmt.Sprintf("%s.tmp", destBucket), "") | ||
output, err = exec.Command("rclone", args...).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("cluster %s, execute rclone deletefile command failed, output: %s, err: %v", bo, string(output), err) |
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.
s/deletefile/delete
Status: corev1.ConditionTrue, | ||
Reason: "UpdateBackupPathFailed", | ||
Message: err.Error(), | ||
}) |
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 setting this condition if StatusUpdater.Update(
fails?
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.
copyed from https://github.com/pingcap/tidb-operator/blob/master/cmd/backup-manager/app/backup/manager.go#L159
It sames that it's better to return error directly?
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.
IMO it's meaningless to call StatusUpdater.Update
again if the previous StatusUpdater.Update
fails as the second call can fail too
our current architecture relies on our backup job to update Backup
CR's status, the situation that the status may not be reported to the apiserver is unavoidable
does it cause problems when the StatusUpdater.Update
call fails?
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.
copyed from https://github.com/pingcap/tidb-operator/blob/master/cmd/backup-manager/app/backup/manager.go#L159
It sames that it's better to return error directly?
yes
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.
If we omit this error, we might be unable to clean files on the Cloud when we delete this CR because BackupPath
is not recorded.
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.
but the bm.backupDataToRemote
will not be run if reporting the backup path to the apiserver fails
(if this is not true, I think we should fix it. one more API call does not help)
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.
So should we omit this error? What about BR?
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.
we didn't omit the error, the job fails
@DanielZhangQD PTAL again |
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.
LGTM
Signed-off-by: ti-srebot <[email protected]>
cherry pick to release-1.1 in PR #3018 |
Signed-off-by: ti-srebot <[email protected]> Co-authored-by: Chunzhu Li <[email protected]>
What problem does this PR solve?
fix #2989
What is changed and how does it work?
cleanData
tocleanPolicy
rclone
judgement for remote file not exist error.Check List
Tests
Code changes
Related changes
Does this PR introduce a user-facing change?: