gpt4 book ai didi

templates - Helm 模板变量为零

转载 作者:行者123 更新时间:2023-12-01 20:19:44 26 4
gpt4 key购买 nike

我正在尝试使用模板从 values.yaml 中提取值,将它们连接到一个 CSV 列表中。这是我的解决方案的一个例子。

值.yaml:

testValue1: "string1"
testValue2: "String2"
credentials:
- c1: "string3"
- c2: "string4"

_helpers.tpl:
{{- define "test.template" -}}
{{- $value1 := .Values.testValue1 -}}
{{- $value2 := .Values.testValue2 -}}
{{- $credentials := "" -}}
{{- range $index, $cred := .Values.credentials -}}
{{- $credentials = $cred "," $credentials -}}
{{- end -}}
{{- printf "%s,%s,%s" $value1 $value2 $credentials -}}
{{- end -}}

测试.yaml
templatedValue: {{ template "test.template" }}

当我跑 helm template --output-dir outputs chart我得到:

测试.yaml
templatedValue: %!s(<nil>),%!s(<nil>),

我设置为值的两个变量都是 nil,而 credentials只是一个空字符串。如果我将 values.yaml 中的值直接放入 test.yaml文件它工作正常。所以我不确定为什么我从模板中获取这些 nil 值。 _helpers.tpl 中还有其他模板它从 values.yaml 文件中获取值,所以我不确定为什么我的模板不起作用。任何帮助是极大的赞赏。
helm version: 
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}

最佳答案

{{ template "test.template" }} Action 执行 "test.template"模板但不传递任何参数给它。所以里面test.template管道将是 <nil>等等 .Values是无效的。
引自 text/template :

{{template "name"}}
The template with the specified name is executed with nil data.

{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.

你必须在 {{template}} 中传递一些东西.如果您没有其他信息要传递,请尝试传递点 . ,当前管道。
{{ template "test.template" . }}

关于templates - Helm 模板变量为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986977/

26 4 0