Helm:通过--set传递数组值

时间:2018-12-19 07:14:54

标签: kubernetes-helm

我有一个cronjob头盔聊天,我可以在values.yaml中定义许多职位,而cronjob.yaml将提供我的职位。在命令行中设置图像标签ID时,我遇到了一个问题,下面的命令不会引发任何错误,但它不会将作业图像标签更新为新的图像。

helm upgrade cronjobs cronjobs/ --wait --set job.myservice.image.tag=b70d744

cronjobs将使用旧的图像标签运行,我该如何解决?

这是我的cronjobs.yaml

{{- $chart_name := .Chart.Name }}
{{- $chart_version := .Chart.Version | replace "+" "_" }}
{{- $release_name := .Release.Name }}

{{- range $job := .Values.jobs }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: "{{ $job.namespace }}"
  name: "{{ $release_name }}-{{ $job.name }}"
  labels:
    chart: "{{ $chart_name }}-{{ $chart_version }}"
spec:
  concurrencyPolicy: {{ $job.concurrencyPolicy }}
  failedJobsHistoryLimit: {{ $job.failedJobsHistoryLimit }}
  suspend: {{ $job.suspend }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: {{ $release_name }}
            cron: {{ $job.name }}
        spec:
          containers:
          - image: "{{ $job.image.repository }}:{{ $job.image.tag }}"
            imagePullPolicy: {{ $job.image.imagePullPolicy }}
            ports:
              - name: http
                containerPort: 80
                protocol: TCP
            name: {{ $job.name }}
            args:
{{ toYaml $job.args | indent 12 }}
            env:
{{ toYaml $job.image.env | indent 12 }}
            volumeMounts:
            - name: nfs
              mountPath: "{{ $job.image.nfslogpath }}"
          restartPolicy: OnFailure
          imagePullSecrets:
            - name: {{ $job.image.secret }}
          volumes:
            - name: nfs
              nfs:
                server: "{{ $job.image.server }}"
                path: "{{ $job.image.nfspath }}"
                readOnly: false
  schedule: {{ $job.schedule | quote }}
  successfulJobsHistoryLimit: {{ $job.successfulJobsHistoryLimit }}
  {{- end }}

这是我的价值观。yaml

jobs:
  - name: myservice
    namespace: default
    image:
      repository: xxx.com/myservice
      tag: fe4544
      pullPolicy: Always
      secret: xxx
      nfslogpath: "/var/logs/"
      nfsserver: "xxx"
      nfspath: "/nfs/xxx/cronjobs/"
      nfsreadonly: false
      env:
    schedule: "*/5 * * * *"
    args:
    failedJobsHistoryLimit: 1
    successfulJobsHistoryLimit: 3
    concurrencyPolicy: Forbid
    suspend: false

  - name: myservice2
    namespace: default
    image:
      repository: xxxx/myservice2
      tag: 1dff39a
      pullPolicy: IfNotPresent
      secret: xxxx
      nfslogpath: "/var/logs/"
      nfsserver: "xxxx"
      nfspath: "/nfs/xxx/cronjobs/"
      nfsreadonly: false
      env:
    schedule: "*/30 * * * *"
    args:
    failedJobsHistoryLimit: 1
    successfulJobsHistoryLimit: 2
    concurrencyPolicy: Forbid
    suspend: false

4 个答案:

答案 0 :(得分:10)

Update for Helm 2.5.0

从Helm 2.5.0开始,可以使用数组索引语法访问列表项。

例如,--set servers[0].port=80变为:

servers:
  - port: 80

答案 1 :(得分:3)

由于您正在 values.yaml 文件中使用数组,请参见related issue

替代解决方案

  • 您的 values.yaml 缺少 args env 的值。我已经在示例中设置了它们,并将缩进量更改为14

  • 您的 cronjob.yaml server: "{{ $job.image.server }}"值为空,我已将其更改为.image.nfsserver

而不是使用数组,而是像下面的示例那样将服务分开:

values.yaml

jobs:
  myservice:
    namespace: default
    image:
      repository: xxx.com/myservice
      tag: fe4544
      pullPolicy: Always
      secret: xxx
      nfslogpath: "/var/logs/"
      nfsserver: "xxx"
      nfspath: "/nfs/xxx/cronjobs/"
      nfsreadonly: false
      env:
        key: val
    schedule: "*/5 * * * *"
    args:
      key: val
    failedJobsHistoryLimit: 1
    successfulJobsHistoryLimit: 3
    concurrencyPolicy: Forbid
    suspend: false

  myservice2:
    namespace: default
    image:
      repository: xxxx/myservice2
      tag: 1dff39a
      pullPolicy: IfNotPresent
      secret: xxxx
      nfslogpath: "/var/logs/"
      nfsserver: "xxxx"
      nfspath: "/nfs/xxx/cronjobs/"
      nfsreadonly: false
      env:
        key: val
    schedule: "*/30 * * * *"
    args:
      key: val
    failedJobsHistoryLimit: 1
    successfulJobsHistoryLimit: 2
    concurrencyPolicy: Forbid
    suspend: false

在您的 cronjob.yaml 中,使用{{- range $job, $val := .Values.jobs }}遍历值。

在使用 {{$ job.name}} 的地方使用 $ job

使用 {{.suspend}} (而不是 {{$ job.suspend}}

)访问诸如 suspend 的值

cronjob.yaml

{{- $chart_name := .Chart.Name }}
{{- $chart_version := .Chart.Version | replace "+" "_" }}
{{- $release_name := .Release.Name }}

{{- range $job, $val := .Values.jobs }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: {{ .namespace }} 
  name: "{{ $release_name }}-{{ $job }}"
  labels:
    chart: "{{ $chart_name }}-{{ $chart_version }}"
spec:
  concurrencyPolicy: {{ .concurrencyPolicy }}
  failedJobsHistoryLimit: {{ .failedJobsHistoryLimit }}
  suspend: {{ .suspend }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: {{ $release_name }}
            cron: {{ $job }}
        spec:
          containers:
          - image: "{{ .image.repository }}:{{ .image.tag }}"
            imagePullPolicy: {{ .image.imagePullPolicy }}
            ports:
              - name: http
                containerPort: 80
                protocol: TCP
            name: {{ $job }}
            args:
{{ toYaml .args | indent 14 }}
            env:
{{ toYaml .image.env | indent 14 }}
            volumeMounts:
            - name: nfs
              mountPath: "{{ .image.nfslogpath }}"
          restartPolicy: OnFailure
          imagePullSecrets:
            - name: {{ .image.secret }}
          volumes:
            - name: nfs
              nfs:
                server: "{{ .image.nfsserver }}"
                path: "{{ .image.nfspath }}"
                readOnly: false
  schedule: {{ .schedule | quote }}
  successfulJobsHistoryLimit: {{ .successfulJobsHistoryLimit }}
{{- end }}

使用-set 传递值:

helm upgrade cronjobs cronjobs/ --wait --set jobs.myservice.image.tag=b70d744

示例:

helm install --debug --dry-run --set jobs.myservice.image.tag=my123tag .

...
HOOKS:
MANIFEST:

---
# Source: foo/templates/cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: default
  name: "illmannered-iguana-myservice"
  labels:
    chart: "foo-0.1.0"
spec:
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 1
  suspend: false
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: illmannered-iguana
            cron: myservice
        spec:
          containers:
          - image: "xxx.com/myservice:my123tag"
            imagePullPolicy:
            ports:
              - name: http
                containerPort: 80
                protocol: TCP
            name: myservice
            args:
              key: val

            env:
              key: val

            volumeMounts:
            - name: nfs
              mountPath: "/var/logs/"
          restartPolicy: OnFailure
          imagePullSecrets:
            - name: xxx
          volumes:
            - name: nfs
              nfs:
                server: "xxx"
                path: "/nfs/xxx/cronjobs/"
                readOnly: false
  schedule: "*/5 * * * *"
  successfulJobsHistoryLimit: 3
---
# Source: foo/templates/cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: default
  name: "illmannered-iguana-myservice2"
  labels:
    chart: "foo-0.1.0"
spec:
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 1
  suspend: false
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: illmannered-iguana
            cron: myservice2
        spec:
          containers:
          - image: "xxxx/myservice2:1dff39a"
            imagePullPolicy:
            ports:
              - name: http
                containerPort: 80
                protocol: TCP
            name: myservice2
            args:
              key: val

            env:
              key: val

            volumeMounts:
            - name: nfs
              mountPath: "/var/logs/"
          restartPolicy: OnFailure
          imagePullSecrets:
            - name: xxxx
          volumes:
            - name: nfs
              nfs:
                server: "xxxx"
                path: "/nfs/xxx/cronjobs/"
                readOnly: false
  schedule: "*/30 * * * *"
  successfulJobsHistoryLimit: 2

希望有帮助!

答案 2 :(得分:1)

如果需要传递数组值,可以使用花括号:

--set test={x,y,z}

结果YAML:

test:
  - x
  - y
  - z

来源:https://github.com/helm/helm/blob/master/docs/using_helm.md#the-format-and-limitations-of---set

答案 3 :(得分:0)

掌舵 3。这对我有用。 --set "servers[0].port=80" --set "servers[1].port=8080"