Helm模板变量为nil

问题描述 投票:1回答:1

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

values.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 -}}

test.yaml

templatedValue: {{ template "test.template" }}

[当我运行helm template --output-dir outputs chart时,我得到:

test.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"}
templates go kubernetes kubernetes-helm go-templates
1个回答
2
投票

{{ template "test.template" }}操作执行"test.template"模板,但不向其传递任何参数。因此在test.template内部,管道将为<nil>,因此.Values无效。

text/template引用:

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}},即当前管道。

.
© www.soinside.com 2019 - 2024. All rights reserved.