1. 引言
Helm Chart 是管理 Kubernetes 应用程序的重要工具。它将部署一个应用所需的所有 Kubernetes 资源 打包成一个独立单元,使得部署更简单、可重复。
但与任何配置管理工具一样,Helm Chart 也可能存在错误,导致部署失败或配置错误。在本文中,我们将探讨多种验证 Helm Chart 内容的方法,确保我们的部署是可靠且避免常见陷阱。我们将介绍 helm lint、helm template、进阶的 schema 验证方法,以及一些最佳实践。我们开始吧!
2. 为什么需要验证 Helm Chart?
验证 Helm Chart 至关重要,原因如下:
✅ 确保配置正确、完整,避免因配置错误导致部署失败、行为异常甚至安全漏洞
✅ 帮助我们在开发早期发现错误,节省时间和资源
✅ 在 CI/CD 流水线中确保只部署合法配置,降低运行时出错和服务中断的风险
常见的 Helm Chart 问题包括语法错误、字段缺失、资源定义错误等。这些错误若未及时发现,可能在上线后才暴露出来,排查成本极高。
3. 使用 helm lint
helm lint 是一个简单但有效的工具,用于检查 Helm Chart 的结构和内容,识别可能导致部署失败的问题。
3.1. 验证单个 Chart
运行以下命令验证一个 Helm Chart:
$ helm lint ./mychart
==> Linting ./mychart
[ERROR] Chart.yaml: version is required
[INFO] Chart.yaml: icon is recommended
Error: 1 chart(s) linted, 1 chart(s) failed
输出说明:
- ✅
[ERROR] Chart.yaml: version is required
— 必须修复的严重问题 - ✅
[INFO] Chart.yaml: icon is recommended
— 建议性改进项
使用 helm lint 可以帮助我们在开发早期发现结构和内容上的问题,确保 Chart 符合最佳实践。
3.2. 验证目录下所有 Chart
若需验证多个 Chart(包括子 Chart),可以使用以下命令批量验证:
$ for chart in charts/*; do helm lint $chart; done
输出示例:
==> Linting charts/chart1
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
==> Linting charts/chart2
[ERROR] Chart.yaml: version is required
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 1 chart(s) failed
📌 最佳实践:
- ✅ 开发过程中定期运行 helm lint
- ✅ 修复所有 ERROR,并尽量处理 WARNING
- ✅ 在 CI/CD 流水线中集成 helm lint,实现自动化校验
4. 使用 helm template + Kubernetes 验证
helm template 命令可以本地渲染 Helm Chart 模板,生成 Kubernetes 清单文件。结合 Kubernetes 的 --dry-run
功能,可以验证生成的清单是否合法,而无需实际部署。
$ helm template ./mychart | kubectl apply --dry-run=client -f -
error: error validating "STDIN": error validating data: ValidationError(Deployment.spec.template.spec.containers[0]): unknown field "imagePullSecrets" in io.k8s.api.core.v1.Container
输出说明:
- ❌
ValidationError(Deployment.spec.template.spec.containers[0])
— Deployment 中第一个容器定义错误 - ❌
unknown field "imagePullSecrets"
— 字段位置错误
📌 优势对比:
方法 | 优点 | 缺点 |
---|---|---|
helm lint | 结构检查、快速 | 无法检查生成的 YAML 是否合法 |
helm template + kubectl apply --dry-run |
检查最终生成的 YAML 合法性 | 需要 Kubernetes 环境支持 |
5. 使用 kubeconform 进行 Schema 验证
kubeconform 是一个专门用于验证 Kubernetes 清单是否符合 JSON Schema 的工具。它可以与 helm template 配合使用,确保生成的 YAML 文件符合 Kubernetes API 的规范。
5.1. 基本验证
$ helm template ./mychart | kubeconform -strict
/path/to/manifest.yaml - Deployment my-deployment is valid
/path/to/manifest.yaml - Service my-service is valid
- ✅
-strict
表示开启严格模式,禁止未定义字段
5.2. 处理 CRD
如果 Chart 包含自定义资源(CRD),可以使用 -ignore-missing-schemas
忽略未定义的资源类型:
$ helm template ./mychart | kubeconform -strict -ignore-missing-schemas
/path/to/manifest.yaml - Deployment my-deployment is valid
/path/to/manifest.yaml - CustomResourceDefinition my-crd is ignored (schema missing)
5.3. 指定 Kubernetes 版本
确保生成的清单兼容特定版本的 Kubernetes:
$ helm template ./mychart | kubeconform -strict -kubernetes-version 1.18
📌 综合使用示例:
$ helm template ./mychart | kubeconform -strict -ignore-missing-schemas -kubernetes-version 1.18
6. 使用 values.schema.json 进行值校验
除了模板渲染和结构检查,还可以通过定义 values.schema.json 来校验 values.yaml
的格式,确保用户输入的参数合法。
6.1. 定义 Schema
{
"$schema": "https://json-schema.org/draft-07/schema#",
"properties": {
"image": {
"description": "Container Image",
"properties": {
"repo": { "type": "string" },
"tag": { "type": "string" }
},
"type": "object"
},
"name": { "type": "string" },
"port": { "minimum": 0, "type": "integer" },
"protocol": { "type": "string" }
},
"required": ["protocol", "port"],
"title": "Values",
"type": "object"
}
当执行 helm install
, helm upgrade
, helm lint
, helm template
等命令时,Helm 会自动校验 values.yaml
是否符合该 schema。
6.2. 嵌套属性与约束
{
"properties": {
"image": {
"properties": {
"repo": { "type": "string" },
"tag": { "type": "string" },
"pullPolicy": {
"type": "string",
"enum": ["Always", "IfNotPresent", "Never"]
}
},
"required": ["repo", "tag"]
},
"resources": {
"properties": {
"requests": {
"properties": {
"cpu": { "type": "string" },
"memory": { "type": "string" }
},
"required": ["cpu", "memory"]
}
}
}
},
"required": ["protocol", "port", "name"]
}
📌 优势:
- ✅ 保证
values.yaml
的结构和类型正确 - ✅ 提前发现非法配置,避免部署失败
- ✅ 提升 Chart 的可维护性和可读性
7. 使用 Go 模板编写自定义校验脚本
当静态 schema 无法满足复杂校验逻辑时,可以使用 Go 模板编写自定义校验脚本,实现动态逻辑校验。
7.1. 基本校验脚本
{{- if .Values.some_feature.enabled -}}
{{- if and (not .Values.some_feature.ip) (not .Values.some_feature.dns) -}}
When some_feature is enabled, either ip or dns must be provided.
{{- end -}}
{{- end -}}
- ✅ 当
some_feature.enabled == true
时,必须提供ip
或dns
7.2. 多条件校验
{{- if .Values.database.enabled -}}
{{- if and (not .Values.database.host) (not .Values.database.port) -}}
When the database is enabled, both host and port must be provided.
{{- else if not .Values.database.host -}}
When the database is enabled, host must be provided.
{{- else if not .Values.database.port -}}
When the database is enabled, port must be provided.
{{- end -}}
{{- end -}}
{{- if and .Values.cache.enabled (not .Values.cache.size) -}}
When cache is enabled, size must be provided.
{{- end -}}
7.3. 校验脚本组织方式
推荐将校验脚本统一放在 /validations
目录下,便于管理和维护:
chart/
├── templates/
│ ├── deployment.yaml
│ └── service.yaml
├── values.yaml
└── validations/
├── database_validation.yaml
├── feature_validation.yaml
└── cache_validation.yaml
📌 优势:
- ✅ 支持动态逻辑校验,如组合判断、依赖校验
- ✅ 可处理 JSON Schema 无法覆盖的复杂场景
- ✅ 提高 Chart 的健壮性,提前发现隐藏错误
8. 总结
在本文中,我们介绍了多种验证 Helm Chart 的方法,包括:
✅ 使用 helm lint 进行结构校验
✅ 使用 helm template + Kubernetes dry-run 验证生成的 YAML
✅ 使用 kubeconform 校验 Kubernetes 清单是否符合 schema
✅ 使用 values.schema.json 保证 values.yaml
的结构合法性
✅ 使用 Go 模板编写自定义校验逻辑,处理复杂依赖关系
通过将这些验证步骤集成到开发流程和 CI/CD 管道中,我们可以显著提升 Kubernetes 应用部署的可靠性与稳定性。