一、介绍
Helm 提供了一个key/value存储类型称为dict
("dictionary"的简称,Python中也有)。dict
是无序类型。
字典的key 必须是字符串。但值可以是任意类型,甚至是另一个dict
或 list
。
dict
是可变的。set
和unset
函数会修改字典的内容。
二、字典函数
2.1 dict
通过调用dict函数并传递一个键值对列表创建字典。
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
name1: {{ get $myDict "name1" }}
name2: {{ get $myDict "name2" }}
name3: {{ get $myDict "name3" }}
2.2 get
给定一个映射和一个键,从映射中获取值。
get $myDict "name1"
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
name1: {{ get $myDict "name1" }}
name2: {{ get $myDict "name2" }}
name3: {{ get $myDict "name3" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:17:01 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
name1: value1
name2: value2
name3: value3
2.3 set
使用set
给字典添加一个键值对。
$_ := set $myDict "name4" "value4"
注意set
返回字典 (Go模板函数的一个要求),因此你可能需要像上面那样使用使用$_
赋值来获取值。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
name1: {{ get $myDict "name1" }}
name2: {{ get $myDict "name2" }}
name3: {{ get $myDict "name3" }}
{{ $_ := set $myDict "name4" "value4" }}
name4: {{ get $myDict "name4" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:20:32 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
name1: value1
name2: value2
name3: value3
name4: value4
2.4 unset
给定一个映射和key,从映射中删除这个key
$_ := unset $myDict "name4"
和set
一样,需要返回字典。
注意,如果key没有找到,这个操作会简单返回,不会生成错误。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
name1: {{ get $myDict "name1" }}
name2: {{ get $myDict "name2" }}
name3: {{ get $myDict "name3" }}
{{ $_ := set $myDict "name4" "value4" }}
name4: {{ get $myDict "name4" }}
{{ $_ := unset $myDict "name4" }}
name4: {{ get $myDict "name4" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:24:08 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
name1: value1
name2: value2
name3: value3
name4: value4
name4:
2.5 haskey
hasKey
函数会在给定字典中包含了给定key时返回true,
如果key没找到,会返回false
。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{ $_ := set $myDict "name4" "value4" }}
name4: {{ hasKey $myDict "name4" }}
{{ $_ := unset $myDict "name4" }}
name4: {{ hasKey $myDict "name4" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:29:11 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
name4: true
name4: false
~
2.6 pluck
pluck
函数给定一个键和多个映射,并获得所有匹配项的列表:
pluck "name1" $myDict $myOtherDict
上述会返回的list
包含了每个找到的值([value1 otherValue1]
)。
如果key在映射中 没有找到 ,列表中的映射就不会有内容(并且返回列表的长度也会小于调用pluck
的字典)。
如果key是 存在的, 但是值是空值,会插入一个值。
Helm模板中的一个常见用法是使用pluck... | first
从字典集合中获取第一个匹配的键。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{ $_ := set $myDict "name4" "value4" }}
name4: {{ pluck "name4" $myDict }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:31:57 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
name4: [value4]
2.7 dig
dig
函数遍历嵌套的字典,从值列表中选择键。如果在关联的字典中找不到键,会返回默认值。
dig "user" "role" "humanName" "guest" $dict
给一个结构化的字典如下:
{
user: {
role: {
humanName: "curator"
}
}
}
那上述命令就会返回"curator"
。 如果字典连user
都没有,就会返回"guest"
2.8 merge,mustMerge
将两个或多个字典合并为一个, 目标字典优先(只合并目标字典中不存在的键值对,目标字典中已存在键值对不变)
给出:
dst:
default: default
overwrite: me
key: true
src:
overwrite: overwritten
key: false
会得到结果
newdict:
default: default
overwrite: me
key: true
$newdict := merge $dest $source1 $source2
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name5" "value5" "name6" "value6" "name7" "value7" }}
{{ $_ := set $myDict1 "name4" "value4" }}
merge: {{ merge $myDict1 $myDict2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 17:57:53 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
merge: map[name1:value1 name2:value2 name3:value3 name4:value4 name5:value5 name6:value6 name7:value7]
mustMerge
会返回错误,以防出现不成功的合并。
2.9 mergeOverwrite和mustMergeOverwrite
合并两个或多个字典,优先按照 从右到左,在目标字典中有效地覆盖值(覆盖目标字典中已存在键值对的值,合并添加不存在的值):
dst:
default: default
overwrite: me
key: truesrc:
overwrite: overwritten
key: false
会生成:
newdict:
default: default
overwrite: overwritten
key: false
$newdict := mergeOverwrite $dest $source1 $source2
mustMergeOverwrite
会返回错误,以防出现不成功的合并。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
mergeOverwrite: {{ mergeOverwrite $myDict1 $myDict2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 21:14:46 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
mergeOverwrite: map[name1:value1 name2:value name3:value3 name4:value name6:value6 name7:value7]
2.10 keys
keys
函数会返回一个或多个dict
类型中所有的key的list
。由于字典是 无序的,key不会有可预料的顺序。 可以使用sortAlpha
存储。
keys $myDict | sortAlpha
当提供了多个词典时,key会被串联起来。使用uniq
函数和sortAlpha
获取一个唯一有序的键列表。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
keys: {{ keys $myDict1 $myDict2 | uniq | sortAlpha }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 21:36:45 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
keys: [name1 name2 name3 name4 name6 name7]
2.11 pick
pick
函数只从字典中选择给定的键,并创建一个新的dict
。
$new := pick $myDict "name1" "name2"
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
pick: {{ $myDict3 := pick $myDict1 "name1" "name2" }}
myDict3: {{ get $myDict3 "name1" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 21:41:50 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
pick:
myDict3: value1
2.12 omit
omit
函数与函数pick效果相反
,该函数忽略指定的key值,获取未被指定的key和value,并返回一个新的字典
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
omit: {{ $myDict3 := omit $myDict1 "name1" "name2" }}
Dict3: {{ get $myDict3 "name3" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 21:56:22 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
omit:
Dict3: value3
2.13 values
values
函数类似于keys
,返回一个新的list
包含源字典中所有的value(只支持一个字典)。
$vals := values $myDict
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
values: {{ $val := values $myDict1 }}
val: {{ $val }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 21:58:59 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
values:
val: [value1 value2 value3]
2.14 deepCopy 和 mustDeepCopy
deepCopy
和 mustDeepCopy
函数给定一个值并深度拷贝这个值。包括字典和其他结构体。
deepCopy
有问题时会出错, 而mustDeepCopy
会返回一个错误给模板系统。
dict "a" 1 "b" 2 | deepCopy
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $myDict1 := dict "name1" "value1" "name2" "value2" "name3" "value3" }}
{{- $myDict2 := dict "name4" "value" "name2" "value" "name6" "value6" "name7" "value7" }}
deepCopy: {{ $myDict4 := deepCopy $myDict1 }}
myDict4: {{ values $myDict4 }}
~
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:03:28 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
deepCopy:
myDict4: [value1 value2 value3]
三、List函数
Helm 提供了一个简单的list
类型,包含任意顺序的列表。类似于数组或切片,但列表是被设计用于不可变数据类型。
创建一个整型列表:
$myList := list 1 2 3 4 5
3.1 first和mustFirst
获取列表中的第一项,使用 first
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ first $list1 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:15:17 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: 1
3.2 rest和mustRest
获取列表的尾部内容(除了第一项外的所有内容)。
rest $myList
returns
[2 3 4 5]
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ rest $list1 }}
~
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:17:27 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [2 3 4 5 6]
3.3 last和mustLast
使用last
获取列表的最后一项
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ last $list1 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:18:41 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: 6
3.4 initial和mustInitial
通过返回所有元素 但 除了最后一个元素
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ initial $list1 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:21:16 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5]
3.5 append和mustAppend
在已有列表中追加一项,创建一个新的列表。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ $list2 := append $list1 7}}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:25:18 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [1 2 3 4 5 6 7]
3.6 prepend和mustPrepend
将元素添加到列表的前面,生成一个新的列表。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ $list2 := prepend $list1 7}}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:31:05 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [7 1 2 3 4 5 6]
3.7 concat
将任意数量的列表串联成一个。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ $list2 := concat $list1 (list 7 8 9 )}}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:35:00 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [1 2 3 4 5 6 7 8 9]
3.8 reverse和mustReverse
反转给定的列表生成一个新列表。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ $list2 := reverse $list1 }}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:38:02 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [6 5 4 3 2 1]
3.9 uniq和mustUniq
生成一个移除重复项的列表。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 6 6 6 6 6 }}
list: {{ $list2 := uniq $list1 }}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:39:23 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [1 2 3 4 5 6]
3.10 without和mustWithout
without
函数从列表中过滤内容。会去掉指定的内容
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ $list2 := without $list1 3 }}
list2: {{ $list2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:48:23 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list:
list2: [1 2 4 5 6]
3.11 has和mustHas
验证列表是否有特定元素
has 4 $myList
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 }}
list: {{ has 4 $list1 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:51:40 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: true
3.12 compact和mustCompact
接收一个列表并删除空值项
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 "" 7 }}
list: {{ $list1 }}
compact: {{ compact $list1 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Thu Jul 4 22:56:58 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
compact: [1 2 3 4 5 6 7]
compact
会返回一个移除了空值(比如, "")的新列表。
3.13 index
使用index list [n]
获取列表的第n个元素。使用index list [n] [m] ...
获取多位列表元素。
index $myList 0
返回1
,同myList[0]
index $myList 0 1
同myList[0][1]
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
list: {{ $list1 }}
index: {{ index $list1 0 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 09:36:36 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
index: 1
3.14 slice和mustSlice
从列表中获取部分元素,使用 slice list [n] [m]
。等同于 list[n:m],即对列表切片
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]
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
list: {{ $list1 }}
slice1: {{ slice $list1 3 }}
slice2: {{ slice $list1 1 3 }}
slice3: {{ slice $list1 }}
slice4: {{ slice $list1 0 3}}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 09:49:10 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
slice1: [4 5 6 7]
slice2: [2 3]
slice3: [1 2 3 4 5 6 7]
slice4: [1 2 3]
3.15 until
until
函数根据给定整数生成一个列表,列表内容为给定整数的范围。
until 5
上述语句会生成一个列表: [0, 1, 2, 3, 4]
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
list: {{ $list1 }}
until: {{ until 5 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 09:51:36 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
until: [0 1 2 3 4]
3.16 untilStep
类似until
, untilStep
生成一个可计数的整型列表。但允许你定义开始,结束和步长:
untilStep 3 6 2
上述语句会生成 [3 5]
,从3开始,每次加2,直到大于等于6。
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
list: {{ $list1 }}
untilStep: {{ untilStep 1 5 2 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 10:01:07 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
untilStep: [1 3]
3.17 seq
- 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
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
list: {{ $list1 }}
seq: {{ seq 1 2 5 }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 10:08:11 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
list: [1 2 3 4 5 6 7]
seq: 1 3 5
四、数学函数
4.1 整数运算
除非另外指定,否则所有的math函数都是操作 int64
的值。
函数 | 说明 |
---|---|
add | 使用
|
add1 | 自增加1,使用 add1 |
sub | 相减 |
div | 整除 |
mod | 取模 |
mul | 相乘 |
max | 返回一组整数中最大的整数。 |
min | 返回一组数中最小的数。 |
len | 返回参数的长度 |
[root@master ~]# vim mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $list1 := list 1 2 3 4 5 6 7 }}
add: {{ add 1 2 3 }}
add1: {{ add1 1 }}
sub: {{ sub 7 2 }}
div: {{ div 6 3 }}
mod: {{ mod 7 2 }}
mul: {{ mul 2 3 }}
max: {{ max 4 2 5 3 }}
min: {{ min 4 2 5 3 }}
len: {{ len "strings" }}
[root@master ~]# helm install --dry-run goodly-guppy ./mychart
NAME: goodly-guppy
LAST DEPLOYED: Fri Jul 5 10:52:25 2024
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
add: 6
add1: 2
sub: 5
div: 2
mod: 1
mul: 6
max: 5
min: 2
len: 7
4.2 浮点数运算
所有的数学函数使用float64
格式。
函数 | 说明 |
---|---|
addf | 求和 |
add1f | 使用add1f 递增1 |
subf | 相减使用subf |
divf | 使用divf 实现整数除法 |
muf | 乘法 |
maxf | 返回最大浮点数 |
minf | 返回最小浮点数 |
floor | 小于等于输入值的最大浮点整数 |
ceil | 大于等于输入值的最小浮点整数 |
round | 返回一个四舍五入到给定小数位的数 |
4.3 网络函数
getHostByName:
接收一个域名返回IP地址。(没有测试成功,如果有人测试成功麻烦提供一下成功的案例,非常感谢!!!)
getHostByName "www.baidu.com"
会返回对应的www.baidu.com
的地址。
4.4 文件函数
函数 | 说明 |
---|---|
base | 返回最后一个元素路径。base "foo/bar/baz" 返回 "baz"。 |
dir | 返回目录, 去掉路径的最后一部分。因此 dir "foo/bar/baz" 返回 foo/bar 。 |
clean | 清除路径clean "foo/bar/../baz" 。会清理 .. 并返回foo/baz 。 |
ext | 返回文件扩展。ext "foo.bar" 结果为: .bar。 |
isAbs | 检查文件路径是否为绝对路径 |
4.5 反射函数
函数 | 说明 |
---|---|
kind函数 | |
kindOf | 返回对象类型。kindOf "hello" |
kindIs | 验证值是否为特定类型 。 kindIs "int" 123 |
type函数 | |
typeOf | 返回值的基础类型:typeOf $foo |
typeIs | 类似 kindIs , 但针对type: typeIs "*io.Buffer" $myVal |
typeIsLike | 类似 typeIs ,除非取消指针引用 |
对比函数 | |
deepEqual | 两个值相比。也适用于非基本类型。deepEqual (list 1 2 3) (list 1 2 3) |