kubernetes: error syncing CronJobController

What happened?

I have some errors from kube-controller-manager logs. The errors repeat and repeat.

E1009 08:42:04.145383       1 cronjob_controllerv2.go:164] error syncing CronJobController cdr/hello, requeuing: Operation cannot be fulfilled on cronjobs.batch "hello": the object has been modified; please apply your changes to the latest version and try again
E1009 08:41:03.919023       1 cronjob_controllerv2.go:164] error syncing CronJobController cdr/hello, requeuing: Operation cannot be fulfilled on cronjobs.batch "hello": the object has been modified; please apply your changes to the latest version and try again

The Cronjob is pretty simple from Kubernetes docs:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          imagePullSecrets:
          - name: dockerhubcred
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
            resources:
              limits:
                memory: "200Mi"
                cpu: "200m"
              requests:
                memory: "100Mi"
                cpu: "100m"

Another cronjobs have the same error.

What did you expect to happen?

I don’t understand why this error is reproduced. Job have Succeeded status and run correctly.

How can we reproduce it (as minimally and precisely as possible)?

Use the above.

Anything else we need to know?

No response

Kubernetes version

$ kubectl version
Server Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.8", GitCommit:"0ce7342c984110dfc93657d64df5dc3b2c0d1fe9", GitTreeState:"clean", BuildDate:"2023-03-15T13:33:02Z", GoVersion:"go1.19.7", Compiler:"gc", Platform:"linux/amd64"}

Cloud provider

No

OS version

From master (nodes is identical):

# On Linux:
$ cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.7"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.7"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.7"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:7:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.7
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.7
$ uname -a
Linux k8s-master1 4.18.0-425.13.1.el8_7.x86_64 #1 SMP Tue Feb 21 15:09:05 PST 2023 x86_64 x86_64 x86_64 GNU/Linux

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 30 (13 by maintainers)

Most upvoted comments

This problem has always existed and belongs to the category of occasional problems. Usually occurs on sync periodic cronjobs。I replicated this issue using this template in the v1.28.2 environment:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "*/1 * * * *"
  successfulJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-container
            image: hub.easystack.io/captain/nginx:1.21
            command: ["/bin/sh", "-c", "sleep 8 && echo 'hello world'"]
          restartPolicy: OnFailure

I added some printing in the code https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controllerv2.go#L211

// Update the CronJob if needed
	if updateStatusAfterCleanup || updateStatusAfterSync {
		logger.V(0).Info("begin update status for cronjob", "cronjob", klog.KObj(cronJobCopy), "resourceVersion", cronJobCopy.ResourceVersion)
		if _, err := jm.cronJobControl.UpdateStatus(ctx, cronJobCopy); err != nil {
			logger.V(0).Info("Unable to update status for cronjob", "cronjob", klog.KObj(cronJobCopy), "resourceVersion", cronJobCopy.ResourceVersion, "err", err)
			return nil, err
		}
	}

And the error log shows that the same resourceVersion was updated by two sync:

`I0305 14:55:11.328838       1 cronjob_controllerv2.go:212] "begin update status for cronjob" cronjob="default/my-cronjob" resourceVersion="361364"
I0305 14:55:11.329024       1 job_controller.go:562] "enqueueing job" key="default/my-cronjob-28493692"
I0305 14:55:11.329184       1 event.go:307] "Event occurred" object="default/my-cronjob" fieldPath="" kind="CronJob" apiVersion="batch/v1" type="Normal" reason="SuccessfulDelete" message="Deleted job my-cronjob-28493692"
I0305 14:55:11.329218       1 event.go:307] "Event occurred" object="default/my-cronjob" fieldPath="" kind="CronJob" apiVersion="batch/v1" type="Normal" reason="SawCompletedJob" message="Saw completed job: my-cronjob-28493695, status: Complete"
I0305 14:55:11.345354       1 cronjob_controllerv2.go:212] "begin update status for cronjob" cronjob="default/my-cronjob" resourceVersion="361364"
I0305 14:55:11.345421       1 event.go:307] "Event occurred" object="default/my-cronjob" fieldPath="" kind="CronJob" apiVersion="batch/v1" type="Normal" reason="SawCompletedJob" message="Saw completed job: my-cronjob-28493695, status: Complete"
I0305 14:55:11.369803       1 cronjob_controllerv2.go:214] "Unable to update status for cronjob" cronjob="default/my-cronjob" resourceVersion="361364" err="Operation cannot be fulfilled on cronjobs.batch \"my-cronjob\": the object has been modified; please apply your changes to the latest version and try again"
E0305 14:55:11.369924       1 cronjob_controllerv2.go:168] error syncing CronJobController default/my-cronjob, requeuing: Operation cannot be fulfilled on cronjobs.batch "my-cronjob": the object has been modified; please apply your changes to the latest version and try again
I0305 14:55:11.370216       1 cronjob_controllerv2.go:212] "begin update status for cronjob" cronjob="default/my-cronjob" resourceVersion="361510"

Because the cronjob obtained by sync every time is taken from the cache:https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controllerv2.go#L183 So this issue may be due to the cache not synchronizing in real-time from ETCD,resulting in the obtained cronjob data not being the last updated data

Can someone else provide some helpful information?

This https://github.com/kubernetes/kubernetes/issues/103607 issue got fixed at https://github.com/kubernetes/kubernetes/pull/104799 but we’re still experiencing it after the fix. We started having the issue with CronJob Controller v2 which hit stable in Kubernetes 1.21. The issue persists on Kubernetes 1.27/1.25 for our case.

@niranjandarshann cronjob works fine. But I see the error message

cronjob_controllerv2.go:164] error syncing CronJobController cdr/hello, requeuing: Operation cannot be fulfilled on cronjobs.batch “hello”: the object has been modified; please apply your changes to the latest version and try again

in kube-controller-manager logs

@devopskolesnikov I am not getting any error message in logs. Its triggering me the message Hello from the Kubernetes cluster