Creating Library Charts
1. Defining Library Chart Type
Example: Library Chart.yaml
apiVersion: v2
name: common
description: Shared template helpers
type: library
version: 1.0.0
| Constraint | Reason |
|---|---|
| No installable resources | helm install fails on type=library |
Only _*.tpl + helpers | Manifest templates would be ignored |
2. Creating Reusable Templates
Example: Common deployment helper
{{- define "common.deployment" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "common.fullname" . }}
spec:
replicas: {{ .Values.replicaCount | default 1 }}
selector:
matchLabels: {{- include "common.selectorLabels" . | nindent 6 }}
template:
metadata:
labels: {{- include "common.selectorLabels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ include "common.image" . }}
{{- end -}}
3. Sharing Common Helpers
| Helper | Purpose |
|---|---|
common.fullname | Standard name truncation |
common.labels | Standard label set |
common.image | Registry/repo/tag/digest resolution |
common.affinity | Anti-affinity boilerplate |
4. Using Library as Dependency
Example: Consumer Chart.yaml
dependencies:
- name: common
version: "^1.0.0"
repository: oci://ghcr.io/acme/charts
5. Including Library Templates
6. Versioning Library Charts
| Rule | Apply |
|---|---|
| MAJOR | Breaking template name or scope changes |
| MINOR | New helpers added (backward compatible) |
| PATCH | Bug fixes in existing helpers |
7. Documenting Library Usage
| Doc | Content |
|---|---|
README.md | List of helpers + expected values |
| Inline tpl comments | Document required .Values keys |
| Example chart | Reference consumer chart in repo |
8. Organizing Library Templates
| File | Helpers |
|---|---|
_names.tpl | name, fullname |
_labels.tpl | labels, selectorLabels |
_deployment.tpl | Workload helpers |
_service.tpl | Service helpers |
9. Testing Library Templates
| Approach | Tool |
|---|---|
| Helper unit tests | helm-unittest on a wrapper application chart |
| Snapshot tests | Verify rendered output diff across PRs |
| Integration | Install consumer chart in kind/minikube |
10. Publishing Library Charts
| Target | Command |
|---|---|
| HTTP repo | helm package + helm repo index |
| OCI registry | helm push lib-1.0.0.tgz oci://ghcr.io/acme/charts |