本文介绍了使用Helm设置多个cronjob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用同一舵图部署多个cronjob。我已经在下面的values.yaml文件中定义了cronjob。

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"

下面是我的模板文件夹中的cronjob.yaml文件。

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

当我在CICD管道中运行此程序以便在GitLab中部署时,它抛出下面的错误。

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]

注意:我已从GitLab复制了整个存储库,但GitLab机密、模板等敏感信息除外。我也查看了其他的博客,但没有一个帮助这些crons工作。GitHub回购链接here

推荐答案

首先,我认为cronjob.crons应该是一个数组。尝试将其更改为:

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

然后您可以通过:

遍历";crons";
{{- range $cron := .Values.cronjob.crons }}

然后您可以像这样访问";cron";字段:

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

这篇关于使用Helm设置多个cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:38