英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论