Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manually set values on a Kubernetes object using kubectl create?

In particular, I want to set environment variables. I have a CronJob definition that runs on a schedule, but every so often I want to invoke it manually while specifying slightly different environment variables.

I can invoke the cron job manually with this command:

kubectl create job --from=cronjob/my-cron-job my-manual-run

But that copies in all the same environment variables that are specified in the resource definition. How can I add additional, new environment variables using this create job command?

like image 364
soapergem Avatar asked Apr 08 '19 15:04

soapergem


Video Answer


1 Answers

I built on the answer from @Rico to first create the job in kubectl as a --dry-run, then modifiy the job using jq, then apply. This removes the need for having base JSON files and having to manage additional job metadata fields.

For example:

$ kubectl create job --from=cronjob/my-cron-job my-manual-run --dry-run -o "json" \
  | jq ".spec.template.spec.containers[0].env += [{ \"name\": \"envname1\", value:\"$envvalue1\" }]" \
  | kubectl apply -f -
like image 78
Hamish Avatar answered Nov 15 '22 08:11

Hamish