Helm 包含了很多可以在模板中利用的模板函数。包括密码安全、日期、字典、逻辑与流程控制、列表、正则表达式、字符串、类型转换、统一资源定位等17大类的模板函数,本节主要介绍列表函数(Lists Functions)。
列表函数(Lists Functions)
Helm 提供了一个简单的list类型,包含任意顺序的列表。类似于数组或切片,但列表是被设计用于不可变数据类型。
Helm 提供了以下列表函数: append(mustAppend)
, compact (mustCompact)
, concat
, first(mustFirst)
, has (mustHas)
, initial (mustInitial)
, last (mustLast)
, prepend (mustPrepend)
, rest (mustRest)
, reverse (mustReverse)
, seq
, index
, slice (mustSlice)
, uniq (mustUniq)
, until
, untilStep
, without (mustWithout)
。
创建一个整型列表:
$myList := list 1 2 3 4 5
,该代码会生成一个列表[1 2 3 4 5]
。
1、first, mustFirst
获取列表中的第一项,使用 first。
- 语法:
first .Arg1
- first 有问题时会出错,mustFirst 有问题时会向模板引擎返回错误。
templates/first.yaml 文件内容
first: |
{{- $li := list 1 2 3 4 5 }}
{{ first $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/first.yaml
---
# Source: myapp/templates/first.yaml
first: |
1
2、rest, mustRest
获取列表的尾部内容(除了第一项外的所有内容),使用rest(获取 list 中 除第一个元素外的 其他所有 元素)。
- 语法:
rest .Arg1
或mustRest .Arg1
- rest有问题时会出错,mustRest 有问题时会向模板引擎返回错误。
templates/rest.yaml 文件内容
rest: |
{{- $li := list 1 2 3 4 5 }}
{{ rest $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/rest.yaml
---
# Source: myapp/templates/rest.yaml
rest: |
[2 3 4 5]
3、last, mustLast
使用last获取列表的最后一项,这大致类似于反转列表然后调用first:
- 语法:
last .Arg1
或mustLast .Arg1
templates/last.yaml 文件内容
last: |
{{- $li := list 1 2 3 4 5 }}
{{ last $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/last.yaml
---
# Source: myapp/templates/last.yaml
last: |
5
4、initial, mustInitial
获取 list 中除最后一个元素外的所有元素。
- 语法:
initial .Arg1
或mustInitial .Arg1
- initial有问题时会出错,但是 mustInitial 有问题时会向模板引擎返回错误。
initial $myList
返回 [1 2 3 4]。
templates/initial.yaml 文件内容
initial: |
{{- $li := list 1 2 3 4 5 }}
{{ initial $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/initial.yaml
---
# Source: myapp/templates/initial.yaml
initial: |
[1 2 3 4]
5、append, mustAppend
在已有列表中追加一项,创建一个新的列表(追加 元素 到 list 中)。
- 语法:
append .Arg1 .Arg2
或mustAppend .Arg1 .Arg2
- append 有问题时会出错,但 mustAppend 有问题时会向模板引擎返回错误。
$new = append $myList 6
语句会设置 $new 为 [1 2 3 4 5 6]。 $myList会保持不变。
templates/append.yaml 文件内容
append: |
{{- $li := list }}
{{ $li = append $li 123 }}
{{ $li = append $li 567 }}
list: {{ $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/append.yaml
---
# Source: myapp/templates/append.yaml
append: |
list: [123 567]
6、prepend, mustPrepend
将元素添加到列表的前面,生成一个新的列表(在 list 中的最前面 追加 元素)。
- 语法:
prepend .Arg1 .Arg2
或mustPrepend .Arg1 .Arg2
- prepend 有问题时会出错,但 mustPrepend 有问题时会向模板引擎返回错误。
prepend $myList 0
语句会生成 [0 1 2 3 4 5]。 $myList会保持不变。
templates/prepend.yaml 文件内容
prepend: |
{{- $li := list }}
{{ $li = prepend $li 123 }}
{{ $li = prepend $li 567 }}
list: {{ $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/prepend.yaml
---
# Source: myapp/templates/prepend.yaml
prepend: |
list: [567 123]
7、concat
将任意数量的列表串联成一个:
- 语法:
concat .Arg1 .Arg2
concat $myList ( list 6 7 ) ( list 8 )
会生成 [1 2 3 4 5 6 7 8]。 $myList 会保持不变。
templates/concat.yaml 文件内容
concat: |
{{- $li1 := list 1 2 3 4 }}
{{- $li2 := list 5 6 7 8 }}
{{ concat $li1 $li2 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/concat.yaml
---
# Source: myapp/templates/concat.yaml
concat: |
[1 2 3 4 5 6 7 8]
8、reverse, mustReverse
反转给定的列表生成一个新列表(对 list 进行 倒序 排序)。
- 语法:
reverse .Arg1
或mustReverse .Arg1
- reverse 有问题时会出错,但 mustReverse 有问题时会向模板引擎返回错误。
templates/reverse.yaml 文件内容
reverse: |
{{- $li := list 1 2 3 4 5 }}
{{ reverse $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/reverse.yaml
---
# Source: myapp/templates/reverse.yaml
reverse: |
[5 4 3 2 1]
9、uniq, mustUniq
生成一个移除重复项的列表(去除重复的元素)。
- 语法:
uniq .Arg1
- uniq 有问题时会出错,但 mustUniq 有问题时会向模板引擎返回错误。
list 1 1 1 2 | uniq
会生成 [1 2]
templates/uniq.yaml 文件内容
uniq: |
{{- $li := list 1 1 2 3 3 4 5 5 }}
{{ uniq $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/uniq.yaml
---
# Source: myapp/templates/uniq.yaml
uniq: |
[1 2 3 4 5]
10、without, mustWithout
without 函数从列表中过滤内容(删除 list 中的一个元素)。
- 语法:
without .Arg1 .Arg2..
- 一个过滤器可以过滤多个元素:
without $myList 1 3 5
- without 有问题时会出错,但 mustWithout 有问题时会向模板引擎返回错误。
templates/without.yaml 文件内容
without: |
{{- $li := list 1 2 3 4 5 }}
{{ without $li 3 }}
{{ without $li 3 5 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/without.yaml
---
# Source: myapp/templates/without.yaml
without: |
[1 2 4 5]
[1 2 4]
11、has, mustHas
验证列表是否有特定元素。
- 语法:
has .Arg1 .Arg2
或mustHas .Arg1 .Arg2
- has 有问题时会出错,但 mustHas 有问题时会向模板引擎返回错误。
has 4 $myList
会返回 true, 但 has “hello” $myList 就会返回false。
templates/has.yaml 文件内容
has: |
{{- $li := list 1 2 3 4 5 }}
{{- if has 2 $li }}
has: true
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/has.yaml
---
# Source: myapp/templates/has.yaml
has: |
has: true
12、compact, mustCompact
接收一个列表并删除空值项(去除 列表 中为空 的元素)。
- 语法:
compact .Arg1
或mustCompact .Arg1
- compact 有问题时会出错,但 mustCompact 有问题时会向模板引擎返回错误。
$list := list 1 "a" "foo" ""
$copy := compact $list
compact 会返回一个移除了空值(比如, “”)的新列表。
templates/compact.yaml 文件内容
compact: |
{{- $li := list "a" "" "c" "d" "f" }}
{{ compact $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/compact.yaml
---
# Source: myapp/templates/compact.yaml
compact: |
[a c d f]
13、index
使用index list [n]
获取列表的第n个元素。使用index list [n] [m] ...
获取多位列表元素。
- 语法:
index .Arg1 .Arg2
或mustCompact .Arg1 .Arg2
- index $myList 0 返回 1,同 myList[0]
- index $myList 0 1 同 myList[0][1]
templates/index.yaml 文件内容
# 定义一个变量 hostsPort
{{- $hostsPort := .Values.hostsPort -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "hello world"
# 遍历 containerPort 的 k,v 值
{{- range $name, $val := .Values.containerPort }}
# $name 变量取值为 $hostsPort 中 $name 变量的值
{{ $name }}: {{ index $hostsPort $name | default $val }}
{{- end }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/index.yaml
---
# Source: myapp/templates/index.yaml
# 定义一个变量 hostsPort
apiVersion: v1
kind: ConfigMap
metadata:
name: RELEASE-NAME-configmap
data:
myvalue: "hello world"
http: 80
https: 443
14、slice, mustSlice
从列表中获取部分元素,使用 slice list [n] [m]
。等同于 list[n:m]
.
- 语法:
slice .Arg1 .Arg2...
或mustSlice .Arg1 .Arg2...
- slice 有问题时会出错,但 mustSlice 有问题时会向模板引擎返回错误。
- slice $myList 返回 [1 2 3 4 5]。 等同于 myList[:]。
- slice $myList 3 返回 [4 5]等同于 myList[3:]。
- slice $myList 1 3 返回 [2 3]等同于 myList[1:3]。
- slice $myList 0 3 返回 [1 2 3]等同于 myList[:3]。
templates/slice.yaml 文件内容
# 定义一个 slice slice{1, 3, 5, 7, 9}
{{ $li := list 1 3 5 7 9 }}
# 取 $li 中的 第2 第4 个值 从0开始
slice: {{ slice $li 2 4 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/slice.yaml
---
# Source: myapp/templates/slice.yaml
slice: [5 7]
15、until
until 函数构建一个整数范围(输出指定数的序列, 存到列表中)。
- 语法:
until .Arg1
- 对循环语句很有用: range $i, $e := until 5。
until 5
语句会生成一个列表: [0, 1, 2, 3, 4]。
templates/until.yaml 文件内容
until: |
{{ until 10 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/until.yaml
---
# Source: myapp/templates/until.yaml
until: |
[0 1 2 3 4 5 6 7 8 9]
16、untilStep
类似until, untilStep 生成一个可计数的整型列表。但允许你定义开始,结束和步长。(输出指定 N-M 的序列,并且可以设置 步长,存到列表中.)
- 语法:
untilStep .Arg1 .Arg2 .Arg3
untilStep 3 6 2
语句会生成 [3 5],从3开始,每次加2,直到大于等于6。类似于Python的 range 函数。
templates/untilStep.yaml 文件内容
untilStep: |
{{ untilStep 1 10 2 }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/untilStep.yaml
---
# Source: myapp/templates/untilStep.yaml
untilStep: |
[1 3 5 7 9]
17、seq
原理和bash的 seq 命令类似。
- 语法:
seq .Arg1 .Arg2 .Arg3
- 1 单个参数 (结束位置) - 会生成所有从1到包含 end 的整数。
- 2 多个参数 (开始, 结束) - 会生成所有包含start 和 end 的整数,递增或者递减。
- 3 多个参数 (开始, 步长, 结束) - 会生成所有包含 start 和 end 按 step递增或递减的整数。
seq 5 => 1 2 3 4 5
seq -3 => 1 0 -1 -2 -3
seq 0 2 => 0 1 2
seq 2 -2 => 2 1 0 -1 -2
seq 0 2 10 => 0 2 4 6 8 10
seq 0 -2 -5 => 0 -2 -4
18、join
通过指定 字符将 列表 中的元素合并为一个字符串。
- 语法:
join .Arg1 .Arg2
templates/join.yaml 文件内容
join: |
{{- $li := list "a" "b" "c" "d" }}
{{ join "_" $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/join.yaml
---
# Source: myapp/templates/join.yaml
join: |
a_b_c_d
19、sortAlpha
将 列表 中的字符串按照 字符的顺序进行排序。
- 语法:
sortAlpha .Arg1 .Arg2
templates/sortAlpha.yaml 文件内容
sortAlpha: |
{{- $li := list "bb" "cc" "def" "func" "abc" }}
{{ println "old-->" $li }}
{{ $li = sortAlpha $li }}
{{ println "new-->" $li }}
运行 template
root@kubernetes:/opt/helm/myapp# helm template . --show-only templates/sortAlpha.yaml
---
# Source: myapp/templates/sortAlpha.yaml
sortAlpha: |
old--> [bb cc def func abc]
new--> [abc bb cc def func]
评论区