问题描述
使用Helm模板,我试图基于values.yaml中的数字生成服务器名称列表.该模板的点设置为数字(其为float64).
Using Helm templates, I'm trying to generate a list of server names based on a number in values.yaml. The dot for this template is set to the number (its a float64).
{{- define "zkservers" -}}
{{- $zkservers := list -}}
{{- range int . | until -}}
{{- $zkservers := print "zk-" . ".zookeeper" | append $zkservers -}}
{{- end -}}
{{- join "," $zkservers -}}
{{- end -}}
对于3的输入,我希望它产生:
For an input of, say, 3 I'm expecting this to produce:
zk-0.zookeeper,zk-1.zookeeper,zk-2.zookeeper
什么也没产生.
我知道range块中的行是空操作,因为每次循环迭代时,变量$ zkservers是一个新变量.与外部作用域中的$ zkservers变量不同.
I understand that the line within the range block is a no-op since the variable $zkservers is a new variable each time the loop iterates. It is not the same variable as the $zkservers in the outer scope.
我希望意图不明确我想做什么.我不知所措.
I hope the intention is clear of what I want to do. I am at a loss how to do it.
有人知道如何使用Helm模板做到这一点吗?
Anyone know how to do this with Helm templates?
推荐答案
我遇到了同样的问题,您的解决方案与字典一起度过了我的一天.这是一个很好的解决方法,它可能更简单一些:
I faced with the same problem and your solution with dictionary saved my day. It's a good workaround and it can be just a little simpler:
{{- define "zkservers" -}}
{{- $zk := dict "servers" (list) -}}
{{- range int . | until -}}
{{- $noop := printf "zk-%d.zookeeper" . | append $zk.servers | set $zk "servers" -}}
{{- end -}}
{{- join "," $zk.servers -}}
{{- end -}}
这篇关于舵:生成逗号分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!