一、quote and squote¶
该函数将字符串用双引号(quote) 或者单引号(squote)括起来
二、indent¶
indent 以指定长度缩进给定字符串所在行,在对齐多行字符串时很有用
indent 4 $lots_of_text
上述结果会将每行缩进4个空格。
下面进行举例说明:
1.创建一个Chart
$ helm create helm-test
2.删掉系统自带的模板文件
[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*
3.新增模板文件,并填写Release.Name内置变量
[root@k8s-master01 ~]# vim /root/helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
trim: {{ .Values.myValue | indent 4 }}
4.清空values.yaml文件,并添加以下内容
[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello"
5.进行验证,观察到输出结果为 hello
[root@k8s-master01 templates]# helm install aaa /root/helm-test/ --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul 9 15:23:06 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: aaa-configmap
data:
trim: hello
三、nindent¶
nindent 函数和indent函数一样,但可以在字符串开头添加新行
nindent 4 $lots_of_text
上述结果会在字符串所在行缩进4个字符,并且在开头新添加一行
下面进行举例说明:
1.创建一个Chart
$ helm create helm-test
2.删掉系统自带的模板文件
[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*
3.新增模板文件,并填写Release.Name内置变量
[root@k8s-master01 ~]# vim /root/helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
trim: {{ .Values.myValue | nindent 4 }}
4.清空values.yaml文件,并添加以下内容
[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello"
5.进行验证,观察到输出结果如下
[root@k8s-master01 templates]# helm install aaa /root/helm-test/ --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul 9 15:23:06 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: aaa-configmap
data:
trim:
hello
四、replace¶
执行简单的字符串替换。
需要三个参数
- 待替换字符串
- 要替换字符串
- 源字符串
"I Am Henry VIII" | replace " " "-"
上述结果为: I-Am-Henry-VIII
下面进行举例说明:
1.创建一个Chart
$ helm create helm-test
2.删掉系统自带的模板文件
[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*
3.新增模板文件,并填写Release.Name内置变量
[root@k8s-master01 ~]# vim /root/helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
trim: {{ .Values.myValue | replace "he" "ll" }}
4.清空values.yaml文件,并添加以下内容
[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello"
5.进行验证,观察到输出结果为llllo
[root@k8s-master01 templates]# helm install aaa /root/helm-test/ --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul 9 15:23:06 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: aaa-configmap
data:
trim: llllo
五、default¶
使用default设置一个简单的默认值
default "foo" .Bar
上述示例中,如果.Bar是非空值,则使用它,否则会返回foo。
"空"定义取决于以下类型:
- 整型: 0
- 字符串: ""
- 列表:
[] - 字典:
{} - 布尔:
false - 以及所有的
nil(或 null)
对于结构体,没有空的定义,所以结构体从来不会返回默认值。
下面进行举例说明:
1.创建一个Chart
$ helm create helm-test
2.删掉系统自带的模板文件
[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*
3.新增模板文件,并填写Release.Name内置变量
[root@k8s-master01 ~]# vim /root/helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
trim: {{ .Values.myValue | default "none" }}
4.清空values.yaml文件,并添加以下内容
[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello"
5.进行验证,观察到输出结果为hello
[root@k8s-master01 templates]# helm install aaa /root/helm-test/ --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul 9 15:23:06 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: aaa-configmap
data:
trim: hello
6.重新修改values.yaml文件,将myValue值设置为空
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: ""
7.重新进行验证,观察到输出结果为none
[root@k8s-master01 templates]# helm install aaa /root/helm-test/ --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul 9 15:23:06 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: aaa-configmap
data:
trim: none