Sub topic 14
Subcharts, dependencies and global values
A chart can depend on other charts. This chapter shows how to declare a dependency, how values reach it, and how global values are shared. It assumes you have read the earlier chapters on values.yaml, .Values and named templates in _helpers.tpl.
Declaring a dependency
List dependencies in the parent chart's Chart.yaml. Each entry names a chart, a version, and where to find it.
dependencies:
- name: common
version: 0.1.0
repository: "file://../common"
condition: common.enabledThe file:// repository points at a chart in a local directory. That directory must contain its own Chart.yaml with name: common and version: 0.1.0. For a published chart, use a repository URL such as https://charts.example.com.
Dependencies are not fetched automatically. Run this from the parent chart's directory:
helm dependency update ./webappHelm packages each dependency into charts/ as a .tgz file and writes Chart.lock, which records the exact versions it resolved. helm dependency build installs the versions recorded in Chart.lock instead of resolving them again.
How values reach a subchart
A subchart is a complete chart with its own values.yaml. Its templates see only their own values, so .Values.replicaCount inside the subchart is not the parent's replicaCount.
The parent changes a subchart's values by using a top-level key that matches the subchart's name:
common:
enabled: true
replicaCount: 1
global:
environment: stagingInside the common subchart, .Values.replicaCount is 1. Values in the parent's common: section override the subchart's own defaults. The same override works from the command line with --set common.replicaCount=1.
Global values
The global key is the exception to the scoping rule. Every chart in the tree, parent and subcharts, can read it as .Values.global:
environment: {{ .Values.global.environment | default "dev" | quote }}Use global for settings that all charts share, such as an environment name, an image registry or a label prefix. Keep it small. Many globals make a chart hard to follow, because a reader cannot tell which subchart depends on which global.
Turning a subchart on or off
The condition field takes a values path. If common.enabled is false, Helm skips the subchart. If the path is not set at all, Helm ignores the condition and includes the subchart. Set the value explicitly in values.yaml so the behavior is clear.
Common errors
found in Chart.yaml, but missing in charts/ directory: you did not runhelm dependency update.- A subchart ignores your override: the key in the parent must match the subchart's
nameexactly, not the directory name. - A subchart cannot see a parent value: move the value under
global, or pass it under the subchart's key. - Stale
charts/*.tgzfiles: after you edit a local subchart, runhelm dependency updateagain.
Exercise
A parent chart sets registry: example.io at the top level of its values.yaml. A template in the common subchart reads .Values.registry and gets nothing. Give two ways to fix this.
Show answer
Subchart templates only see their own values and global. Either move the setting to global.registry and read .Values.global.registry in the subchart, or put it under the subchart's key as common.registry so the subchart sees it as .Values.registry.