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?
Asked
Active
Viewed 809 times
1 Answers
1
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
- 5,662
- 22
- 35
-
4I'm not sure this answers the original poster's question. He doesn't want the schedule; he wants to know the scheduled (not necessarily start) time of each job. For example, let's say a CronJob is scheduled to run Mondays at 11 p.m. Ideally we could expose that time as an environment variable to the job's container(s). This means that if the job, say, fails, and restarts at 1 a.m. Tuesday, the date exposed to the container is _still_ the original scheduled time. This is probably possible by parsing the job name, assuming it starts on time? – Adam Donahue Jun 23 '21 at 14:48