Skip to content

Commit 634f2b1

Browse files
authored
Merge pull request #929 from khchan123/fix-cronjob
📖 Fix cronjob tutorial to delete and get next schedule correctly
2 parents f633613 + 5c79a48 commit 634f2b1

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,14 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
277277
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)
278278
})
279279
for i, job := range failedJobs {
280-
if err := r.Delete(ctx, job); err != nil {
281-
log.Error(err, "unable to delete old failed job", "job", job)
282-
}
283-
if int32(i) >= *cronJob.Spec.FailedJobsHistoryLimit {
280+
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
284281
break
285282
}
283+
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil {
284+
log.Error(err, "unable to delete old failed job", "job", job)
285+
} else {
286+
log.V(0).Info("deleted old failed job", "job", job)
287+
}
286288
}
287289
}
288290

@@ -294,12 +296,14 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
294296
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
295297
})
296298
for i, job := range successfulJobs {
297-
if err := r.Delete(ctx, job); err != nil {
298-
log.Error(err, "unable to delete old successful job", "job", job)
299-
}
300-
if int32(i) >= *cronJob.Spec.SuccessfulJobsHistoryLimit {
299+
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
301300
break
302301
}
302+
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil {
303+
log.Error(err, "unable to delete old successful job", "job", job)
304+
} else {
305+
log.V(0).Info("deleted old successful job", "job", job)
306+
}
303307
}
304308
}
305309

@@ -333,10 +337,10 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
333337
Otherwise, we'll just return the missed runs (of which we'll just use the latest),
334338
and the next run, so that we can know when it's time to reconcile again.
335339
*/
336-
getNextSchedule := func(cronJob *batch.CronJob, now time.Time) (lastMissed *time.Time, next time.Time, err error) {
340+
getNextSchedule := func(cronJob *batch.CronJob, now time.Time) (lastMissed time.Time, next time.Time, err error) {
337341
sched, err := cron.ParseStandard(cronJob.Spec.Schedule)
338342
if err != nil {
339-
return nil, time.Time{}, fmt.Errorf("Unparseable schedule %q: %v", cronJob.Spec.Schedule, err)
343+
return time.Time{}, time.Time{}, fmt.Errorf("Unparseable schedule %q: %v", cronJob.Spec.Schedule, err)
340344
}
341345

342346
// for optimization purposes, cheat a bit and start from our last observed run time
@@ -357,12 +361,12 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
357361
}
358362
}
359363
if earliestTime.After(now) {
360-
return nil, sched.Next(now), nil
364+
return time.Time{}, sched.Next(now), nil
361365
}
362366

363367
starts := 0
364368
for t := sched.Next(earliestTime); !t.After(now); t = sched.Next(t) {
365-
lastMissed = &t
369+
lastMissed = t
366370
// An object might miss several starts. For example, if
367371
// controller gets wedged on Friday at 5:01pm when everyone has
368372
// gone home, and someone comes in on Tuesday AM and discovers
@@ -380,7 +384,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
380384
starts++
381385
if starts > 100 {
382386
// We can't get the most recent times so just return an empty slice
383-
return nil, time.Time{}, fmt.Errorf("Too many missed start times (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew.")
387+
return time.Time{}, time.Time{}, fmt.Errorf("Too many missed start times (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew.")
384388
}
385389
}
386390
return lastMissed, sched.Next(now), nil
@@ -409,7 +413,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
409413
410414
If we've missed a run, and we're still within the deadline to start it, we'll need to run a job.
411415
*/
412-
if missedRun == nil {
416+
if missedRun.IsZero() {
413417
log.V(1).Info("no upcoming scheduled times, sleeping until next")
414418
return scheduledResult, nil
415419
}
@@ -442,7 +446,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
442446
if cronJob.Spec.ConcurrencyPolicy == batch.ReplaceConcurrent {
443447
for _, activeJob := range activeJobs {
444448
// we don't care if the job was already deleted
445-
if err := r.Delete(ctx, activeJob); ignoreNotFound(err) != nil {
449+
if err := r.Delete(ctx, activeJob, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil {
446450
log.Error(err, "unable to delete active job", "job", activeJob)
447451
return ctrl.Result{}, err
448452
}
@@ -493,7 +497,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
493497
// +kubebuilder:docs-gen:collapse=constructJobForCronJob
494498

495499
// actually make the job...
496-
job, err := constructJobForCronJob(&cronJob, *missedRun)
500+
job, err := constructJobForCronJob(&cronJob, missedRun)
497501
if err != nil {
498502
log.Error(err, "unable to construct job from template")
499503
// don't bother requeuing until we get a change to the spec

0 commit comments

Comments
 (0)