Read schedule expression of Kubernetes CronJob from ConfigMap

8/27/2020

I've more that 10 CronJobs configured in Kubernetes and all working properly as expected.

Now I am planning add 20 more CronJobs. All jobs getting data from external resources, processing it and generating some kind of reports.

I want to configure schedule expression of CronJobs into ConfigMap. for example something like,

kind: ConfigMap
apiVersion: v1
metadata:
  name: scheduler-config
  namespace: test
data:
  job1.schedule.expression: */1 * * * *
  job2.schedule.expression: */5 * * * *
  job3.schedule.expression: */30 * * * *
  job4.schedule.expression: 0 1 * * *
  job5.schedule.expression: 0 0 * * THU

I want to make it more flexible if possible, for example if I update the ConfigMap with new expression then CronJob should be updated with new schedule expression and next time it should run as per new expression value in ConfigMap.

-- Jignesh Dhua
configmap
kubernetes
kubernetes-cronjob

1 Answer

8/31/2020

As I already mentioned in comments

As far as I know ConfigMap is used to set environment variables inside container or is mounted as volume. I don't think you can use configmap to set schedule in cronjob.


As an alternative you could use helm cronjobs for that and specify the schedules in values.yaml.

Take a look at below cronjobs created with above helm cronjobs.

kubectl get cronjob
NAME                     SCHEDULE      SUSPEND   ACTIVE    LAST SCHEDULE   AGE
cold-fly-hello-env-var   * * * * *     False     0         23s             1m
cold-fly-hello-ubuntu    */5 * * * *   False     0         23s             1m
cold-fly-hello-world     * * * * *     False     0         23s             1m

And their schedule vlaues there, there and there.

-- Jakub
Source: StackOverflow