fix the error when cleaning up jobs for cronjob

This commit is contained in:
Aohan Yang 2021-09-06 19:37:08 +08:00
parent a8c9dd6274
commit ad4fe13528
2 changed files with 12 additions and 2 deletions

View File

@ -613,13 +613,14 @@ func (jm *ControllerV2) syncCronJob(
}
cj.Status.Active = append(cj.Status.Active, *jobRef)
cj.Status.LastScheduleTime = &metav1.Time{Time: *scheduledTime}
if _, err := jm.cronJobControl.UpdateStatus(ctx, cj); err != nil {
updatedCJ, err = jm.cronJobControl.UpdateStatus(ctx, cj)
if err != nil {
klog.InfoS("Unable to update status", "cronjob", klog.KRef(cj.GetNamespace(), cj.GetName()), "resourceVersion", cj.ResourceVersion, "err", err)
return cj, nil, fmt.Errorf("unable to update status for %s (rv = %s): %v", klog.KRef(cj.GetNamespace(), cj.GetName()), cj.ResourceVersion, err)
}
t := nextScheduledTimeDuration(sched, now)
return cj, t, nil
return updatedCJ, t, nil
}
func getJobName(cj *batchv1.CronJob, scheduledTime time.Time) string {

View File

@ -26,6 +26,7 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
@ -153,6 +154,14 @@ func TestCronJobLaunchesPodAndCleansUp(t *testing.T) {
ns := framework.CreateTestingNamespace(namespaceName, server, t)
defer framework.DeleteTestingNamespace(ns, server, t)
backupHandlers := runtime.ErrorHandlers
runtime.ErrorHandlers = append(runtime.ErrorHandlers, func(e error) {
t.Fatalf("Failed with error: %v", e)
})
defer func() {
runtime.ErrorHandlers = backupHandlers
}()
cjClient := clientSet.BatchV1().CronJobs(ns.Name)
stopCh := make(chan struct{})