一、Helm语法详解

1.1 Helm内置变量

对象可以通过模板引擎传递到模板中。 当然你的代码也可以传递对象。(我们在使用withrange语句时,会看到示例)。有几种方式可以在模板中创建新对象,比如说我们后面会看到的tuple功能。

对象可以是非常简单的:仅有一个值。或者可以包含其他对象或方法。比如,Release对象可以包含其他对象(比如:Release.Name)和Files对象有一组方法。

Release是你可以在模板中访问的顶层对象之一。

  • Release:Release对象描述了版本发布本身。包含了以下对象:
  • Release.Name: release名称
  • Release.Namespace: 版本中包含的命名空间(如果manifest没有覆盖的话)
  • Release.IsUpgrade: 如果当前操作是升级或回滚的话,该值将被设置为true
  • Release.IsInstall: 如果当前操作是安装的话,该值将被设置为true
  • Release.Revision: 此次修订的版本号。安装时是1,每次升级或回滚都会自增
  • Release.Service: 该service用来渲染当前模板。Helm里始终Helm
  • ValuesValues对象是从values.yaml文件和用户提供的文件传进模板的。默认为空
  • ChartChart.yaml文件内容。 Chart.yaml里的所有数据在这里都可以可访问的。比如 {{ .Chart.Name }}-{{ .Chart.Version }} 会打印出 mychart-0.1.0
  • Files: 在chart中提供访问所有的非特殊文件的对象。你不能使用它访问Template对象,只能访问其他文件。
  • Files.Get 通过文件名获取文件的方法
  • Files.GetBytes 用字节数组代替字符串获取文件内容的方法。 对图片之类的文件很有用
  • Files.Glob 用给定的shell glob模式匹配文件名返回文件列表的方法
  • Files.Lines 逐行读取文件内容的方法。迭代文件中每一行时很有用
  • Files.AsSecrets 使用Base 64编码字符串返回文件体的方法
  • Files.AsConfig 使用YAML格式返回文件体的方法
  • Capabilities: 提供关于Kubernetes集群支持功能的信息
  • Capabilities.APIVersions 是一个版本列表
  • Capabilities.APIVersions.Has $version 说明集群中的版本 (比如,batch/v1) 或是资源 (比如, apps/v1/Deployment) 是否可用
  • Capabilities.KubeVersionCapabilities.KubeVersion.Version 是Kubernetes的版本号
  • Capabilities.KubeVersion.Major Kubernetes的主版本
  • Capabilities.KubeVersion.Minor Kubernetes的次版本
  • Capabilities.HelmVersion 包含Helm版本详细信息的对象,和 helm version 的输出一致
  • Capabilities.HelmVersion.Version 是当前Helm语义格式的版本
  • Capabilities.HelmVersion.GitCommit Helm的git sha1值
  • Capabilities.HelmVersion.GitTreeState 是Helm git树的状态
  • Capabilities.HelmVersion.GoVersion 是使用的Go编译器版本
  • Template: 包含当前被执行的当前模板信息
  • Template.Name: 当前模板的命名空间文件路径 (e.g. mychart/templates/mytemplate.yaml)
  • Template.BasePath: 当前chart模板目录的路径 (e.g. mychart/templates)

下面以变量Release.Name进行举例说明:

1.创建一个Chart

$ helm create helm-test

2.删掉系统自带的模板文件

[root@k8s-master01 ~]# rm -rf helm-test/templates/*

3.新增模板文件,并填写Release.Name内置变量

