Golang template (helm) iterating over a list of maps

对着背影说爱祢 提交于 2019-12-05 12:23:06

Inside the loop the value of the . is set to the current element and you have to use $.Chart.Name to access your data.

I asked a similar question and I think the answer https://stackoverflow.com/a/44734585/8131948 will answer your question too.

I ended up saving the global context and then updating all of my references like this:

{{ $global := . }}
{{ range $i, $job := .Values.jobs -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "name" $global }}-{{ $job.name }}
  labels:
    chart: "{{ $global.Chart.Name }}-{{ $global.Chart.Version | replace "+" "_" }}"
spec:
  activeDeadlineSeconds: {{ $job.activeDeadlineSeconds }}
  template:
    metadata:
      labels:
        app: {{ template "name" $global }}-{{ $job.name }}
    spec:
      containers:
      - name: {{ $global.Chart.Name }}
        image: "{{ $global.Values.image.repository }}:{{ $global.Values.image.tag }}"
        imagePullPolicy: {{ $global.Values.image.pullPolicy }}
        command: {{ $job.command }}
        env:
{{ toYaml $global.Values.service.env | indent 10 }}
        ports:
        - containerPort: {{ $global.Values.service.internalPort }}
{{- end }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!