Skip to content

Commit

Permalink
controllers: fix the bug for auto backup #737
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Nov 9, 2022
1 parent 83917d7 commit 055df30
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions controllers/backupcron_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,20 @@ func (r *BackupCronReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if err = instance.Validate(); err != nil {
return ctrl.Result{}, err
}

if *instance.Spec.Replicas == 0 {
if err := r.Cron.Remove(instance.Name + "auto"); err == nil {
log.V(1).Info("remove cronjob from cluster", "name", instance.Name)
}
// without bothS3NFs, clear all
if err := r.ClearBothS3NFS(ctx, instance.Unwrap(), log); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to clear cronjob: %s", err)
}

}
// if spec.backupScheduler is not set then don't do anything
if instance.Spec.BothS3NFS == nil &&
(len(instance.Spec.BackupSchedule) == 0 || *instance.Spec.Replicas == 0) {
if err := r.Cron.Remove(instance.Name); err == nil {
if len(instance.Spec.BackupSchedule) == 0 && instance.Spec.BothS3NFS == nil {
if err := r.Cron.Remove(instance.Name + "auto"); err == nil {
log.V(1).Info("remove cronjob from cluster", "name", instance.Name)
}

Expand Down Expand Up @@ -120,14 +130,16 @@ func (r *BackupCronReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
return ctrl.Result{}, nil
} else {
// without bothS3NFs, clear all
if err := r.ClearBothS3NFS(ctx, instance.Unwrap(), log); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to clear cronjob: %s", err)
}
schedule, err := cron.Parse(instance.Spec.BackupSchedule)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to parse schedule: %s", err)
}

log.V(1).Info("register cluster in cronjob", "key", instance, "schedule", schedule)

return ctrl.Result{}, r.updateClusterSchedule(ctx, instance.Unwrap(), schedule, "", log)
return ctrl.Result{}, r.updateClusterSchedule(ctx, instance.Unwrap(), schedule, "auto", log)
}

}
Expand All @@ -149,7 +161,7 @@ func (r *BackupCronReconciler) updateClusterSchedule(ctx context.Context, cluste
log.Info("update cluster scheduler", "key", cluster,
"scheduler", schedule)

if err := r.Cron.Remove(cluster.Name); err != nil {
if err := r.Cron.Remove(cluster.Name + BackupType); err != nil {
return err
}
break
Expand All @@ -165,7 +177,7 @@ func (r *BackupCronReconciler) updateClusterSchedule(ctx context.Context, cluste
if BackupType == "nfs" {
nfsServerAddress = cluster.Spec.NFSServerAddress
}

log.V(1).Info("register cluster in cronjob", "key", cluster.Name+BackupType, "schedule", schedule)
r.Cron.Schedule(schedule, &backup.CronJob{
ClusterName: cluster.Name,
Namespace: cluster.Namespace,
Expand All @@ -175,7 +187,29 @@ func (r *BackupCronReconciler) updateClusterSchedule(ctx context.Context, cluste
NFSServerAddress: nfsServerAddress,
BackupType: BackupType,
Log: log,
}, cluster.Name)
}, cluster.Name+BackupType)

return nil
}

// Clear all nfs and s3 cronjob
func (r *BackupCronReconciler) ClearBothS3NFS(ctx context.Context, cluster *apiv1alpha1.MysqlCluster, log logr.Logger) error {
r.LockJobRegister.Lock()
defer r.LockJobRegister.Unlock()

for _, entry := range r.Cron.Entries() {
j, ok := entry.Job.(*backup.CronJob)
if ok && j.ClusterName == cluster.Name &&
j.Namespace == cluster.Namespace &&
(j.BackupType == "nfs" || j.BackupType == "s3") {
log.V(1).Info("find s3 or nfs cron.", "key", cluster)

if err := r.Cron.Remove(cluster.Name + j.BackupType); err != nil {
return err
}
break
}
}

return nil
}
Expand Down

0 comments on commit 055df30

Please sign in to comment.