在Helm模板中定义变量 [英] Define a variable in Helm template
本文介绍了在Helm模板中定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if
语句定义一个变量,并多次使用该变量。
为了不重复if
,我尝试了这样的操作:
{{ if condition}}
{{ $my_val = "http" }}
{{ else }}
{{ $my_val = "https" }}
{{ end }}
{{ $my_val }}://google.com
但是,这将返回错误:
Error: render error in "templates/deployment.yaml":
template: templates/deployment.yaml:30:28:
executing "templates/deployment.yaml" at
<include (print $.Template.BasePath "/config.yaml") .>: error calling
include: template: templates/config.yaml:175:59:
executing "templates/config.yaml" at <"https">: undefined variable: $my_val
想法?
推荐答案
最直接的方法是使用ternary
函数provided by the Sprig library。这将允许您编写类似
{{ $myVal := ternary "http" "https" condition -}}
{{ $myVal }}://google.com
一个更简单但更间接的方法是编写一个模板来生成值,并将其调用
{{- define "scheme" -}}
{{- if condition }}http{{ else }}https{{ end }}
{{- end -}}
{{ template "scheme" . }}://google.com
如果您需要将其包含在另一个变量中,Helm提供了一个include
函数,该函数的作用与template
类似,只是它是一个"表达式",而不是直接输出的内容。
{{- $url := printf "%s://google.com" (include "scheme" .) -}}
这篇关于在Helm模板中定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文