Skip to content

Commit 8f5bdaa

Browse files
authored
Merge pull request #662 from acekingke/fixCronCanNotRemove
controllers: fix the bug cronjob exits when cluster is gone #661
2 parents 84bdd96 + eb9b49c commit 8f5bdaa

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

backup/cronbackup.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func (j *CronJob) Run() {
3636
if j.BackupScheduleJobsHistoryLimit != nil {
3737
defer j.backupGC()
3838
}
39-
39+
if !j.backupNeedRun() {
40+
log.Info("the cron is deleting when cronjob is starting")
41+
return
42+
}
4043
// check if a backup is running
4144
if j.scheduledBackupsRunningCount() > 0 {
4245
log.Info("at least a backup is running", "running_backups_count", j.scheduledBackupsRunningCount())
@@ -73,6 +76,18 @@ func (j *CronJob) backupSelector() *client.ListOptions {
7376
return selector
7477
}
7578

79+
func (j *CronJob) backupNeedRun() bool {
80+
// When remove the entries, it may has cron task is running.
81+
cluster := &apiv1alpha1.MysqlCluster{}
82+
if err := j.Client.Get(context.TODO(), client.ObjectKey{
83+
Name: j.ClusterName,
84+
Namespace: j.Namespace,
85+
}, cluster); err != nil {
86+
return false
87+
}
88+
return *cluster.Spec.Replicas != 0
89+
}
90+
7691
func (j *CronJob) recurrentBackupLabels() map[string]string {
7792
return map[string]string{
7893
"recurrent": "true",

controllers/backupcron_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func (r *BackupCronReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7878
// Object not found, return. Created objects are automatically garbage collected.
7979
// For additional cleanup logic use finalizers.
8080
log.Info("instance not found, maybe removed")
81+
if err := r.Cron.Remove(instance.Name); err == nil {
82+
log.V(1).Info("remove cronjob from cluster", "name", instance.Name)
83+
}
8184
return ctrl.Result{}, nil
8285
}
8386
return ctrl.Result{}, err
@@ -87,7 +90,11 @@ func (r *BackupCronReconciler) Reconcile(ctx context.Context, req ctrl.Request)
8790
return ctrl.Result{}, err
8891
}
8992
// if spec.backupScheduler is not set then don't do anything
90-
if len(instance.Spec.BackupSchedule) == 0 {
93+
if len(instance.Spec.BackupSchedule) == 0 || *instance.Spec.Replicas == 0 {
94+
if err := r.Cron.Remove(instance.Name); err == nil {
95+
log.V(1).Info("remove cronjob from cluster", "name", instance.Name)
96+
}
97+
9198
return reconcile.Result{}, nil
9299
}
93100

0 commit comments

Comments
 (0)