使用Helm模板时,无法计算类型字符串中的字段值 [英] can't evaluate field Values in type string when using Helm template
本文介绍了使用Helm模板时,无法计算类型字符串中的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所示的K8货单,与Helm一起打包。
apiVersion: v1
kind: ServiceAccount
metadata:
{{- template "myFunction" "blah" -}}
我已将_helper.tpl
myFunction定义如下。
{{- define "myFunction" }}
{{- if .Values.nameOverride }}
name: {{ . }}
{{- else }}
name: "test"
{{- end }}
{{- end }}
最后,我有values.yaml
定义nameOverride: ""
。根据我的理解,因为我没有为nameOverride定义任何东西,myFunction
应该输出name: "test"
,当我为nameOverride定义一些东西时,输出应该是name: "blah"
。但是,我收到以下错误。
Error: template: product-search/templates/_helpers.tpl:2:16: executing "myFunction" at <.Values.nameOverride>: can't evaluate field Values in type string
helm.go:88: [debug] template: product-search/templates/_helpers.tpl:2:16: executing
"myFunction" at <.Values.nameOverride>: can't evaluate field Values in type string
你知道为什么吗?谢谢!
推荐答案
在GOtext/template
语言中,.Values
是在特殊变量.
中名为Values
的字段的查找。这个变量会在不同的上下文中改变含义。在define
模板中,.
作为模板的参数值开始,该模板不一定是顶级Helm对象。
{{ template "myFunction" "blah" }}
{{ define "myFunction" }}
{{/* with the call above, . is the string "blah" */}}
{{ end }}
一个模板只有一个参数。在这里,您可以做几件事。一种方法是确定调用方中的字段值,并将其作为模板参数传递:
{{-/* Emit a name: label. The parameter is a string. */-}}
{{- define "myFunction" -}}
name: {{ . }}
{{ end -}}
{{-/* if .Values.nameOverride then "blah" else "test" */-}}
{{- $label := ternary "blah" "test" .Values.nameOverride -}}
{{ include "myFunction" $label | indent 4 }}
另一种方法是将这两个值打包到单个参数中。为此,我倾向于使用Helmlist
函数。但是,这会使模板逻辑变得更加复杂。
{{/* Emit a name: label. The parameter is a list of two values,
the top-level Helm object and the alternate label name to use
if .Values.nameOverride is defined. */}}
{{- define "myFunction" -}}
{{/* . is a list of two values; unpack them */}}
{{- $top := index . 0 -}}
{{- $label := index . 1 -}}
{{/* Now we can look up .Values within the passed top-level object */}}
{{- if $top.Values.nameOverride -}}
name: {{ $label }}
{{ else -}}
name: "test"
{{ end -}}
{{- end -}}
{{/* When calling the function manually construct the parameter list */}}
{{ include "myFunction" (list . "blah") | indent 4 }}
这篇关于使用Helm模板时,无法计算类型字符串中的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文