Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍类型转换函数(Type Conversion)及正则表达式函数(Regular Expressions)。
1、类型转换函数(Type Conversion)
Helm提供了以下类型转换函数:
- atoi: 字符串转换成整型。
- float64: 转换成 float64。
- int: 按系统整型宽度转换成int。
- int64: 转换成 int64。
- toDecimal: 将unix八进制转换成int64。
- toString: 转换成字符串。
- toStrings: 将列表、切片或数组转换成字符串列表。
- toJson (mustToJson): 将列表、切片、数组、字典或对象转换成JSON。
- toPrettyJson (mustToPrettyJson): 将列表、切片、数组、字典或对象转换成格式化JSON。
- toRawJson (mustToRawJson): 将列表、切片、数组、字典或对象转换成HTML字符未转义的JSON。
只有atoi需要输入一个特定的类型。其他的会尝试将任何类型转换成目标类型。
- 比如,int64可以把浮点数转换成整型,也可以把字符串转换成整型。
1.1 toString
转换成字符串。
- 语法:
toString .Arg1
templates/toString.yaml文件内容
{{- $age := 22 }}
toString: {{ $age |toString }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/toString.yaml
---
# Source: myapp/templates/toString.yaml
toString: 22
1.2 toStrings
将列表、切片或数组转换成字符串列表。
- 语法:
toStrings .Arg1
list 1 2 3 | toStrings
会将列表中的所有对象都转换为字符串,如1转成"1",2转成"2"等等,然后将其作为列表返回。
下例将 整型(int)的列表 转换成 字符串(string) 类型的列表
templates/toString.yaml文件内容
toStrings: |
{{- $li := list 1 2 3 }}
{{ toStrings $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/toStrings.yaml
---
# Source: myapp/templates/toStrings.yaml
toStrings: |
[1 2 3]
1.3 toDecimal
给定一个unix八进制权限,转换成十进制(将 八进制 转换为 十进制)。
- 语法:
toDecimal .Arg1
"0777" | toDecimal
会将 0777 转换成 511 并返回int64的值。
1.4 toJson, mustToJson
toJson函数将内容编码为JSON字符串。如果内容无法被转换成JSON会返回空字符串。
- 语法:
toJson .Arg1
- 语法:
mustToJson .Arg1
- mustToJson会返回错误以防无法编码成JSON。
toJson .Item
结果为: .Item
的JSON字符串表示。
templates/toJson.yaml文件内容
toJson: {{ .Values.favorite | toJson }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/toJson.yaml
---
# Source: myapp/templates/toJson.yaml
toJson: {"drink":"coffee","game":"LOL"}
1.5 toPrettyJson, mustToPrettyJson
toPrettyJson函数将内容编码为好看的(缩进的)JSON字符串。
- 语法:
toPrettyJson .Arg1
- 语法:
mustToPrettyJson .Arg1
toPrettyJson .Item
结果为: .Item
的已缩进的JSON字符串表示。
templates/toPrettyJson.yaml文件内容
toPrettyJson: {{ .Values.favorite | toPrettyJson }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/toPrettyJson.yaml
---
# Source: myapp/templates/toPrettyJson.yaml
toPrettyJson: {
"drink": "coffee",
"game": "LOL"
}
1.6 toRawJson, mustToRawJson
toRawJson 函数将内容编码成包含非转义HTML字符的JSON字符串。
- 语法:
toRawJson .Arg1
toRawJson .Item
上述代码结果为: .Item的非转义的JSON字符串表示。
1.7 atoi
atoi函数将 string 类型转换成 int 类型, 如果 string 为字符非数字, 转换后输出 0 。
- 语法:
atoi .Arg1
templates/atoi.yaml文件内容
{{- $name := "2222" }}
atoi: {{ $name | atoi }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/atoi.yaml
---
# Source: myapp/templates/atoi.yaml
atoi: 2222
1.8 int64 函数
将 int 类型转换成 int64 位的整型。
- 语法:
int64 .Arg1
1.9 float64 函数
将 float32 类型转换成 float64 位的浮点型。
- 语法:
float64 .Arg1
2、正则表达式函数(Regular Expressions)
Helm 包含以下正则表达式函数 regexFind(mustRegexFind)
, regexFindAll(mustRegexFindAll)
, regexMatch (mustRegexMatch)
, regexReplaceAll (mustRegexReplaceAll)
, regexReplaceAllLiteral(mustRegexReplaceAllLiteral)
, regexSplit (mustRegexSplit)
。
2.1 regexMatch, mustRegexMatch
如果输入字符串包含可匹配正则表达式任意字符串,则返回true。
- 语法:
regexMatch .Arg1 .Arg2
- 语法:
mustRegexMatch .Arg1 .Arg2
- regexMatch有问题时会出错, mustRegexMatch有问题时会向模板引擎返回错误。
regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" "test@acme.com"
结果为: true
templates/regexMatch.yaml文件内容
regexMatch: |
{{- $IP := "127.0.0.1" }}
{{- if (regexMatch `^[0-9]{1,3}(\.[0-9]{1,3}){3}$` $IP) }}
IP: {{ $IP }}
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/regexMatch.yaml
---
# Source: myapp/templates/regexMatch.yaml
regexMatch: |
IP: 127.0.0.1
2.2 regexFindAll, mustRegexFindAll
使用 正则表达式 匹配所有字符串中的内容,输出 N 条数,最后一个参数表示要返回的子字符串的数量,-1表示匹配所有。
- 语法:
regexFindAll .Arg1 .Arg2 .Arg3
- regexFindAll 有问题时会出错, mustRegexFindAll 有问题时会向模板引擎返回错误。
regexFindAll "[2,4,6,8]" "123456789" -1
结果为: [2 4 6 8]
templates/regexFindAll.yaml文件内容
regexFindAll: {{ regexFindAll "\\d+" "123abc432hhhs9982sahc3hc245" -1 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/regexFindAll.yaml
---
# Source: myapp/templates/regexFindAll.yaml
regexFindAll: [123 432 9982 3 245]
2.3 regexFind, mustRegexFind
返回输入字符串的第一个 (最左边的) 正则匹配(使用 正则表达式匹配字符串中的内容)。
- 语法:
regexFind .Arg1 .Arg2
- 语法:
mustRegexFind .Arg1 .Arg2
- regexFind 有问题时会出错, mustRegexFind 有问题时会向模板引擎返回错误。
regexFind "[a-zA-Z][1-9]" "abcd1234"
结果为: d1
2.4 regexReplaceAll, mustRegexReplaceAll
用 正则表达式 匹配所有字符串中的内容(Regexp的匹配项) 并替换成指定的 字符串,返回输入字符串的拷贝。在替换字符串里面, $
标志被解释为扩展,因此对于实例来说 $1
表示第一个子匹配的文本。
- 语法:
regexReplaceAll .Arg1 .Arg2 .Arg3
- 语法:
mustRegexReplaceAll .Arg1 .Arg2 .Arg3
- regexReplaceAll 有问题时会出错, mustRegexReplaceAll 有问题时会向模板引擎返回错误。
regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W"
结果为: -W-xxW-
templates/regexReplaceAll.yaml文件内容
regexReplaceAll: |
{{ regexReplaceAll "\\d+" "123abc432hhhs9982sahc3hc245" "==" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/regexReplaceAll.yaml
---
# Source: myapp/templates/regexReplaceAll.yaml
regexReplaceAll: |
==abc==hhhs==sahc==hc==
2.5 regexReplaceAllLiteral, mustRegexReplaceAllLiteral
返回输入字符串的拷贝,用替换字符串替换Regexp的匹配项。匹配字符串直接替换而不是扩展。
- 语法:
regexReplaceAllLiteral .Arg1 .Arg2 .Arg3
- 语法:
mustRegexReplaceAllLiteral .Arg1 .Arg2 .Arg3
- regexReplaceAllLiteral 有问题时会出错,mustRegexReplaceAllLiteral 有问题时会向模板引擎返回错误。
regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"
结果为:-${1}-${1}-
2.6 regexSplit, mustRegexSplit
用 正则表达式 匹配所有字符串中的内容 并进行字符串分割,返回一个 list。最后一个参数n确定要返回的子字符串数量,-1表示返回所有匹配。
- 语法:
regexSplit .Arg1 .Arg2 .Arg3
- 语法:
mustRegexSplit .Arg1 .Arg2 .Arg3
- regexSplit 有问题时会出错,mustRegexSplit 有问题时会向模板引擎返回错误。
regexSplit "z+" "pizza" -1
结果为:[pi a]
templates/regexSplit.yaml文件内容
regexSplit: {{ regexSplit "\\d+" "123abc432hhhs9982sahc3hc245" -1 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/regexSplit.yaml
---
# Source: myapp/templates/regexSplit.yaml
regexSplit: [ abc hhhs sahc hc ]
评论区