如何在Kubernetes的Helm中使用$val从Map中获取值?

huangapple go评论90阅读模式
英文:

how to use $val to get value from map in helm for kubernetes?

问题

我在values.yaml中得到了一个映射:

Schedule:
  app1: node01
  app2: node07
  app3: node13

我想在template/app.yaml中使用它:

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

但是它无法工作:
> 错误:解析错误 (xxx/templates/app.yaml:51):字符 U+0024 '$' 错误
helm.go:94: [debug] 解析错误 (xxx/templates/app.yaml:51):字符 U+0024 '$' 错误

我尝试了一些方法,但仍然无法解决。

#{{- $ScheduleName :=  printf "app%d" $vali }}
#nodeName: get $.Values.Schedule "$ScheduleName"
#这个也无法工作。
英文:

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.

答案1

得分: 0

Go text/template语言包含一个index函数,可以通过键或索引进行任意查找。所以你最后的形式几乎是正确的:你需要将键构造为一个字符串,然后使用index来检索它。

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

确保不要引用$scheduleName变量引用,以免模板语言将其解释为字符串字面量。

英文:

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.

huangapple
  • 本文由 发表于 2022年11月1日 18:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/74274868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定