@@ -277,12 +277,14 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
277
277
return failedJobs [i ].Status .StartTime .Before (failedJobs [j ].Status .StartTime )
278
278
})
279
279
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 {
284
281
break
285
282
}
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
+ }
286
288
}
287
289
}
288
290
@@ -294,12 +296,14 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
294
296
return successfulJobs [i ].Status .StartTime .Before (successfulJobs [j ].Status .StartTime )
295
297
})
296
298
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 {
301
300
break
302
301
}
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
+ }
303
307
}
304
308
}
305
309
@@ -333,10 +337,10 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
333
337
Otherwise, we'll just return the missed runs (of which we'll just use the latest),
334
338
and the next run, so that we can know when it's time to reconcile again.
335
339
*/
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 ) {
337
341
sched , err := cron .ParseStandard (cronJob .Spec .Schedule )
338
342
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 )
340
344
}
341
345
342
346
// 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) {
357
361
}
358
362
}
359
363
if earliestTime .After (now ) {
360
- return nil , sched .Next (now ), nil
364
+ return time. Time {} , sched .Next (now ), nil
361
365
}
362
366
363
367
starts := 0
364
368
for t := sched .Next (earliestTime ); ! t .After (now ); t = sched .Next (t ) {
365
- lastMissed = & t
369
+ lastMissed = t
366
370
// An object might miss several starts. For example, if
367
371
// controller gets wedged on Friday at 5:01pm when everyone has
368
372
// 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) {
380
384
starts ++
381
385
if starts > 100 {
382
386
// 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." )
384
388
}
385
389
}
386
390
return lastMissed , sched .Next (now ), nil
@@ -409,7 +413,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
409
413
410
414
If we've missed a run, and we're still within the deadline to start it, we'll need to run a job.
411
415
*/
412
- if missedRun == nil {
416
+ if missedRun . IsZero () {
413
417
log .V (1 ).Info ("no upcoming scheduled times, sleeping until next" )
414
418
return scheduledResult , nil
415
419
}
@@ -442,7 +446,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
442
446
if cronJob .Spec .ConcurrencyPolicy == batch .ReplaceConcurrent {
443
447
for _ , activeJob := range activeJobs {
444
448
// 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 {
446
450
log .Error (err , "unable to delete active job" , "job" , activeJob )
447
451
return ctrl.Result {}, err
448
452
}
@@ -493,7 +497,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
493
497
// +kubebuilder:docs-gen:collapse=constructJobForCronJob
494
498
495
499
// actually make the job...
496
- job , err := constructJobForCronJob (& cronJob , * missedRun )
500
+ job , err := constructJobForCronJob (& cronJob , missedRun )
497
501
if err != nil {
498
502
log .Error (err , "unable to construct job from template" )
499
503
// don't bother requeuing until we get a change to the spec
0 commit comments