13

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?

Nicholas Porter
  • 2,588
  • 2
  • 23
  • 37
  • 1
    Does this thread help? https://stackoverflow.com/a/54367455/10363259 – vijay v Apr 21 '21 at 05:43
  • Hello @NicholasPorter. Could you please explain in more detail what is your goal here? What is the exact use case? Also, please share your config files by editing your question. – Wytrzymały Wiktor Apr 21 '21 at 09:09
  • 1
    this sounds a really nice feature to me, is there a way to raise a feature support ticket for this? – Charlie Han Nov 01 '22 at 16:13

2 Answers2

15

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
Anthony Pan
  • 181
  • 1
  • 4
9

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
Nicholas Porter
  • 2,588
  • 2
  • 23
  • 37