Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍逻辑与流程控制函数(Logic and Flow Control)。
逻辑与流程控制函数(Logic and Flow Control)
Helm 包括了需要逻辑和流程控制的函数,包括 and
, coalesce
, default
, empty
, eq
, fail
, ge
, gt
, le
, lt
, ne
, not
, or
。
1、and
返回两个参数的and布尔值。
- 函数返回它的第一个 empty 参数或者最后一个参数。
- 语法:and .Arg1 .Arg2
templates/and.yaml 文件内容:
# 如果 5 大于 1 and 3 小于等于 10
{{- if and (gt 5 1 ) (le 3 10) }}
msg: true
{{- else }}
msg: false
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/and.yaml
---
# Source: myapp/templates/and.yaml
# 如果 5 大于 1 and 3 小于等于 10
msg: true
2、or
返回两个参数的or布尔值。
- 函数返回它的第一个非 empty 参数或者最后一个参数。
- 语法:
or .Arg1 .Arg2
templates/or.yaml 文件内容
# 判断 1 大于 2 或 1 小于 2 返回为 true
{{- if or (gt 1 2 ) (le 1 2) }}
msg: true
{{- else }}
msg: false
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/or.yaml
---
# Source: myapp/templates/or.yaml
# 判断 1 大于 2 或 1 小于 2 返回为 true
msg: true
3、not
返回参数的布尔求反。(函数返回它的单个参数的布尔值是否定)
- 语法:
not .Arg
templates/not.yaml 文件内容
# 判断 如果 1 大于 2 返回 false 取非 为 true
{{- if not (gt 1 2 ) }}
msg: true
{{- end}}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/not.yaml
---
# Source: myapp/templates/not.yaml
# 判断 如果 1 大于 2 返回 false 取非 为 true
msg: true
4、eq
返回参数的布尔等式(比如, Arg1 == Arg2)。
- 语法:
eq .Arg1 .Arg2
5、ne
返回参数的布尔非等式(比如 Arg1 != Arg2)。
- 语法:
ne .Arg1 .Arg2
6、lt
如果第一参数小于第二参数,返回布尔真。否则返回假(比如, Arg1 < Arg2)。
- 语法:
lt .Arg1 .Arg2
7、le
如果第一参数小于等于第二参数,返回布尔真,否则返回假(比如, Arg1 <= Arg2)。
- 语法:
le .Arg1 .Arg2
8、gt
如果第一参数大于第二参数,返回布尔真,否则返回假(比如, Arg1 > Arg2)。
- 语法:
gt .Arg1 .Arg2
9、ge
如果第一参数大于等于第二参数,返回布尔真,否则返回假。(比如, Arg1 >= Arg2)。
- 语法:
ge .Arg1 .Arg2
10、empty
判断字符串是否为空,如果给定的值被认为是空的,则empty函数返回true,否则返回false。空值列举在default部分。
- 语法:
empty .Foo
注意在Go模板条件中,空值是为你计算出来的。这样你很少需要 if empty .Foo ,仅使用 if .Foo 即可。
templates/empty.yaml 文件内容
empty: |
{{- $name := "" }}
{{- if empty $name }}
name: "Null"
{{- else }}
name : {{ $name }}
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/empty.yaml
---
# Source: myapp/templates/empty.yaml
empty: |
name: "Null"
11、fail
抛出异常, 输出自定义的错误. 类似于 go 语言的 panic。(无条件地返回带有指定文本的空 string 或者 error)这在其他条件已经确定而模板渲染应该失败的情况下很有用。
- 语法:
fail .Arg1
fail "Please accept the end user license agreement"
templates/fail.yaml 文件内容
{{ " my error to failed " | fail }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/fail.yaml
Error: template: myapp/templates/fail.yaml:1:28: executing "myapp/templates/fail.yaml" at <fail>: error calling fail: my error to failed
12、coalesce
coalesce函数获取一个列表并返回第一个非空值。
- 语法:
coalesce .Arg1 .Arg2...
coalesce 0 1 2
上述会返回1。
此函数用于扫描多个变量或值:
coalesce .name .parent.name "Matt"
上述示例会优先检查 .name 是否为空。如果不是,就返回值。如果是空, 继续检查.parent.name。 最终,如果 .name 和 .parent.name 都是空,就会返回 Matt。
13、default
使用default设置一个简单的默认值。
- 语法:
default .Arg1 .Arg2
default "foo" .Bar
上述示例中,如果.Bar是非空值,则使用它,否则会返回foo。
"空"定义取决于以下类型:
- 整型: 0
- 字符串: “”
- 列表: []
- 字典: {}
- 布尔: false
- 以及所有的nil (或 null)
对于结构体,没有空的定义,所以结构体从来不会返回默认值。
templates/default.yaml 文件内容
default: {{ defalut 99 "" }}
14、ternary
ternary函数获取两个值和一个test值。如果test值是true,则返回第一个值。如果test值是空,则返回第二个值。 这和C或其他编程语言中的的ternary运算符类似。
- 语法:
ternary .Arg1 .Arg2
true test value
ternary "foo" "bar" true
或者
true | ternary "foo" "bar"
上述结果返回 “foo”。
false test value
ternary "foo" "bar" false
或者
false | ternary "foo" "bar"
上述结果返回 “bar”.
评论区