一、repeat

重复字符串多次:

repeat 3 "hello"

上述结果为: hellohellohello

下面进行举例说明:

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 | repeat 3 }}

4.清空values.yaml文件,并添加以下内容

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello"

5.进行验证,观察到输出结果为hellohellohello

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 15:02:04 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: hellohellohello

二、substr

获取字符串的子串,有三个参数:

  • start (int)
  • end (int)
  • string (string)
substr 0 5 "helloworld"

上述结果为: hello

下面进行举例说明:

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 | substr 0 5 }}

4.清空values.yaml文件,并添加以下内容

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "helloworld"

5.进行验证,观察到输出结果为hello

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 15:02:04 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

三、nospace

去掉字符串中的所有空格:

nospace "h e l l o"

上述结果为:hello

下面进行举例说明:

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 | nospace }}

4.清空values.yaml文件,并添加以下内容

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "h e l l o"

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

四、trunc

截断字符串

trunc 5 "hello world"

上述结果为: hello

下面进行举例说明:

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 | trunc }}

4.清空values.yaml文件,并添加以下内容

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "hello world"

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

五、contains

测试字符串是否包含在另一个字符串中

contains "cat" "catch"

上述结果为: true 因为 catch 包含了 cat

下面进行举例说明:

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 | contains "cat" }}

4.清空values.yaml文件,并添加以下内容

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
myValue: "catch"

5.进行验证,观察到输出结果为true

[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: true

六、hasPrefix and hasSuffix

hasPrefixhasSuffix 函数测试字符串是否有给定的前缀或后缀

hasPrefix "cat" "catch"

上述结果为: true 因为 catchcat