Chart Development
Chart Development in Helm involves creating reusable, structured templates for deploying applications, enabling efficient and consistent Kubernetes management.
Chart Development refers to the structured process of creating, organizing, and maintaining Helm charts, which are packages that define, install, and upgrade Kubernetes applications. This process encompasses designing the chart's structure, writing reusable templates, managing configuration values, incorporating dependencies, and ensuring quality through testing and validation. The goal of Chart Development is to produce Helm charts that are reliable, reusable, configurable, and maintainable, facilitating consistent and repeatable deployment of Kubernetes workloads.
Chart Design
Chart Design involves planning the overall structure and components of the Helm chart to ensure clarity, modularity, and ease of use. This phase defines the chart's purpose, the Kubernetes resources it will manage, and how users will interact with it via configuration.
Chart Structure
A well-designed chart follows the standard Helm directory layout:
Chart.yaml: Metadata about the chart such as name, version, description.values.yaml: Default configuration values that users can override.templates/: Directory containing Kubernetes manifest templates.charts/: Directory for chart dependencies (subcharts).templates/_helpers.tpl: Template helper definitions for reuse.
A clear structure promotes maintainability and simplifies chart consumption.
Resource Modeling
Designing which Kubernetes resources the chart will manage (e.g., Deployments, Services, ConfigMaps) and how these resources interrelate. This includes identifying resource dependencies, lifecycle considerations, and the scope of customization needed.
Configuration Interface
Defining the parameters exposed to users via values.yaml and documenting their purpose and usage. Good design ensures parameters are intuitive, logically grouped, and cover the necessary customization points without exposing internal implementation details unnecessarily.
Chart Creation and Starters
Chart Creation is the initial phase of building the Helm chart files based on the design plan. It involves generating the skeleton chart and progressively adding templates and configuration.
Using Helm CLI for Chart Creation
The Helm CLI provides commands such as:
helm create mychart
which scaffolds a starter chart with sample manifests and structure. This enables developers to start with a functional baseline and customize as needed.
Developing from Starters and Examples
Leveraging existing charts, official starters, or open-source examples to accelerate development. Customizing these templates allows for rapid iteration while adhering to best practices.
Versioning and Metadata
Setting the chart version and maintaining metadata in Chart.yaml to support chart lifecycle management and compatibility with Helm repositories.
Chart Values
Chart Values are user-configurable parameters that enable customization of Kubernetes manifests generated by the chart templates.
Values.yaml Management
The values.yaml file contains default values for all configurable parameters. Good practice includes:
- Providing sensible defaults.
- Grouping related values logically.
- Including comments for clarity.
- Using nested structures for complex configurations.
Value Overrides
Users can override values using:
- Command-line flags (
helm install --set key=value). - Custom YAML files (
helm install -f myvalues.yaml).
Supporting flexible overrides is essential for chart usability.
Value Validation
Although Helm does not natively validate values, best practice includes:
- Documenting value constraints.
- Using schema validation introduced in Helm 3.5+ (
values.schema.json) to enforce types and required fields.
Chart Templates
Templates are the core of Chart Development, enabling dynamic generation of Kubernetes manifests using the Go template language.
Template Syntax and Functions
Templates use Go templating constructs such as {{ .Values }}, {{ if }}, {{ range }}, and built-in functions for control flow and string manipulation.
Reusable Template Helpers
Defining reusable snippets in templates/_helpers.tpl using the define and template directives to avoid duplication and improve maintainability.
Example:
{{- define "mychart.fullname" -}}
{{ .Release.Name }}-{{ .Chart.Name }}
{{- end -}}
Managing Conditional Resources
Templates often contain conditions to include or omit resources based on values, improving flexibility.
Output Resource Manifests
Templates produce YAML manifests for Kubernetes resources such as Deployments, Services, ConfigMaps, Secrets, etc., which Helm applies to the cluster.
Subcharts and Composition
Managing complex applications often requires composing multiple charts through dependencies known as subcharts.
Defining Dependencies
Dependencies are declared in Chart.yaml under the dependencies field, specifying the name, version, and repository of subcharts.
dependencies:
- name: redis
version: 14.4.0
repository: https://charts.bitnami.com/bitnami
Subchart Values Isolation
Subcharts have their own values.yaml and templates, but their values can be set or overridden from the parent chart using a nested structure.
Chart Composition Strategies
- Aggregating multiple distinct components as subcharts.
- Using umbrella charts to manage large applications.
Chart Hooks
Hooks are special templates that execute Kubernetes resources at specific points in a release lifecycle, enabling advanced workflows.
Hook Lifecycle Events
Common hook events include:
pre-installpost-installpre-upgradepost-upgradepre-deletepost-delete
Use Cases for Hooks
- Running database migrations before application deployment.
- Creating or cleaning up resources that do not persist across upgrades.
- Performing validation or initialization tasks.
Hook Annotations
Hooks are defined by adding annotations to resource metadata:
metadata:
annotations:
"helm.sh/hook": pre-install
Proper use of hooks requires careful handling to avoid conflicts and ensure idempotency.
Custom Resource Definitions in Charts
Charts may need to install or manage Kubernetes Custom Resource Definitions (CRDs).
CRD Management
CRDs must be installed before any custom resources are created. Helm provides two approaches:
- Placing CRDs in the
crds/directory to be installed separately and retained after uninstall. - Including CRD manifests as regular templates, which can cause upgrade/delete issues.
Best Practices
- Use the
crds/directory for CRDs. - Avoid modifying CRDs with Helm upgrades.
- Document CRD versions and compatibility.
Chart Tests
Chart tests validate that the chart deploys correctly and behaves as expected.
Test Hooks
Helm supports test hooks that run pods to verify the installation:
metadata:
annotations:
"helm.sh/hook": test
Integration and Smoke Tests
Writing test pods or jobs that perform sanity checks on deployed resources, e.g., connectivity tests, API availability.
Automated Testing Tools
Using tools like helm unittest or CI pipelines to run tests automatically on chart changes.
Chart Linting
Linting is the process of static analysis to detect common errors and enforce best practices.
Helm Lint Command
helm lint checks chart structure, syntax errors in templates, and basic validation of Chart.yaml and values.yaml.
Custom Lint Rules
Integrating additional linters or custom scripts to enforce organization-specific policies.
Continuous Integration
Incorporating linting into CI pipelines to ensure chart quality before publishing.
Render Validation
Render Validation involves verifying that templates produce valid Kubernetes manifests with expected configurations.
Template Rendering
Using helm template to render manifests locally for inspection without deploying.
Schema Validation
Combining rendered manifests with Kubernetes schema validation tools to detect invalid resource definitions.
Value Combination Testing
Validating multiple combinations of values to ensure templates handle different configurations gracefully.
Chart Documentation
Comprehensive documentation is vital for chart usability and adoption.
README and Usage Instructions
Providing clear installation, upgrade, and configuration instructions.
Value Descriptions
Documenting all configurable values with explanations, acceptable ranges, and examples.
Examples and Use Cases
Including practical examples showing common deployment scenarios.
CHANGELOG and Versioning
Maintaining a changelog to inform users of updates, fixes, and breaking changes.
Chart Development is a multifaceted discipline that combines Kubernetes knowledge, templating expertise, and software engineering practices to create Helm charts that empower reliable, scalable, and configurable Kubernetes application deployments.