一、条件语句 if

运算符:

eq: 等于(equal to)
ne: 不等于(not equal to)
lt: 小于(less than)
le: 小于等于(less than or equal to)
gt: 大于(greater than)
ge: 大于等于(greater than or equal to)

if/else 用法:

{{ if PIPELINE }}
  # Do something
{{ else if OTHER PIPELINE }}
  # Do something else
{{ else }}
  # Default case
{{ end }}

当返回值是以下值时,管道会被设置为 false:

布尔false
数字0
空字符串
nil (空或null)
空集合(map, slice, tuple, dict, array)

【示例】:要求.Values.favorite.drink的值等于coffee,则输出mug: true

# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
  drink: coffee
  food: pizza

# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if and .Values.favorite.drink (eq .Values.favorite.drink "coffee") }}mug: true{{ end }}

# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 15:36:58 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytestfunction-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: true

注意: 空白行

# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
  drink: coffee
  food: pizza

# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{ if eq .Values.favorite.drink "coffee"}}
  mug: true
  {{ end }}

# 渲染部署,观察到mug: true上下各存在一个空行
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 15:46:33 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytestfunction-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

  mug: true

# 当模板引擎运行时,它会删除 {{ 和 }} 中的内容,但保留其余空白。通过新增{- if ...}} 的方式消除此空行,修改后内容如下
[root@master01 ~]#  cd  /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | default "tea" | quote }}
  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee"}}
  mug: true
  {{- end }}

# 再次渲染,空行已取消
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 15:50:14 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytestfunction-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: true

二、作用域切换 with

这个用来 控制变量范围

with 的语法与 if 语句类似:

{{ with PIPELINE }}
  # restricted scope
{{ end }}

作用域可以被改变。with允许你为特定对象设定当前作用域( . )。比如,我们已经在使用.Values.favorite。 修改配置映射中的.的作用域指向.Values.favorite:

# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
  drink: coffee
  food: pizza

# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}

# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 16:24:52 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytestfunction-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

但是这里有个注意事项,在限定的作用域内,无法使用.访问父作用域的对象。错误示例如下:

{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}
food: {{ .food | upper | quote }}
release: {{ .Release.Name }} //release不在作用域内
{{- end }}

这样会报错因为 Release.Name不在 . 限定的作用域内 。但是如果对调最后两行就是正常的,因为在{{ end }}之后作用域被重置了。

{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}
food: {{ .food | upper | quote }}
{{- end }}
release: {{ .Release.Name }} #脱离作用域,可以获取到Release.Name

或者,我们可以使用 $从父作用域中访问Release.Name对象。当模板开始执行后 $会 被映射到根作用域,且执行过程中不会更改。

下面这种方式也可以正常工作:

{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}
food: {{ .food | upper | quote }}
release: {{ $.Release.Name }}       //$类似全局参数
{{- end }}

三、循环语句 range

在一个集合中迭代的方式是使用 range操作符。

# 定义values.yaml文件
[root@master01 ~]# cd /root/3/testfunction
[root@master01 testfunction]# vim values.yaml
name: soulchild
favorite:
  drink: coffee
  food: pizza
pizzaTypes:
  - mushrooms
  - cheese
  - peppers
  - onions

现在我们有了一个pizzaTypes列表(模板中称为切片)。修改模板把这个列表打印到配置映射中:

# 定义configmap.yaml文件
[root@master01 ~]# cd /root/3/testfunction/templates/
[root@master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
  toppings: |-
    {{- range .Values.pizzaTypes }}
    - {{ . | title | quote }}
    {{- end }}

该range函数将遍历pizzaTypes列表。每次通过循环,.的值都会发生改变,即 第一次.为mushrooms。将第二个迭代为cheese,依此类推。第二步继续使用后续title及quote函数:

# 渲染部署
[root@master01 ~]# cd /root/3
[root@master01 3]# helm install mytestfunction testfunction/ --dry-run
NAME: mytestfunction
LAST DEPLOYED: Mon Nov 13 16:39:38 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytestfunction-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  toppings: |-
    - "Mushrooms"
    - "Cheese"
    - "Peppers"
    - "Onions"

注意:YAML中的 |- 标记意为采用多行字符串。这对于在清单中嵌入大数据块是一种有用的技术,如例子中所示。

  sizes: |-
    {{- range list "small" "medium" "large" }}
    - {{ . }}
    {{- end }}

结果:

  sizes: |-
    - small
    - medium
    - large