How to change the schedule of a Kubernetes cronjob or how to start it manually?

SmCaterpillar picture SmCaterpillar · Mar 15, 2018 · Viewed 10.2k times · Source

Is there a simple way to change the schedule of a kubernetes cronjob like kubectl change cronjob my-cronjob "10 10 * * *"? Or any other way without needing to do kubectl apply -f deployment.yml? The latter can be extremely cumbersome in a complex CI/CD setting because manually editing the deployment yaml is often not desired, especially not if the file is created from a template in the build process.

Alternatively, is there a way to start a cronjob manually? For instance, a job is scheduled to start in 22 hours, but I want to trigger it manually once now without changing the cron schedule for good (for testing or an initial run)?

Answer

Radek 'Goblin' Pieczonka picture Radek 'Goblin' Pieczonka · Mar 15, 2018

You can update only the selected field of resourse by patching it

patch -h                     
Update field(s) of a resource using strategic merge patch, a JSON merge patch, or a JSON patch.           

JSON and YAML formats are accepted.

Please refer to the models in
https://htmlpreview.github.io/?https://github.com/kubernetes/kubernetes/blob/HEAD/docs/api-reference/v1/definitions.html
to find if a field is mutable.

As provided in comment for ref :

kubectl patch cronjob my-cronjob -p '{"spec":{"schedule": "42 11 * * *"}}'

Also, in current kubectl versions, to launch a onetime execution of a declared cronjob, you can manualy create a job that adheres to the cronjob spec with

kubectl create job --from=cronjob/mycron