Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting up multiple cronjob using helm

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

like image 793
Shailesh Sutar Avatar asked Oct 11 '25 10:10

Shailesh Sutar


1 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 }}
like image 188
cecunami Avatar answered Oct 14 '25 09:10

cecunami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!