Home > Software engineering >  how to use $val to get value from map in helm for kubernetes?
how to use $val to get value from map in helm for kubernetes?

Time:11-02

I got a map in values.yaml:

Schedule:
  app1: node01
  app2: node07
  app3: node13

and I want to use it in template/app.yaml:

{{- $tuplei := untilStep 1 4 1 -}}
{{- range $keyi, $vali := $tuplei }}
---
spec:
  template:
    spec:
      nodeName: {{ $.Values.Schedule.node$vali }}

It can't work:

Error: parse error at (xxx/templates/app.yaml:51): bad character U 0024 '$' helm.go:94: [debug] parse error at (xxx/templates/app.yaml:51): bad character U 0024 '$'

I have tried some ways, but still can't make it.

#{{- $ScheduleName :=  printf "app%d" $vali }}
#nodeName: get $.Values.Schedule "$ScheduleName"
#can't work, too.

CodePudding user response:

The Go text/template language includes an index function, which does an arbitrary lookup by key or index. So your last form is almost correct: you need to construct the key in a string, and then use index to retrieve it.

{{- $scheduleName := printf "app%d" $vali -}}
nodeName: {{ index $.Values.Schedule $scheduleName }}

Make sure to not quote the $scheduleName variable reference, lest the template language interpret it as a string literal.

  • Related