0

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.

silent
  • 14,494
  • 4
  • 46
  • 86
  • you can’t update the image tag and pull the images using the configmaps at the same time but since you are saying that the changes made to the tag are for using them at a later point of time, you can have two jobs one for updating the image tags in your configmaps and one for pulling and deploying your image. If you find this a bit tricky you can stare the tag details to a txt or csv file which can be used for pulling the tag details or you can use some `CI/CD tools` and in the pipeline you can update the image tags and then pull them for deploying. – Kranthiveer Dontineni Jul 10 '23 at 11:36
  • @KranthiveerDontineni not sure what you mean by "at the same time". My main requirement is that I don't want to touch the cronjob definitions again (using helm or any other deployment tool) – silent Jul 10 '23 at 12:14
  • If you are using the helm or any other deployment tool it's not required to touch the cronjob definitions again. What I meant by `at the same time` is, if you are tying to implement this without using any external tools just by relying on kubernetes jobs you need one job for updating the definition and another job for running the definition. – Kranthiveer Dontineni Jul 10 '23 at 12:33

1 Answers1

1

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • thanks. Yeah I'm quite familiar with helm etc. as well, but my question here is really about runtime referencing of values, not deployment time (which is what helm etc are for) – silent Jul 10 '23 at 12:11
  • There's no mechanism to do that. But if the image tag changes as part of a build, it seems like it shouldn't be too complex to re-run the deployment tool at that point as well. – David Maze Jul 10 '23 at 13:05
  • the problem is that the cronjobs are not created as part of a deployment (using helm etc) but with a different process. Anyway, I get your point that what I'm looking for does not seem to be possible – silent Jul 10 '23 at 13:35