跳转至

第四章:Chart 模板

模板语法

Helm 使用 Go Template 语言,并添加了一些扩展功能。

基本语法

# 变量引用
name: {{ .Values.name }}

# 默认值
name: {{ .Values.name | default "default-name" }}

# 引号
name: {{ .Values.name | quote }}

# 条件判断
{{- if .Values.enabled }}
enabled: true
{{- end }}

# 循环
{{- range .Values.items }}
- {{ . }}
{{- end }}

内置对象

Release 对象

# Release 名称
name: {{ .Release.Name }}

# Release 命名空间
namespace: {{ .Release.Namespace }}

# Release 是否在升级
isUpgrade: {{ .Release.IsUpgrade }}

# Release 是否在安装
isInstall: {{ .Release.IsInstall }}

# Release 修订版本
revision: {{ .Release.Revision }}

# Release 服务
service: {{ .Release.Service }}

Values 对象

# 访问 values.yaml 中的值
replicas: {{ .Values.replicaCount }}
image: {{ .Values.image.repository }}
tag: {{ .Values.image.tag }}

Chart 对象

# Chart 名称
name: {{ .Chart.Name }}

# Chart 版本
version: {{ .Chart.Version }}

# 应用版本
appVersion: {{ .Chart.AppVersion }}

# Chart 描述
description: {{ .Chart.Description }}

Files 对象

# 获取文件内容
{{ .Files.Get "config.ini" }}

# 获取文件内容(Base64)
{{ .Files.Get "config.ini" | b64enc }}

# 列出文件
{{ range .Files.Lines "config.ini" }}
{{ . }}
{{ end }}

# Glob 模式匹配
{{ range .Files.Glob "configs/*" }}
{{ . }}
{{ end }}

Capabilities 对象

# Kubernetes 版本
kubeVersion: {{ .Capabilities.KubeVersion.Version }}

# API 版本检查
{{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" }}
apiVersion: networking.k8s.io/v1
{{- end }}

# Helm 版本
helmVersion: {{ .Capabilities.HelmVersion.Version }}

Template 对象

# 当前模板名称
name: {{ .Template.Name }}

# 模板基础路径
basePath: {{ .Template.BasePath }}

函数

字符串函数

# 转小写
name: {{ .Values.name | lower }}

# 转大写
name: {{ .Values.name | upper }}

# 首字母大写
name: {{ .Values.name | title }}

# 去除空白
name: {{ .Values.name | trim }}

# 截断
name: {{ .Values.name | trunc 63 }}

# 引号
name: {{ .Values.name | quote }}

# 替换
name: {{ .Values.name | replace "-" "_" }}

# 包含检查
{{- if contains "nginx" .Values.image.repository }}
isNginx: true
{{- end }}

类型转换

# 转字符串
value: {{ .Values.port | toString }}

# 转整数
value: {{ .Values.count | int }}

# 转 JSON
config: {{ .Values.config | toJson }}

# 从 JSON 解析
{{- $config := .Values.configJson | fromJson }}
value: {{ $config.key }}

列表函数

# 列表长度
count: {{ .Values.items | len }}

# 列表第一个元素
first: {{ first .Values.items }}

# 列表最后一个元素
last: {{ last .Values.items }}

# 列表包含
{{- if has "nginx" .Values.items }}
contains: true
{{- end }}

# 列表合并
items: {{ concat .Values.items1 .Values.items2 }}

# 列表去重
items: {{ .Values.items | uniq }}

字典函数

# 获取键
keys: {{ keys .Values.config }}

# 获取值
values: {{ values .Values.config }}

# 合并字典
config: {{ merge .Values.config1 .Values.config2 }}

# 深度合并
config: {{ deepCopy .Values.config | merge .Values.override }}

编码函数

# Base64 编码
data: {{ .Values.secret | b64enc }}

# Base64 解码
data: {{ .Values.encoded | b64dec }}

# SHA256
checksum: {{ .Values.config | sha256sum }}

# MD5
hash: {{ .Values.data | md5 }}

流程控制

if/else

{{- if .Values.enabled }}
enabled: true
{{- else if .Values.disabled }}
enabled: false
{{- else }}
enabled: default
{{- end }}

with

# with 块改变作用域
{{- with .Values.ingress }}
{{- if .enabled }}
ingress:
  host: {{ .host }}
{{- end }}
{{- end }}

range

# 遍历列表
{{- range .Values.hosts }}
- host: {{ . }}
{{- end }}

# 遍历字典
{{- range $key, $value := .Values.config }}
{{ $key }}: {{ $value }}
{{- end }}

# 遍历索引
{{- range $index, $host := .Values.hosts }}
- index: {{ $index }}
  host: {{ $host }}
{{- end }}

模板定义和引用

定义模板

{{/*
定义标签模板
*/}}
{{- define "mychart.labels" -}}
app: {{ .Values.name }}
version: {{ .Chart.Version }}
{{- end -}}

引用模板

metadata:
  name: {{ .Values.name }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}

模板嵌套

{{- define "mychart.selectorLabels" -}}
app: {{ .Values.name }}
{{- end -}}

{{- define "mychart.labels" -}}
{{ include "mychart.selectorLabels" . }}
version: {{ .Chart.Version }}
{{- end -}}

空白控制

# 移除左侧空白
{{- if .Values.enabled }}

# 移除右侧空白
{{- if .Values.enabled -}}

# 移除两侧空白
{{- if .Values.enabled -}}

# 缩进
{{- include "mychart.labels" . | nindent 4 }}

变量

# 定义变量
{{- $name := .Values.name -}}
name: {{ $name }}

# 在 range 中使用变量
{{- $root := . -}}
{{- range .Values.hosts }}
host: {{ . }}
name: {{ $root.Values.name }}
{{- end }}

高级技巧

条件渲染

# 等于
{{- if eq .Values.env "production" }}

# 不等于
{{- if ne .Values.env "development" }}

# 与
{{- if and .Values.enabled .Values.configured }}

# 或
{{- if or .Values.enabled .Values.configured }}

# 非
{{- if not .Values.disabled }}

# 大于
{{- if gt .Values.replicas 1 }}

# 小于
{{- if lt .Values.replicas 10 }}

空值检查

# 检查是否为空
{{- if empty .Values.config }}

# 检查是否非空
{{- if not (empty .Values.config) }}

# 检查键是否存在
{{- if hasKey .Values "config" }}

默认值

# 使用默认值
name: {{ .Values.name | default "default-name" }}

# 空值使用默认值
name: {{ .Values.name | default "" }}

# 条件默认值
name: {{ .Values.name | default (printf "%s-default" .Release.Name) }}

配置文件校验和

# 当 ConfigMap 变化时自动重启 Pod
annotations:
  checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}

资源名称生成

{{- define "mychart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

调试技巧

# 渲染模板
helm template myrelease mychart

# 调试模式
helm install myrelease mychart --dry-run --debug

# 只渲染特定文件
helm template myrelease mychart -x templates/deployment.yaml

# 验证语法
helm lint mychart

小结

本章学习了:

  • ✅ 模板基本语法
  • ✅ 内置对象
  • ✅ 函数使用
  • ✅ 流程控制
  • ✅ 模板定义和引用
  • ✅ 高级技巧

下一章

第五章:Chart 仓库 - 学习 Chart 仓库管理。