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
helm version --short
)? – Discarnatehelm version --short v3.2.0+ge11b7ce
– Arletharesource name may not be empty
so I addedname:
field undermetadata:
and it seems to be working. Not fully correctly at all, because helm is not iteratingcron.cronjobs
and always using the first value, but it still different issue than yours. – Discarnate