Access scheduled run time inside Kubertes CronJob


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

1 Answer


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: This is a variation of 1 but using the resourceFieldRef. Not sure you can refer a cronjob resource tho :(

-- Max Lobur
Source: StackOverflow