[root@k8s-master01 ~]# cd /root/helm-test/templates
[root@k8s-master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"

4.测试模板渲染的内容,观察到以自己自定义的名字进行命名

[root@k8s-master01 ~]# helm install aaa helm-test/ --debug --dry-run
install.go:178: [debug] Original chart version: ""
install.go:195: [debug] CHART PATH: /root/helm-test

NAME: aaa
LAST DEPLOYED: Sun Jul  9 10:10:44 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
affinity: {}
autoscaling:
  enabled: false
  maxReplicas: 100
  minReplicas: 1
  targetCPUUtilizationPercentage: 80
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: nginx
  tag: ""
imagePullSecrets: []
ingress:
  annotations: {}
  className: ""
  enabled: false
  hosts:
  - host: chart-example.local
    paths:
    - path: /
      pathType: ImplementationSpecific
  tls: []
nameOverride: ""
nodeSelector: {}
podAnnotations: {}
podSecurityContext: {}
replicaCount: 1
resources: {}
securityContext: {}
service:
  port: 80
  type: ClusterIP
serviceAccount:
  annotations: {}
  create: true
  name: ""
tolerations: []

HOOKS:
MANIFEST:
---
# Source: helm-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: aaa-configmap
data:
  myvalue: "Hello World"

上面参数说明:

  • --dry-run :可以避免真正地创建和部署资源
  • --debug: 查看 Helm 在执行过程中的每个步骤和输出

1.2 Helm常用函数

1.2.1 字符串函数

2.8.2.1.1 trim

trim行数移除字符串两边的空格

trim "   hello    "

上述结果为: hello

下面进行举例说明

1.创建一个Chart

$ helm create helm-test

2.删掉系统自带的模板文件

[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*

3.新增模板文件,并填写Release.Name内置变量

[root@k8s-master01 ~]# cd /root/helm-test/templates
[root@k8s-master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  trim: {{ .Values.myValue | trim }}

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 helm-test]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 14:38:19 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:
  myvalue: "Hello World"
  trim: Hello
2.8.2.1.2 trimAll

从字符串中移除给定的字符

trimAll "$" "$5.00"

上述结果为:5.00 (作为一个字符串)。

下面进行举例说明:

1.创建一个Chart

$ helm create helm-test

2.删掉系统自带的模板文件

[root@k8s-master01 ~]# rm -rf /root/helm-test/templates/*

3.新增模板文件,并填写Release.Name内置变量

[root@k8s-master01 ~]# cd /root/helm-test/templates
[root@k8s-master01 templates]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  trim: {{ .Values.myValue | trimAll "$" }}

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

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

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

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 14:50:07 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: 5.00
2.8.2.1.3 trimSuffix

从字符串中移除后缀

trimSuffix "-" "hello-"

上述结果为: 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 | trimSuffix "-"  }}

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: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
2.8.2.1.4 trimPrefix

从字符串中移除前缀

trimPrefix "-" "-hello"

上述结果为: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 | trimPrefix "-" }}

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: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
2.8.2.1.5 upper

将整个字符串转换成大写

upper "hello"

上述结果为: 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 | upper }}

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: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
2.8.2.1.6 lower

将整个字符串转换成小写

lower "HELLO"

上述结果为: 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 | lower }}

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: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
2.8.2.1.7 title

首字母转换成大写

title "hello"

上述结果为: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 | title }}

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: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
2.8.2.1.8 untitle

移除首字母大写:untitle "Hello World" 会得到 hello world

下面进行举例说明:

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

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: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
2.8.2.1.9 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
2.8.2.1.10 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
2.8.2.1.11 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
2.8.2.1.12 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
2.8.2.1.13 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
2.8.2.1.14 hasPrefix and hasSuffix

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

  • hasPrefix:判断是否以什么开头
  • hasSuffix:判断是否以什么结尾
hasPrefix "cat" "catch"

上述结果为: true 因为 catchcat

2.8.2.1.15 quote and squote

该函数将字符串用双引号(quote) 或者单引号(squote)括起来

2.8.2.1.16 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
2.8.2.1.17 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
2.8.2.1.18 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
2.8.2.1.19 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
2.8.2.1.20 cat

使用cat将字符串使用空格进行拼接

2.8.2.1.21 print/println/printf

print:组合字符串

println:和print效果一样,但是会在末尾新添加一行

printf:格式化字符串,并打印输出

1.2.2 类型转换函数

2.8.2.2.1 atoi

作用: 将字符串转换为整数(String → Integer)。

使用场景: 当需要将字符串类型的数值(如 "8080")转换为整数进行算术运算时。

示例:

# 编写values.yaml
port: "80"

# 模板中使用
containerPort: {{ .Values.port | atoi }}

# 输出
containerPort: 80

注意: 如果输入不是有效的数字字符串(如 "abc"),会触发错误。

