Apache License 2.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

Kapitan:把配置当成可编译的产物

项目背景与痛点

配置管理这件事,在 2019 年前后开始变得尴尬。Helm 的 values 只有一层扁平结构,稍微复杂的场景就要靠 if 嵌套和 tpl 函数硬撑;Kustomize 的 patch 机制在“表达继承”这件事上先天残疾——你很难用 overlay 描述“三个集群共享 80% 配置,各自覆盖 20%”。更麻烦的是 Terraform 和 Kubernetes 各自为政:同一套环境参数要在 terraform.tfvars 和 values.yaml 里抄两遍,一旦漂移,线上事故就来了。至于 secrets,绝大多数团队仍然在老实地把明文塞进仓库,或者用一套独立的 Vault 流程,跟配置彻底割裂。

Kapitan 的定位很清楚:它不试图成为 Helm 或 Terraform 的替代品,它要做的是这两者之上的“配置编译器”。输入是一棵可继承的参数树和一批模板,输出是确定性的目录树——K8s 的 YAML、Terraform 的 tfvars、甚至任意纯文本文件。

核心技术揭秘

Kapitan 的执行模型是一个典型的多阶段流水线,理解它就能理解它所有能力的来源。

graph TD A[inventory:classes 与 targets] --> B[Reclass 继承与合并] B --> C[Target 参数树] C --> D{"input type"} D -->|"jsonnet"| E[Jsonnet VM 求值] D -->|"kadet"| F[Python 类生成] D -->|"jinja2"| G[模板渲染] E --> H[含引用占位符的中间产物] F --> H G --> H H --> I[引用解析器 ${ref:...}] I -->|"引用 secret"| J[Secrets 后端解密] I --> K[compiled/target 输出] J --> K K --> L[kapitan diff 与上游比对]

第一层是 inventory。Kapitan 复用了 reclass 的类继承模型:classes 声明依赖,parameters 逐层深合并,子类覆盖父类。这套机制的价值在于它天然支持多继承——你可以同时继承 production、eu-west、pci-compliant 三个维度,而不用写三层目录嵌套。规模上去以后 Python 实现的 reclass 会成为瓶颈,所以项目近年引入了 Rust 重写的 reclass-rs 作为可选后端。

第二层是 input type。这是 Kapitan 最有争议也最有意思的设计:它默认把 Jsonnet 作为一等公民(同时提供 Python 版的 Kadet,让你用类而不是模板生成结构)。选 Jsonnet 意味着放弃 YAML 的可读性,换来的是真正的图灵完备、可导入库、可单元测试。Jinja2 只是兜底方案。

第三层是 references,也是整个架构的技术难点。模板里可以写 ${ref:targets/us-east:parameters:cluster.endpoint} 这类跨 target 引用,Kapitan 必须在所有 target 都完成第一阶段渲染后,才统一做引用解析,否则无法处理 A 引 B、B 引 C 的链式依赖。解析器需要做递归展开、类型过滤(|int、|str)以及循环检测。Secret 引用还会触发后端解密(GPG、Vault、AWS SM、GCP KMS 等),并且对文件型 secret 记录输入哈希,避免无谓重加密造成 diff 噪音——这个细节很实用,但文档里几乎没人提。

功能亮点与差异

跨工具的统一配置源是它最硬核的差异点。Tanka 同样用 Jsonnet,但只服务 K8s;Terragrunt 只管 Terraform。Kapitan 允许一次 compile 同时产出 manifests、Terraform 输入和自定义文本,参数树只有一份。

Secrets 内联但不明文。${ref:...} 让 secret 的消费方式和普通参数完全一致,仓库里只留密文和引用,--reveal 时才解密。这套设计比“外挂一个 Vault Helm chart”要顺滑得多。

deterministic 输出。同一份输入永远产出同一份 YAML(键有序、无随机 ID),这让 git diff 变成了可靠的变更审查手段,配合 kapitan diff 还能直接对比线上现状。

应用场景与落地建议

适合的场景很明确:多集群、多环境、多云的平台团队,配置规模在几十到几百个 target 量级,且已经有独立的 CI 管道。

不适合的场景同样明确:小团队、单一集群、只想改几个镜像 tag 的,上 Kapitan 是杀鸡用牛刀,Kustomize 更合适。

落地时要注意几点:一是 Jsonnet 的学习成本被严重低估,团队里至少要有一两个人能读懂 lazy evaluation 和 std.mergePatch 的行为,否则出问题没人能 debug;二是 inventory 的合并顺序必须写进团队规范,深合并虽然方便,但排查“这个值到底从哪来”时,kapitan inventory 是唯一的救命稻草;三是 secret 后端的密钥分发要单独设计,别把 GPG key 也塞进同一个仓库。

