setting up multiple cronjob using helm

Viewed 3048

I am trying to deploy multiple cronjob using same helm chart. I have defined cronjobs in values.yaml file which is below.

cronjob:
  crons:
    "0":
    name: one-minute-cron
    schedule: "*/1 * * * *"
    "1":
    name: five-minute-cron
    schedule: "*/5 * * * *"

  metadata:
    namespace: "{{K8S_NS}}"
    creationTimestamp: null

  restartPolicy: OnFailure

  image:
    repository: "{{CI_REGISTRY_IMAGE}}/{{CI_COMMIT_REF_SLUG}}:{{CI_COMMIT_SHA}}.{{CI_PIPELINE_IID}}"
    pullPolicy: "Always"
    imagePullSecrets: git-image-pull-secret-cron
    restartPolicy: OnFailure

  resources:
    requests:
      cpu: 1.0
      memory: "128Mi"
    limits:
      cpu: 2.0
      memory: "192Mi"

Below is my cronjob.yaml file from templates folder.

{{- range $job, $val := .Values.cronjob.crons }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: {{ $.Values.cronjob.metadata.namespace }}
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    metadata:
      creationTimestamp: {{ $.Values.cronjob.metadata.creationTimestamp }}
      name: {{ $.Values.cronjob.crons.name }}
    spec:
      template:
        metadata:
          creationTimestamp: {{ $.Values.cronjob.metadata.creationTimestamp }}
        spec:
          containers:
          - image: {{ $.Values.cronjob.image.repository }}
            imagePullPolicy: {{ $.Values.cronjob.image.pullPolicy }}
            name: {{ $.Values.cronjob.crons.name }}
            resources:
              requests:
                memory: {{ $.Values.cronjob.resources.requests.memory }}
                cpu: {{ $.Values.cronjob.resources.requests.cpu }}
              limits:
                memory: {{ $.Values.cronjob.resources.limits.memory }}
                cpu: {{ $.Values.cronjob.resources.limits.cpu }}

          dnsPolicy: ClusterFirst
          restartPolicy: {{ $.Values.cronjob.image.restartPolicy }}
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
          imagePullSecrets:
            - name: {{ $.Values.cronjob.image.imagePullSecrets }}
  schedule: {{ quote $.Values.cronjob.crons.schedule }}
  successfulJobsHistoryLimit: 3
  suspend: false
status: {}
---
{{- end }}

When I run this in CICD pipeline for deployment in gitlab it throws below error.

Error: UPGRADE FAILED: error validating "": error validating data: [ValidationError(CronJob.spec.jobTemplate.spec.template.spec.containers[0]): missing required field "name" in io.k8s.api.core.v1.Container, ValidationError(CronJob.spec): missing required field "schedule" in io.k8s.api.batch.v1beta1.CronJobSpec]

Note: I have copied the whole repository from gitlab except the sensitive information such as gitlab secrets, templates. I have checked the other blogs as well for this but none of them are helping to get these crons working. Github repo link is here

2 Answers

First, I think that cronjob.crons should be an array. Try changing it to:

cronjob:
  crons:
    - id: "0"
      name: "one-minute-cron"
      schedule: "*/1 * * * *"
    - id: "1"
      name: five-minute-cron
      schedule: "*/5 * * * *"

Then you can iterate over "crons" via:

{{- range $cron := .Values.cronjob.crons }}

You can then access the "cron" fields like so:

...
imagePullPolicy: {{ $.Values.cronjob.image.pullPolicy }}
name: {{ $cron.name }}

If anyone (like me) stumbles on this thread, here is a working example.

In the templates/cronbjob.yaml

{{- range $cron := .Values.cronjob.crons }}
apiVersion: batch/v1
kind: CronJob
spec:
  schedule: {{ $cron.schedule | quote }}
  successfulJobsHistoryLimit: 5

And the values.yaml

cronjob:
  repository: XXXX
  crons:
    "0":
      name: somename
      schedule: "*/10 * * * *"
    "1":
      name: someothername
      schedule: "*/2 * * * *"

Since the schedule field contains a * it will mess up the rendering without the | quote

Related