2.8.2.2.2 toString

作用: 将任意类型的数据转换为字符串(Any → String)。

使用场景: 确保变量以字符串形式输出(如拼接路径、环境变量值)。

示例:

# 将布尔值转换为字符串
enabled: {{ true | toString }}

# 输出
enabled: "true"

# 数字转字符串
count: {{ 42 | toString }}

# 输出
count: "42"
2.8.2.2.3 toStrings

作用: 将列表中的每个元素转换为字符串,返回字符串列表(List → List<String>)。

使用场景: 需要确保列表中的所有元素为字符串类型(如命令行参数列表)。

示例:

# values.yaml
args:
  - 8080
  - "prod"
  - true

# 模板中使用
args:
  {{- range .Values.args | toStrings }}
  - {{ . }}
  {{- end }}

# 输出
args:
  - "8080"
  - "prod"
  - "true"
2.8.2.2.4 toYaml

作用: 将数据结构转换为标准 YAML 格式的字符串(Any → YAML String)。

使用场景: 在模板中嵌入复杂的 YAML 配置(如 ConfigMap 数据、多行配置)。

示例:

# values.yaml
config:
  server:
    port: 8080
    debug: true
  database:
    url: "mysql://user:pass@db:3306/app"

# 模板中使用(ConfigMap)
data:
  app.yaml: |-
    {{ .Values.config | toYaml | indent 4 }}

# 输出
data:
  app.yaml: |-
    server:
      port: 8080
      debug: true
    database:
      url: mysql://user:pass@db:3306/app

注意:

  • 通常配合 indent 函数调整缩进。
  • 转换后的 YAML 字符串可能需要处理首尾的空行或缩进问题。

1.2.3 逻辑函数

2.8.2.3.1 and

作用: 逻辑“与”操作,所有条件为 true 时返回 true

示例:

# 判断两个条件同时成立
{{ if and (eq .Values.env "prod") (gt .Values.replicas 3) }}
  replicas: 5
{{ end }}
2.8.2.3.2 or

作用: 逻辑“或”操作,任意条件为 true 时返回 true

示例:

# 判断环境是开发或测试环境
{{ if or (eq .Values.env "dev") (eq .Values.env "test") }}
  debug: true
{{ end }}
2.8.2.3.3 not

作用: 逻辑“非”操作,反转布尔值。

示例:

# 当未启用缓存时,设置配置
{{ if not .Values.cache.enabled }}
  cacheSize: 0
{{ end }}

1.2.4 比较函数

2.8.2.4.1 eq

作用: 判断两个值是否相等。

示例:

# 检查环境是否为生产环境
{{ if eq .Values.env "prod" }}
  replicas: 5
{{ end }}
2.8.2.4.2 ne

作用: 判断两个值是否不相等。

示例:

# 当版本不是 "v1" 时生效
{{ if ne .Values.version "v1" }}
  useNewFeature: true
{{ end }}
2.8.2.4.3 lt

作用: 判断是否小于

2.8.2.4.4 le

作用: 判断是否小于等于

2.8.2.4.5 gt

作用: 判断是否大于

2.8.2.4.6 ge

作用: 判断是否大于等于

2.8.2.4.7 default

作用: 设置默认值

2.8.2.4.8 empty

作用: 判断是否为空

1.3 Helm管道符

Helm的管道符用"|"表示,可以用于按顺序完成一系列任务。

示例如下:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | repeat 5 | quote }}
  food: {{ .Values.favorite.food | upper | quote }}

1.4 Helm流程控制

控制结构(在模板语言中称为"actions")提供控制模板迭代流的能力。 Helm的模板语言提供了以下控制结构:

  • if/else, 用来创建条件语句
  • with, 用来指定范围
  • range, 提供"for each"类型的循环

除了这些之外,还提供了一些声明和使用命名模板的关键字:

  • define 在模板中声明一个新的命名模板
  • template 导入一个命名模板
  • block 声明一种特殊的可填充的模板块

1.4.1 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)

在所有其他条件下,条件都为true。

下面进行示例说明:

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

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

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
favorite:
#  drink: coffee
  food: pizza

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

