问题描述
我有util.tpl
:
{{- define "pod-test" -}}
{{- if eq .Values.appName.properties.name "m1" }}
x: {{.Values.appName.properties.x }}
{{- end }}
我想让 appName
中的 Values.appName.properties.x
字符串动态化。
我从目标 YAML 调用此模板
{{- include "pod-test" . (list "app-name") | indent 2}}
{{- $arg1 := index . 0 }}
{{.Values.{{ $arg1 }}.properties.x }}
这行不通,最好的方法是什么?
解决方法
不会在掌舵上工作。相反,您可以使用 index
函数。从此:
{{ .Values.{{ .app-name }}.properties.x }}
为此:
{{ (index .Values .app-name).properties.x }}
请记住,.Values[.app-name]
应该存在,否则它会抛出错误,因为它会尝试从 nil
对象访问属性。您可以做的是将其包含在检查属性是否存在的 if 块中。
{{ if has .Values .index }}
{{ (index .Values .app-name).properties.x }}
{{ end }}