In a k8s cronjob, can I pull the image (or just the tag) which is being used dynamically (at job execution time, not at deployment time using helm etc.) from a configmap (or something similar)?
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:{my-dynamic-tag-here}
My use case is that I have a lot of cron jobs which are getting created at different times and I would need to make changes to the image/tag that is being used for them at a later time.
The image:
value must be directly specified in the Kubernetes manifest. Nothing in Kubernetes proper does any sort of lookup, substitution, or indirect reference to modify this value.
Setting this image is one of the most useful abilities of wrapper tools like Helm or Kustomize. I'm most familiar with Helm. There you'd use Helm's templating language to inject the image tag at deployment time
# templates/cronjob.yaml
image: busybox:{{ .Values.dynamicTag }}
and then when you actually go to deploy it, you can specify that value at the command line
helm upgrade my-app . --set-string dynamicTag=20230710
There is also a path to pass a file of YAML (or JSON) deploy-time configuration values, which can be clearer if your CI tool can write this file.
Kustomize has a specific path to change the image:
value. Again, this involves your CI tool writing out the Kustomization bundle, or running the kustomize edit
CLI tool to modify it at deploy time.