Skip to content

Commit

Permalink
*: simplify backup code #226
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Sep 18, 2021
1 parent 969e237 commit dccb23a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
2 changes: 0 additions & 2 deletions backup/syncer/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func (s *jobSyncer) updateStatus(job *batchv1.Job) {
// check for completion condition
if cond := jobCondition(batchv1.JobComplete, job); cond != nil {
s.backup.UpdateStatusCondition(v1alpha1.BackupComplete, cond.Status, cond.Reason, cond.Message)

if cond.Status == corev1.ConditionTrue {
s.backup.Status.Completed = true
}
Expand All @@ -91,7 +90,6 @@ func (s *jobSyncer) updateStatus(job *batchv1.Job) {
// check for failed condition
if cond := jobCondition(batchv1.JobFailed, job); cond != nil {
s.backup.UpdateStatusCondition(v1alpha1.BackupFailed, cond.Status, cond.Reason, cond.Message)

if cond.Status == corev1.ConditionTrue {
s.backup.Status.Completed = true
}
Expand Down
2 changes: 1 addition & 1 deletion charts/mysql-operator/crds/mysql.radondb.com_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ spec:
description: HostName represents the host for which to take backup
type: string
image:
default: radondb/mysql-sidecar:0.1.88
default: radondb/mysql-sidecar:latest
description: To specify the image that will be used for sidecar container.
type: string
required:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/mysql.radondb.com_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ spec:
description: HostName represents the host for which to take backup
type: string
image:
default: radondb/mysql-sidecar:0.1.88
default: radondb/mysql-sidecar:latest
description: To specify the image that will be used for sidecar container.
type: string
required:
Expand Down
8 changes: 4 additions & 4 deletions controllers/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func (r *BackupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

// Clear the backup, Just keep historyLimit len
if err = r.clearHistoryJob(ctx, req, backup); err != nil {
if err = r.clearHistoryJob(ctx, req, *backup.Spec.HistoryLimit); err != nil {
return reconcile.Result{}, err
}
return ctrl.Result{}, nil
}

// Clear the History finished Jobs over HistoryLimit.
func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, backup *backup.Backup) error {
func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, historyLimit int32) error {
log := log.Log.WithName("controllers").WithName("Backup")
backups := batchv1.JobList{}
labelSet := labels.Set{"Type": utils.BackupJobTypeName}
Expand All @@ -123,7 +123,7 @@ func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request
})

for i, job := range finishedBackups {
if int32(i) >= int32(len(finishedBackups))-*backup.Spec.HistoryLimit {
if int32(i) >= int32(len(finishedBackups))-historyLimit {
break
}
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
Expand Down Expand Up @@ -155,7 +155,7 @@ func (r *BackupReconciler) updateBackup(savedBackup *apiv1alpha1.Backup, backup
log.Info("update backup object status")
if err := r.Status().Update(context.TODO(), backup.Unwrap()); err != nil {
log.Error(err, fmt.Sprintf("update status backup %s/%s", backup.Name, backup.Namespace),
"backupStatus", backup.Status)
"backupStatus", backup.Status, "saveBackupStatus", savedBackup.Status)
return err
}
}
Expand Down

0 comments on commit dccb23a

Please sign in to comment.