Access scheduled run time inside Kubertes CronJob

4/15/2019

I'm trying run a cron job with Kubernetes. The job executes a program that performs some computation based on the job's scheduled start time. How can my program get access to this value during its execution?

-- mway
kubernetes
kubernetes-cronjob

1 Answer

4/15/2019

Option 1 (easiest): Put the same value to job container environment variable. I assume you install with helm or similar mechanism, so it should be easy to reuse schedule variable in 2 places.

Option 2: Use a combination of a Role, RoleBinding and ServiceAccount, then use kubectl / language k8s client to retrieve cronjob (you will need to know its name if there's multiple in a namespace) and get schedule from its parameters.

Option 3: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/ This is a variation of 1 but using the resourceFieldRef. Not sure you can refer a cronjob resource tho :(

-- Max Lobur
Source: StackOverflow