3

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
  • 255
  • 2
  • 15

1 Answers1

3

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.

Nick_Kh
  • 5,089
  • 2
  • 10
  • 16
  • Thanks for the detailed answer, but for the solution to work (whether the command to retrieve lastScheduleTime and set LAST_SCHEDULE is provided in postStart or in an init container) I need to know is it possible to run kubectl commands to patch a resource from within the container/pod? ... I mean without having to curl k8s API and authenticate to access just the same cluster the container is in, Thanks – Monem Sep 25 '19 at 10:19
  • Indeed you can install `kubectl` command line tool inside the container and supply it with corresponded `kubeconfig` file which will point to the target k8s cluster, as for example look at this Stack [thread](https://stackoverflow.com/questions/51717471/how-to-install-kubectl-in-kubernetes-container-through-docker-image/51795179#51795179). – Nick_Kh Sep 27 '19 at 10:59