Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍字符串函数(String)。
字符串函数(String)
Helm 包含了以下字符串函数(39个): abbrev
, abbrevboth
, camelcase
, cat
, contains
, hasPrefix
, hasSuffix
, indent
, initials
, kebabcase
, lower
, nindent
, nospace
, plural
, print
, printf
, println
, quote
, randAlpha
, randAlphaNum
, randAscii
, randNumeric
, repeat
, replace
, shuffle
, snakecase
, squote
, substr
, swapcase
, title
, trim
, trimAll
, trimPrefix
, trimSuffix
, trunc
, untitle
, upper
, wrap
, wrapWith
。
1、print
返回各部分组合的字符串。如果可能,非字符串类型会被转换成字符串。
- 语法:
print .Arg1 .Arg2 .Arg3...
- 注意,当相邻两个参数不是字符串时会在它们之间添加一个空格。
print "Matt has " .Dogs " dogs"
templates/print.yaml 文件内容
apiVersion: v1
kind: Configmap
matadata:
name: {{ .Release.Name }}-configmap
data:
{{- $name := .Release.Name }}
print: {{- print $name }}
printf: {{- printf "game - %s drink - %s" .Values.favorite.game .Values.favorite.drink }}
println: {{- println "The Game - " .Values.favorite.game }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/print.yaml
---
# Source: myapp/templates/print.yaml
apiVersion: v1
kind: Configmap
matadata:
name: RELEASE-NAME-configmap
data:
print:RELEASE-NAME
printf:game - LOL drink - coffee
println:The Game - LOL
2、println
和 print效果一样,但会在末尾新添加一行。例子见print部分。
- 语法:
println .Arg1 .Arg2 .Arg3...
3、printf
返回参数按顺序传递的格式化字符串。例子见print部分。
- 语法:
printf .Arg1 .Arg2 .Arg3...
printf "%s has %d dogs." .Name .NumberDogs
占位符取决于传入的参数类型。这包括:
一般用途:
- %v 默认格式的值
- 当打印字典时,加号参数(%+v)可以添加字段名称
- %% 字符百分号,不使用值
布尔值:
- %t true或false
整型:
- %b 二进制
- %c the character represented by the corresponding Unicode code point
- %d 十进制
- %o 8进制
- %O 带0o前缀的8进制
- %q 安全转义的单引号字符
- %x 16进制,使用小写字符a-f
- %X 16进制,使用小写字符A-F
- %U Unicode格式: U+1234; 和"U+%04X"相同
浮点数和复杂成分
- %b 指数二次幂的无小数科学计数法,比如 -123456p-78
- %e 科学计数法,比如: -1.234456e+78
- %E 科学计数法,比如: -1.234456E+78
- %f 无指数的小数,比如: 123.456
- %F 与%f同义
- %g %e的大指数,否则是%f
- %G %E的大指数,否则是%F
- %x 十六进制计数法(和两个指数的十进制幂),比如: -0x1.23abcp+20
- %X 大写的十六进制计数法,比如: -0X1.23ABCP+20
字符串和字节切片:
- %s 未解析的二进制字符串或切片
- %q 安全转义的双引号字符串
- %x 十六进制,小写,每个字节两个字符
- %X 十六进制,大写,每个字节两个字符
切片:
- %p 16进制的第0个元素的地址,以0x开头
4、trim
trim函数移除字符串两边的空格:trim " hello "
结果为 hello
。
- 语法:
trim .Arg1
templates/trim.yaml 文件内容
trim: {{ " a b c " |trim }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/trim.yaml
---
# Source: myapp/templates/trim.yaml
trim: a b c
5、trimAll
从字符串中移除给定的字符:trimAll "$" "$5.00"
结果为 5.00
(作为一个字符串)。
- 语法:
trimAll .Arg1 .Arg2
templates/trimAll.yaml 文件内容
trimAll: {{ "++jicki++" | trimAll "+" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/trimAll.yaml
---
# Source: myapp/templates/trimAll.yaml
trimAll: jicki
6、trimPrefix
从字符串中移除前缀:trimPrefix "-" "-hello""
结果为 hello
。
- 语法:
trimPrefix .Arg1 .Arg2
7、trimSuffix
从字符串中移除后缀:trimSuffix "-" "hello-"
结果为 hello
。
- 语法:
trimSuffix .Arg1 .Arg2
8、lower
将整个字符串转换成小写:lower "HELLO"
结果为 hello
。
- 语法:
lower .Arg1
templates/lower.yaml 文件内容
lower: {{ "ABCDEFG" | upper }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/lower.yaml
---
# Source: myapp/templates/lower.yaml
lower: abcdefg
9、upper
将整个字符串转换成大写:upper "hello"
结果为 HELLO
。
- 语法:
upper .Arg1
templates/upper.yaml 文件内容
upper: {{ "abcdefg" | upper }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/upper.yaml
---
# Source: myapp/templates/upper.yaml
upper: ABCDEFG
10、title
首字母转换成大写: title "hello world"
结果为 Hello World
。
- 语法:
title .Arg1
templates/title.yaml 文件内容
title: {{ "jicki hello world" | title }}
title: {{ "jicki-hello-world" | title }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/title.yaml
---
# Source: myapp/templates/title.yaml
title: Jicki Hello World
title: Jicki-Hello-World
11、untitle
移除首字母大写:untitle "Hello World"
结果为 hello world
。
- 语法:
untitle .Arg1
12、repeat
重复字符串多次(重复输出N次指定字符串):repeat 3 "hello"
结果为 hellohellohello
。
- 语法:
repeat .Arg1 .Arg2
templates/repeat.yaml 文件内容
repeat: {{ "jicki" | repeat 5 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/repeat.yaml
---
# Source: myapp/templates/repeat.yaml
repeat: jickijickijickijickijicki
13、substr
获取字符串的子串(截取字符串 N-M 长度 的内容 ( N 从0开始, M = M-1)),有三个参数:
- 语法:
substr .Arg1 .Arg2 .Arg3
- start (int)
- end (int)
- string (string)
substr 0 5 "hello world"
结果为 hello
。
templates/substr.yaml 文件内容
substr: {{ "abcdefghi" |substr 1 5 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/substr.yaml
---
# Source: myapp/templates/substr.yaml
substr: bcde
14、nospace
去掉字符串中的所有空格:nospace "hello w o r l d"
结果为 helloworld
。
- 语法:
nospace .Arg1
15、trunc
截断字符串:trunc 5 "hello world""
结果为 hello
。trunc -5 "hello world""
结果为 world
。
- 语法:
trunc .Arg1 .Arg2
templates/trunc.yaml 文件内容
trunc: {{ "abcdefg" | trunc 2 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/trunc.yaml
---
# Source: myapp/templates/trunc.yaml
trunc: ab
16、abbrev
隐藏指定字符长度, 以 … 缩写(用省略号截断字符串…):
- 语法:
abbrev .Arg1 .Arg2
- 最大长度
- 字符串
abbrev 5 "hello world"
结果为 he...
,因为将省略号算进了长度中。
templates/abbrev.yaml 文件内容
abbrev: {{ "abcdefg" | abbrev 5 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/abbrev.yaml
---
# Source: myapp/templates/abbrev.yaml
abbrev: ab...
17、abbrevboth
用省略号截断字符串 (…),两边都省略:
- 语法:
abbrevboth .Arg1 .Arg2 .Arg3
- 左侧偏移值
- 最大长度
- 字符串
abbrevboth 5 10 "1234 5678 9123"
结果为 ...5678...
,将2边的省略号都算进了长度中。
18、initials
截取给定字符串每个单词的首字母,并组合在一起。:initials "First Try"
结果为 FT
。
- 语法:
initials .Arg1
templates/initials.yaml 文件内容
initials: {{ "Jicki hello world" |initials }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/initials.yaml
---
# Source: myapp/templates/initials.yaml
initials: Jhw
19、randAlphaNum, randAlpha, randNumeric, and randAscii
这四个字符串函数生成加密安全的(使用 crypto/rand)的随机字符串,但是字符集不同:
- 语法:
randAlpha .Arg1
, 输出指定位数随机 字母,randAlpha 使用 a-zA-Z - 语法:
randNumeric .Arg1
, 输出指定位数随机 数字,randNumeric 使用 0-9 - 语法:
randAlphaNum .Arg1
, 输出指定位数随机 (数字 + 字母),randAlphaNum 使用 0-9a-zA-Z - 语法:
randAscii .Arg1
, 输出指定位数随机 Ascii 码,randAscii 使用所有的可打印ASCII字符
每个函数都需要一个参数:字符串的整型长度。 如:randNumeric 3
会生成三个数字的随机字符串。
templates/randAlpha.yaml 文件内容
randAlpha: {{ randAlpha 20 }}
randNumeric: {{ randNumeric 20 }}
randAlphaNum: {{ randAlphaNum 20 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/randAlpha.yaml
---
# Source: myapp/templates/randAlpha.yaml
randAlpha: dmrHUNScimXhpUiGMapG
randNumeric: 37465873746598120398
randAlphaNum: H2Bv00ToRKntFpy9PFYX
20、wrap
以给定列数给文字换行:wrap 80 $someText
结果会以 $someText
的内容在80列处换行。
- 语法:
wrap .Arg1 .Arg2
21、wrapWith
在字符串中, 指定间隔插入指定字符串。wrapWith 和 wrap 类似,但可以以指定字符串换行。(wrap 使用的是 \n):
- 语法:
wrapWith .Arg1 .Arg2 .Arg3
wrapWith 5 "\t" "Hello World"
结果为 hello world
(其中空格是ASCII tab字符)。
templates/wrapWith.yaml 文件内容
wrapWith: |
{{ "jickiHelloWorld" | wrapWith 5 ":-->" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/wrapWith.yaml
---
# Source: myapp/templates/wrapWith.yaml
wrapWith: |
jicki:-->Hello:-->World
22、contains
测试字符串是否包含在另一个字符串(字符串中是否包含某些字符):
- 语法:
contains .Arg1 .Arg2
contains "cat" "catch"
结果为 true
(因为 catch 包含了 cat)。
templates/contains.yaml 文件内容
contains: |
{{- $name := "jicki" }}
{{- if contains $name "jicki hello world" }}
name: "jicki"
{{- else }}
name: "Null"
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/contains.yaml
---
# Source: myapp/templates/contains.yaml
contains: |
name: "jicki"
23、hasPrefix and hasSuffix
hasPrefix
和 hasSuffix
函数测试字符串是否有给定的前缀或后缀:
- 语法:
hasPrefix .Arg1 .Arg2
,字符串中是否包含指定的前缀 - 语法:
hasSuffix .Arg1 .Arg2
,字符串中是否包含指定的后缀
hasPrefix "cat" "catch"
结果为 true
(因为catch以cat为前缀)。
templates/hasPrefix.yaml 文件内容
hasPrefix: |
{{- if hasPrefix "https" "http://www.baidu.com" }}
url: "https"
{{- else }}
url: "http"
{{- end }}
hasSuffix: |
{{- if hasSuffix "com" "https://baidu.com" }}
url: "url com"
{{- else }}
url: "not com"
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/hasPrefix.yaml
---
# Source: myapp/templates/hasPrefix.yaml
hasPrefix: |
url: "http"
hasSuffix: |
url: "url com"
24、quote and squote
该函数将字符串用双引号(quote) 或者单引号(squote)括起来。
- 语法:
quote .Arg1
- 语法:
squote .Arg1
templates/quote.yaml 文件内容
Notquote: {{ "jicki" }}
quote: {{ quote "jicki" }}
squote: {{ squote "jicki" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/quote.yaml
---
# Source: myapp/templates/quote.yaml
Notquote: jicki
quote: "jicki"
squote: 'jicki'
25、cat
cat 函数将多个字符串合并成一个,用空格分隔:
- 语法:
cat .Arg1 .Arg2 .Arg3...
cat "hello" "beautiful" "world""
结果为 hello beautiful world
。
templates/cat.yaml 文件内容
cat: |
{{ cat "jicki" "hello" "world" "!" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/cat.yaml
---
# Source: myapp/templates/cat.yaml
cat: |
jicki hello world !
26、indent
indent 以指定长度缩进给定字符串所在行,在对齐多行字符串时很有用:
- 语法:
indent .Arg1 .Arg2
indent 4 $lots_of_text
结果为:会将每行缩进4个空格。
templates/indent.yaml 文件内容
indent: {{ "jicki" | indent 3 |quote }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/indent.yaml
---
# Source: myapp/templates/indent.yaml
indent: " jicki"
27、nindent
nindent 函数和indent函数一样,但可以在字符串开头添加新行(字符串换行后缩进指定的长度):
- 语法:
nindent .Arg1 .Arg2
nindent 4 $lots_of_text
结果为:会在字符串所在行缩进4个字符,并且在开头新添加一行。
templates/nindent.yaml 文件内容
nindent: |
{{ "jicki" | nindent 3 |quote }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/nindent.yaml
---
# Source: myapp/templates/nindent.yaml
nindent: |
"\n jicki"
28、replace
执行简单的字符串替换。
- 语法:
replace .Arg1 .Arg2 .Arg3
- 待替换字符串
- 要替换字符串
- 源字符串
"I Am Henry VIII" | replace " " "-"
结果为 I-Am-Henry-VIII
。
templates/replace.yaml 文件内容
replace: |
{{ "http://www.baidu.com" |replace "baidu" "qq" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/replace.yaml
---
# Source: myapp/templates/replace.yaml
replace: |
http://www.qq.com
29、plural
字符串复数化,有3个参数:
- 语法:
plural .Arg1 .Arg2 .Arg3
- 单数字符串
- 复数字符串
- 整型长度
len $fish | plural "one anchovy" "many anchovies"
结果为:如果字符串长度为1,则第一个参数会被打印(one anchovy)。否则,会打印第二个参数(many anchovies)。
- 注意: Helm 现在不支持多语言复杂的复数规则。0被认为是复数的因为英文中作为(zero anchovies) 对待。
30、snakecase
将驼峰写法转换成蛇形写法(将字符串中的单词以 _
相连):snakecase "FirstName"
结果为 first_name
。
- 语法:
snakecase .Arg1
templates/snakecase.yaml 文件内容
snakecase: {{ "jicki hello world"|snakecase }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/snakecase.yaml
---
# Source: myapp/templates/snakecase.yaml
snakecase: jicki_hello_world
31、camelcase
将字符串从蛇形写法转换成驼峰写法(将字符串中的_
相连的单词首字母大写并且去掉_
):
- 语法:
camelcase .Arg1
camelcase "http_server"
结果为 HttpServer
。
templates/camelcase.yaml 文件内容
camelcase: {{ "jicki_hello_world_!" | camelcase }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/camelcase.yaml
---
# Source: myapp/templates/camelcase.yaml
camelcase: JickiHelloWorld!
32、kebabcase
将驼峰写法转换成烤串写法(将字符串中的单词以 -
相连):
- 语法:
kebabcase .Arg1
kebabcase "FirstName"
结果为 first-name
。
templates/kebabcase.yaml 文件内容
kebabcase: {{ "jicki hello_world" | kebabcase }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/kebabcase.yaml
---
# Source: myapp/templates/kebabcase.yaml
kebabcase: jicki-hello-world
33、swapcase
基于单词的算法切换字符串的大小写(将字符串中 大小写 互转):
- 语法:
swapcase .Arg1
转换算法:
- 大写字符变成小写字母
- 首字母变成小写字母
- 空格后或开头的小写字母转换成大写字母
- 其他小写字母转换成大写字母
- 通过unicode.IsSpace(char)定义空格
swapcase "This Is A.Test"
结果为:tHIS iS a.tEST
。
templates/swapcase.yaml 文件内容
swapcase: {{ "abcDEFghiJKL" |swapcase }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/swapcase.yaml
---
# Source: myapp/templates/swapcase.yaml
swapcase: ABCdefGHIjkl
34、shuffle
对字符串进行洗牌(将字符串的次序打乱):
- 语法:
shuffle .Arg1
shuffle "hello"
结果为hello的随机字符串,可能会是 oelhl
。
templates/shuffle.yaml 文件内容
shuffle: {{ "jicki-hello-world" |shuffle }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/shuffle.yaml
---
# Source: myapp/templates/shuffle.yaml
shuffle: -lk-rdolijohwecli
35、splitList
以指定符号 分割 字符串, 并存到 列表中:
- 语法:
splitList .Arg1 .Arg2
templates/splitList.yaml 文件内容
splitList: |
{{ splitList "," "jicki,hello,world!" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/splitList.yaml
---
# Source: myapp/templates/splitList.yaml
splitList: |
[jicki hello world!]
36、splitn
以指定符号 分割 N 个字符串, 并存到 map 中:
- 语法:
splitn .Arg1 .Arg2
templates/splitList.yaml 文件内容
splitn: |
{{ splitn "," 2 "jicki,hello,world!" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/splitn.yaml
---
# Source: myapp/templates/splitn.yaml
splitn: |
map[_0:jicki _1:hello,world!]
37、其他函数
1)html 函数
- 语法:
html .Arg1
templates/html.yaml 文件内容
html: |
{{ html "<html><head><title>Title</title></head></html>" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/html.yaml
---
# Source: myapp/templates/html.yaml
html: |
<html><head><title>Title</title></head></html>
2)js 函数
- 语法:
js .Arg1
templates/js.yaml 文件内容
js: {{ js "<script>var a = 1; var b = a + 1; </scrpit>" }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/js.yaml
---
# Source: myapp/templates/js.yaml
js: \x3Cscript\x3Evar a \x3D 1; var b \x3D a + 1; \x3C/scrpit\x3E
3)hello 函数
- 语法:
hello
templates/hello.yaml 文件内容
hello: {{ hello }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/hello.yaml
---
# Source: myapp/templates/hello.yaml
hello: Hello!
评论区