helm cronjob 多个容器

helm cronjob multiple containers

我需要在一个 cronjob 执行中 运行 多个容器。目前我有以下 cronjob.yaml 模板:

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

上面生成了一个 cronjob,它通过传递不同的环境变量来执行两个容器。我可以通过遍历变量使用 values.yaml 生成相同的内容吗?

我能够根据本文中的示例解决这个问题 - https://nikhils-devops.medium.com/helm-chart-for-kubernetes-cronjob-a694b47479a

这是我的模板:

{{- 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 }}

及相关值:

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