Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍网络函数(Network Functions)、文件路径函数(File Path Functions)、反射函数(Reflection Functions)。
1、网络函数(Network Functions)
Helm提供了一个网络函数: getHostByName
。getHostByName
获取指定名称的 IPv4 地址(接收一个域名返回IP地址)。
- 语法:
getHostByName .Arg1
getHostByName "www.google.com"
会返回对应的 www.google.com
的IP地址。
templates/getHostByName.yaml 文件内容
jicki.cn: {{ getHostByName "www.jicki.cn" }}
localhost: {{ getHostByName "localhost" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/getHostByName.yaml
---
# Source: myapp/templates/getHostByName.yaml
jicki.cn: 163.181.36.174
localhost: 127.0.0.1
2、文件路径函数(File Path Functions)
Helm模板函数没有访问文件系统的权限,提供了遵循文件路径规范的函数。包括 base
, clean
, dir
, ext
, isAbs
。
2.1 base
返回最后一个元素路径(获取 URL 的文件名)。
- 语法:
base .Arg1
base "foo/bar/baz"
返回 “baz”。
templates/base.yaml 文件内容
base: |
{{ base "/usr/local/bin/helm" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/base.yaml
---
# Source: myapp/templates/base.yaml
base: |
helm
2.2 dir
返回目录, 去掉路径的最后一部分(获取文件 URL 的路径)。
- 语法:
dir .Arg1
dir "foo/bar/baz"
返回 foo/bar。
templates/dir.yaml 文件内容
dir: |
{{ dir "/usr/local/bin/helm" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/dir.yaml
---
# Source: myapp/templates/dir.yaml
dir: |
/usr/local/bin
2.3 clean
清除路径中的无效字符(也可以删除 URL 路径末尾的 / 号)。
- 语法:
clean .Arg1
clean "foo/bar/../baz"
该语句会清理 … 并返回foo/baz。
templates/clean.yaml 文件内容
clean: |
{{ clean "/usr/local/bin/" }}
{{ clean "foo/bar/../baz" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/clean.yaml
---
# Source: myapp/templates/clean.yaml
clean: |
/usr/local/bin
/foo/baz
2.4 ext
返回文件扩展(获取文件名的后缀)。
- 语法:
ext .Arg1
ext "foo.bar"
结果为: .bar
.
templates/ext.yaml 文件内容
root@kubernetes:/opt/helm/myapp# cat templates/ext.yaml
ext: |
{{ ext "/opt/helm/Chart.yaml" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/ext.yaml
---
# Source: myapp/templates/ext.yaml
ext: |
.yaml
2.5 isAbs
检查文件路径是否为绝对路径,使用 isAbs。
- 语法:
isAbs .Arg1
templates/isAbs.yaml 文件内容
isAbs: |
{{- if isAbs "/opt/helm" }}
isAbs: true
{{- else }}
isAbs: false
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/isAbs.yaml
---
# Source: myapp/templates/isAbs.yaml
isAbs: |
isAbs: true
3、反射函数(Reflection Functions)
Helm 提供了基本的反射工具。这有助于高级模板开发者理解特定值的基本Go类型信息。Helm是由Go编写的且是强类型的。 类型系统应用于模板中。
- Go 有一些原始 类型,比如 string, slice, int64, 和 bool。
- Go 有一个开放的 类型 系统,允许开发者创建自己的类型。
Helm 通过 kind functions
和 type functions
提供了一组函数。 deepEqual
也可以用来比较值。
3.1 Kind Functions
有两个类型函数,kindOf 返回对象类型,Kindis函数可以验证值是否为特定类型。
3.1.1 kindOf
kindOf获取一个元素的类型, 深度获取(返回对象类型)。
- 语法:
kindOf .Arg1
kindOf "hello"
返回 string。
templates/kindOf.yaml 文件内容
kindOf: |
{{- $li := list 12 23 34 }}
{{ kindOf $li }}
{{ kindOf "String" }}
{{ kindOf 123 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/kindOf.yaml
---
# Source: myapp/templates/kindOf.yaml
kindOf: |
slice
string
int
3.1.2 Kindis
Kindis函数为类型断言,可以验证值是否为特定类型(判断元素的类型是否为指定类型,可获取深度的类型. 如: interface。对于简单测试(比如在if块中),Kindis函数可以验证值是否为特定类型:
- 语法:
Kindis .Arg1 .Arg2
kindIs "int" 123
返回 true。
templates/Kindis.yaml 文件内容
kindIs: |
{{- $li := list 12 34 56 }}
{{- if kindIs "slice" $li }}
kindIs: slice
{{- else }}
kindIs: interface
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/kindIs.yaml
---
# Source: myapp/templates/kindIs.yaml
kindIs: |
kindIs: slice
3.2 Type Functions
类型处理起来稍微有点复杂,所以有三个不同的函数:
- typeOf 返回值的基础类型: typeOf $foo
- typeIs 类似 kindIs, 但针对type: typeIs “*io.Buffer” $myVal
- typeIsLike 类似 typeIs,除非取消指针引用
注意: 这些都不能测试是否实现了给定的接口,因为在这之前需要提前编译接口。
3.2.1 typeOf
获取元素的类型(返回值的基础类型)。
- 语法:
typeOf .Arg1
templates/typeOf.yaml 文件内容
typeOf: |
{{- $li := list 12 23 34 }}
{{ typeOf $li }}
{{ typeOf "String" }}
{{ typeOf 123 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/typeOf.yaml
---
# Source: myapp/templates/typeOf.yaml
typeOf: |
[]interface {}
string
int
3.2.2 typeIs
类型断言, 判断元素的类型是否为指定类型。
- 语法:
typeIs .Arg1 .Arg2
templates/typeIs.yaml 文件内容
typeIs: |
{{- if typeIs "int" 123 }}
typeIs: true
{{- else }}
typeIs: false
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/typeIs.yaml
---
# Source: myapp/templates/typeIs.yaml
typeIs: |
typeIs: true
3.2.3 typeIsLike
typeIsLike 类似 typeIs,除非取消指针引用。
- 语法:
typeIsLike .Arg1 .Arg2
3.3 deepEqual
如果两个值相比是 “deeply equal”,deepEqual返回true(深度比较两个 元素 是否相等)。
- 语法:
deepEqual .Arg1 .Arg2
- 也适用于非基本类型 (相较于内置的 eq)。
deepEqual (list 1 2 3) (list 1 2 3)
语句返回 true。
templates/deepEqual.yaml 文件内容
deepEqual: |
{{- $li1 := list 12 23 34 }}
{{- $li2 := list 12 23 34 }}
{{ deepEqual $li1 $li2 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/deepEqual.yaml
---
# Source: myapp/templates/deepEqual.yaml
deepEqual: |
true
3.4 tuple / list 函数
创建一个 列表/元祖。
- 语法:
tuple .Arg1 .Arg2...
templates/tuple.yaml 文件内容
tuple: |
{{- $tp:= tuple "1" "2" "3" }}
tuple: {{ $tp }}
typeOf: {{ typeOf $tp }}
kindOf: {{ kindOf $tp }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/tuple.yaml
---
# Source: myapp/templates/tuple.yaml
tuple: |
tuple: [1 2 3 4]
typeOf: []interface {}
kindOf: slice
评论区