[root@k8s-master01 helm-test]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 17:35:31 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:
  myvalue: "Hello World"
  drink: "tea"
  food: "PIZZA"

6.重新修改values.yaml文件,将drink: coffee前面的注释取消掉

[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
favorite:
  drink: coffee
  food: pizza

7.重新进行验证,观察到输出结果为coffee

[root@k8s-master01 helm-test]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 17:35:00 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:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"

1.4.2 控制空格

YAML认为空白是有意义的,因此管理空白变得很重要。Helm模板有些工具可以处理此类问题。

首先,模板声明的大括号语法可以通过特殊的字符修改,并通知模板引擎取消空白。{{-(包括添加的横杠和空格)表示向左删除空白, 而-}}表示右边的空格应该被去掉。 一定注意空格就是换行

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

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

[root@k8s-master01 ~]# > /root/helm-test/values.yaml
[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
favorite:
  drink: coffee
  food: pizza

5.进行验证,观察到输出结果中存在空行

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 17:47:08 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:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

  mug: "true"

6.重新修改configmap.yaml文件,添加-,{{-(包括添加的横杠和空格)表示向左删除空白

[root@k8s-master01 ~]# vim /root/helm-test/templates/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 }}

当然你也可以使用

7.重新进行验证,观察到输出结果中空行已经消失

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 17:51:19 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:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"

1.4.3 修改使用with的范围

with操作一般来控制变量范围,.是对当前作用域的引用。因此 .Values就是告诉模板在当前作用域查找Values对象。

with的语法与if语句类似:

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

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

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

注意:with后面的块只有在 PIPELINE 的值不为空时才会执行。

需要注意的是,在限定的作用域内,无法使用.访问父作用域的对象。错误示例如下:

  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  release: {{ .Release.Name }}
  {{- end }}

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

  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
  release: {{ .Release.Name }}

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

  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  release: {{ $.Release.Name }}
  {{- end }}

1.4.4 使用range操作循环

在Helm的模板语言中,在一个集合中迭代的方式是使用range操作符。

开始之前,我们先在values.yaml文件添加一个披萨的配料列表:

[root@k8s-master01 ~]# vim /root/helm-test/values.yaml
favorite:
  drink: coffee
  food: pizza
pizzaToppings:
  - mushrooms
  - cheese
  - peppers
  - onions

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

[root@k8s-master01 ~]# vim /root/helm-test/templates/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.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }} 

上面参数说明:

  • toppings: |-:声明的多行字符串

也可以使用$从父作用域访问Values.pizzaToppings列表。当模板开始执行后$会被映射到根作用域, 且执行过程中不会更改

[root@k8s-master01 ~]# vim /root/helm-test/templates/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 }}
  toppings: |-
    {{- range $.Values.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }}    
  {{- end }}

进行验证,观察到输出结果

[root@k8s-master01 templates]# helm install aaa /root/helm-test/  --dry-run
NAME: aaa
LAST DEPLOYED: Sun Jul  9 18:12:15 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:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  toppings: |-
    - "Mushrooms"
    - "Cheese"
    - "Peppers"
    - "Onions"

1.5 Helm模板定义

_helpers.tpl文件是Helm中一个非常重要的组成部分,通常用来定义可以被其他模板文件复用的辅助函数、片段或变量等。

在Helm中可以将常用的代码片段和逻辑放在helpers.tpl中,用来保持模板的整洁、避免重复,并且更容易管理和维护。

_helpers.tpl示例文件:

# 定义模板
{{- define "myapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}
{{- end }}

# 使用模板
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "myapp.fullname" . }}-api

1.6 Helm安装说明-NOTES.TXT

NOTES.TXT是一个Helm Chart的安装说明文件,在执行helm install或helm upgrade命令后,Helm会打印出对用户有用的信息,比如使用说明。

NOTES文件同样可以使用template语法进行生成,比如:

{{- printf "Thank you for installing %s.\nYour release is named %s.\nTo learn more about the release, try:\n  helm status %s\n  helm get all %s\n" .Chart.Name .Release.Name .Release.Name .Release.Name }}

生成后的内容如下:

Thank you for installing myapp.
Your release is named prod.
To learn more about the release, try:
  helm status prod
  helm get all prod