Search code examples
yamlkubernetes-helmkubernetes-cronjobk8s-cronjobber

helm cronjob multiple containers


I need to run multiple containers in one cronjob execution. Currently I have the following cronjob.yaml template:

jobTemplate:
 spec:
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
        cron: {{ .Values.filesjob.jobName }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        env:
          - name: FILE_MASK
            value: "{{ .Values.filesjob.fileMask }}"
          - name: ID
            value: "{{ .Values.filesjob.id }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        name: {{ .Values.filesjob.jobName }}
        volumeMounts:
        - mountPath: /data
          name: path-to-clean
        - name: path-logfiles
          mountPath: /log
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        env:
          - name: FILE_MASK
            value: "{{ .Values.filesjob.fileMask }}"
          - name: ID
            value: "{{ .Values.filesjob.id }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        name: {{ .Values.filesjob.jobName2 }}
        volumeMounts:
        - mountPath: /data
          name: path2-to-clean
        - name: path2-logfiles
          mountPath: /log

The above generates a cronjob which executes two containers with passing different env variables. can I generate the same using values.yaml by iterating over a variable?


Solution

  • I was able to solve this basing on example from this article - https://nikhils-devops.medium.com/helm-chart-for-kubernetes-cronjob-a694b47479a

    Here is my template:

    {{- if .Values.cleanup.enabled -}}
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: registry-cleanup-job
      namespace: service
      labels:
        {{- include "some.labels" . | nindent 4 }}
    spec:
      schedule: {{ .Values.cleanup.schedule }}
      concurrencyPolicy: Forbid
      successfulJobsHistoryLimit: 2
      failedJobsHistoryLimit: 1
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              {{- range .Values.cleanup.crons }}
              - name: {{ .name | quote }}
                image: {{ $.Values.cleanup.imageName }}
                imagePullPolicy: {{ $.Values.cleanup.pullPolicy }}
                args:
                - {{ .command }}
              {{- end}}
              restartPolicy: Never
    {{- end }}
    

    And related values:

    cleanup:
      enabled: true
      schedule: "0 8 * * 5"
      imageName: digitalocean/doctl:1.60.0
      pullPolicy: IfNotPresent
      crons:
        - command0:
          name: "cleanup0"
          command: command0
        - command1:
          name: "cleanup1"
          command: command1
        - command2:
          name: "cleanup2"
          command: command2
        - command3:
          name: "cleanup3"
          command: command3
        - command4:
          name: "cleanup4"
          command: command4