综合评价

优点:设计思想干净,把“配置”抽象成了一次编译过程,而不是一堆模板加一堆变量;跨资源类型的能力在同类工具里罕见;secrets 与引用的结合是真需求驱动的设计;输出确定性对 GitOps 非常友好。

缺点:学习曲线陡,Jsonnet + reclass + references 三套心智模型叠加,新人上手周期以周计;文档质量参差,很多行为只能读源码确认;社区规模远小于 Helm 生态,遇到问题很难搜到答案;错误信息经常只抛 Jsonnet error 而不给行号上下文。

一句话总结:Kapitan 是给那些已经把配置管理当成软件工程来做的团队准备的工具——如果你愿意为可维护性付出前期学习成本,它会长期回报你;如果你只想尽快把应用部署上去,绕开它。

关于

GitHub 原始描述(英文)

Generic templated configuration management for Kubernetes, Terraform and other things

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

CUE
Dockerfile
Go Template
HTML
Jinja
Jsonnet
Makefile
Mustache
Nix
Python
Shell

Release

README

Kapitan: Configuration Management for Kubernetes, Terraform, and Infrastructure

Test, Build and Publish docker image Python Version Downloads Docker Pulls Releases Docker Image Size License

Kapitan logo

Kapitan is an open source configuration management tool for Kubernetes, Terraform, and complex infrastructure systems. It helps teams generate, organize, reuse, and validate configuration across environments using an inventory-driven model, templates (Jsonnet, Jinja2, Kadet), and integrations with Helm, Kustomize, CUE, and external references.

Kapitan provides native secrets management (GPG, AWS KMS, GCP KMS, Azure Key Vault, HashiCorp Vault) and is designed for Platform Engineering and GitOps workflows.


Quick start

The fastest way to try Kapitan is with the Kapitan Reference repository:

git clone https://github.com/kapicorp/kapitan-reference.git kapitan-templates
cd kapitan-templates
./kapitan compile

For a minimal project from a cookiecutter template:

pip3 install cruft
cruft create https://github.com/kapicorp/kapitan-reference --checkout cookiecutter --no-input

What is Kapitan?

Kapitan lets you model infrastructure configuration with reusable inventory classes and targets, then compile that data into manifests, scripts, documentation, and Terraform resources. Instead of copying values across Helm values files, Kustomize overlays, and Terraform variables, you define everything once in the Kapitan inventory and let each input type generate the files it needs.

Install Kapitan

docker run -t --rm -v $(pwd):/src:delegated kapicorp/kapitan -h

On Linux, add -u $(id -u) to preserve file permissions.

Pip

Kapitan requires Python 3.10 or newer.

pip3 install --user --upgrade kapitan

See the full installation guide for platform-specific steps.

What Kapitan does

Kapitan turns a hierarchical inventory and a set of input templates into compiled configuration files ready for deployment.

  1. Define reusable classes and per-environment targets in YAML.
  2. Write templates with your preferred tools.
  3. Run kapitan compile.
  4. Deploy the generated output from the compiled/ directory.

Supported input types

Native integrations

  • Secret management: GPG, HashiCorp Vault, AWS KMS, GCP KMS, Azure Key Vault
  • Remote dependencies: Git, HTTP, ORAS (OCI registry)
  • Validation: JSON Schema, TOML, YAML linting
  • GitOps-friendly: compiles to fully rendered, plain-text output

When to use Kapitan

  • You manage the same application across many environments (dev, staging, prod, regions) and want a single source of truth.
  • You need to reuse configuration fragments across targets without copy-paste.
  • You want to combine multiple templating tools in one pipeline.
  • You need native secret management embedded in the same configuration workflow.
  • You prefer a compile step that generates fully rendered output before deployment.

When another tool may be enough

  • Helm alone is sufficient if you only need to template a single chart with values files and do not share complex configuration across many services.
  • Kustomize alone is sufficient if your environment differences are mostly patches and overlays on a small set of bases.
  • Plain YAML with a CD tool is sufficient if you have very few environments and simple configuration with little reuse.
  • Terraform alone is sufficient if you only manage infrastructure resources and do not need a broader multi-language configuration layer.

Project status

Kapitan is actively maintained by KapiCorp and the open source community. Releases are published regularly with release notes. The project uses an MIT license.

Contributing

We welcome contributions. Please open an issue or pull request to get started.

Security

If you discover a security issue, please open a private security advisory or contact the maintainers directly.

Support

评论

暂无评论