Is it possible to pass Kubernetes Cronjob status.lastScheduleTime timestamp to the container of scheduled job?

9/20/2019

I have a cronjob that should process events occurred since the last operation, for that I use DB to persist this timestamp, my question is whether or not it is possible to directly pass Kubernetes status.lastScheduleTime into the cronjob object as an environment variable?

-- Monem
kubernetes
kubernetes-cronjob

1 Answer

9/23/2019

I see the easiest way accomplish your goal via kubectl set env command, injecting into the particular Cronjob template object the desired status.lastScheduleTime field as environment variable LAST_SCHEDULE:

kubectl set env cronjob/name LAST_SCHEDULE=$(kubectl get cronjob name -o jsonpath='{.status.lastScheduleTime}')

env:
- name: LAST_SCHEDULE
  value: "2019-09-23T08:56:00Z"

You may also find a more comprehensive way achieving Cronjob resource patching, supplying target environment variable in the corresponded template with most recent lastScheduleTime value via Init Containers or through postStart/preStop handlers.

-- mk_sta
Source: StackOverflow