Mos*_*she 4 go-templates kubernetes-helm
I need to define a variable based on an if
statement and use that variable multiple times.
In order not to repeat the if
I tried something like this:
{{ if condition}}
{{ $my_val = "http" }}
{{ else }}
{{ $my_val = "https" }}
{{ end }}
{{ $my_val }}://google.com
Run Code Online (Sandbox Code Playgroud)
However this returns an error:
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
Run Code Online (Sandbox Code Playgroud)
Ideas?
Dav*_*aze 13
最直接的路径,这是使用ternary
功能,由一枝库提供。那会让你写一些类似的东西
{{ $myVal := ternary "http" "https" condition -}}
{{ $myVal }}://google.com
Run Code Online (Sandbox Code Playgroud)
一个更简单但更间接的路径是编写一个模板来生成值,并调用它
{{- define "scheme" -}}
{{- if condition }}http{{ else }}https{{ end }}
{{- end -}}
{{ template "scheme" . }}://google.com
Run Code Online (Sandbox Code Playgroud)
如果您需要将 this 包含在另一个变量中,Helm 提供了一个include
函数template
,它的作用与此类似,只是它是一个“表达式”而不是直接输出的东西。
{{- $url := printf "%s://google.com" (include "scheme" .) -}}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11811 次 |
最近记录: |