Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubectl create job from cronjob and override args

Kubectl allows you to create ad hoc jobs based on existing crons.

This works great but in the documentation there is no specification for passing arguments upon creation of the job.

Example:

kubectl -n my-namespace create job --from=cronjob/myjob my-job-clone

Is there any way I can pass arguements to this job upon creation?

like image 451
Nicholas Porter Avatar asked Dec 06 '25 01:12

Nicholas Porter


2 Answers

Although kubectl currently does not allow you to use the --from flag and specify a command in the same clause, you can work around this limitation by getting the yaml from a dry run and using yq to apply a patch to it.

For example:

# get the original yaml file
kubectl create job myjob --from cronjob/mycronjob --dry-run=client --output yaml > original.yaml

# generate a patch with your new arguments
yq new 'spec.template.spec.containers[0].args[+]' '{INSERT NEW ARGS HERE}' > patch.yaml

# apply the patch
yq merge --arrays update patch.yaml original.yaml > final.yaml

# create job from the final yaml 
kubectl create -f final.yaml
like image 184
Anthony Pan Avatar answered Dec 08 '25 15:12

Anthony Pan


Ok turns out that kubectl does not allow you to use the --from and specify a command in the same clause.

You will get the following error cannot specify --from and command.

For example:

kubectl create job --from=cronjob/my-job.yaml my-job-test -- node run.js --date '2021-04-04'

error: cannot specify --from and command

So in short you cannot use your existing cron template and specify a command.

Closest thing you can get is use the --image flag and manually pass in the image that your file needs, then specify the command and args after.

kubectl create job --image=<YOUR IMAGE NAME> my-job-test -- node run.js --date '2021-04-04'
 
job.batch/my-job-test created
like image 36
Nicholas Porter Avatar answered Dec 08 '25 16:12

Nicholas Porter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!