✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Subcharts and Composition

Subcharts and Composition in Helm enable modular, reusable chart structures by allowing nested charts to be composed into a single, cohesive deployment unit.

Subcharts and Composition in Helm refer to the mechanism by which a Helm chart can include and manage other charts as dependencies, allowing modular, reusable, and composable packaging of Kubernetes resources. This approach enables complex applications to be structured into smaller, independently maintained units (subcharts) that are combined into a parent chart, facilitating easier maintenance, versioning, and configuration management.


Subcharts Overview

Subcharts are Helm charts that are embedded within another chart as dependencies. They reside in the charts/ directory of a parent chart or are specified as dependencies in the Chart.yaml file. By including subcharts, the parent chart can compose complex applications from multiple smaller components, each potentially representing a microservice, a database, a monitoring tool, or any other Kubernetes workload or service.

Subcharts have their own templates, values, and metadata, but when installed as part of a parent chart, they are rendered and deployed together, forming a unified application stack.

How Subcharts Are Added

Subcharts can be added in two primary ways:

  • Local subcharts: Placing a chart directory inside the parent chart's charts/ folder.
  • Remote dependencies: Declaring dependencies in the parent chart’s Chart.yaml under the dependencies section, specifying the name, version, and repository URL of the subchart.

When dependencies are declared, running helm dependency update downloads and places the subcharts into the charts/ directory.

Benefits of Using Subcharts

  • Modularity: Encapsulates functionality into discrete units.
  • Reusability: Common components can be reused across multiple charts.
  • Simplified Management: Parent charts can update subcharts independently.
  • Version Control: Each subchart maintains its own versioning.
  • Isolation: Templates and values are scoped, reducing conflicts.

Composition and Template Rendering

Composition in Helm means assembling multiple charts (via subcharts) into a single deployment package. During rendering, Helm processes the parent chart templates alongside all subcharts’ templates, merging their resources into one manifest.

Namespacing and Scoping

Subcharts have isolated namespaces for their templates and values by default. This means:

  • Template names are prefixed with the subchart name to avoid collisions.
  • Values for subcharts are nested under the subchart key in the values file.

For example, if a parent chart has a subchart named database, the values for that subchart are accessed under .Values.database.

Template Rendering Order

Helm renders the parent chart first and then processes subcharts in dependency order. Templates from subcharts cannot directly reference parent chart templates, but parents can override subchart behavior via values.


Values Management in Subcharts and Composition

Values control the configuration of both parent charts and subcharts. Helm distinguishes between:

  • Subchart values: Defined and used within the subchart scope.
  • Global values: Shared values accessible by parent and all subcharts.

Subchart Values

Each subchart has its own default values.yaml. When the parent chart is installed, the user can override subchart values by specifying them under the subchart key in the parent chart's values.yaml or via --set flags.

Example:

database:
  image:
    tag: "10.5"
  persistence:
    enabled: true

These values affect the database subchart but do not interfere with other subcharts.

Global Values

Global values are defined under .Values.global and are visible to all charts in the composition. This allows the parent chart to specify common settings, like image repositories, storage classes, or logging levels, that subcharts can reference.

Subcharts can access global values using .Values.global, enabling consistent configuration without duplication.

Value Precedence

When merging values, Helm applies the following precedence order:

  1. Command-line overrides (--set flags).
  2. Parent chart values.
  3. Subchart default values.

This system enables parent charts to customize or disable subcharts without modifying their source.


Library Charts and Reuse in Composition

Library charts are special Helm charts designed to share template snippets and functions among multiple charts without producing Kubernetes resources themselves. They facilitate reuse of common logic such as label templates, helper functions, or resource definitions.

Characteristics of Library Charts

  • Declared as type: library in their Chart.yaml.
  • Contain only templates and helper functions.
  • Not installable on their own.
  • Imported by other charts to use common template code.

Role in Composition

Library charts complement subcharts by providing reusable utilities that multiple subcharts and parent charts can consume. This improves maintainability and reduces duplication across a Helm chart ecosystem.


Best Practices for Subcharts and Composition

  • Use subcharts for logically separable components: Avoid making subcharts too granular or too monolithic.
  • Leverage global values sparingly: Overusing global values can complicate configuration management.
  • Override subchart values through parent chart: Avoid editing subchart code directly; use values overrides to customize behavior.
  • Keep library charts focused: Use them strictly for reusable template logic.
  • Document dependencies clearly: Maintain clear dependency declarations and version constraints.
  • Test composite charts thoroughly: Ensure that changes in subcharts do not break the overall application.

Summary

Subcharts and Composition in Helm enable modular, scalable, and maintainable packaging of Kubernetes applications by allowing charts to include other charts as dependencies. Through isolated scoping of templates and values, combined with the ability to override configurations at the parent level, Helm facilitates complex application deployment workflows. Library charts further enhance this model by enabling shared template logic, promoting DRY (Don't Repeat Yourself) principles across multiple charts. Proper management of subcharts, values, and dependencies ensures robust, flexible, and reusable Helm chart compositions.