Golang template (helm) iterating over a list of maps

后端 未结 2 560
悲哀的现实
悲哀的现实 2021-02-10 02:12

I\'m using helm to generate kubernetes yamls.

My values.yaml looks like this:

...
jobs:
  - nme: job1
    command: [sh, -c, \"/app/deployment/start.sh j         


        
相关标签:
2条回答
  • 2021-02-10 02:58

    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 }}
    
    0 讨论(0)
  • 2021-02-10 03:14

    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.

    0 讨论(0)
提交回复
热议问题