Sub topic 4
Configuring subcharts with values and globals
This chapter assumes you know Helm 3 basics (install, upgrade, release), can read Go templates in templates/, and have declared a dependency in Chart.yaml. Here you will see how a parent chart passes settings down to a subchart, and how global values are shared.
How values flow to a subchart
A subchart is a complete chart. It has its own values.yaml and its own templates. When Helm renders a release, it merges values in this order, with later sources winning:
- The subchart's own
values.yaml. - The parent chart's
values.yaml, under a key that matches the subchart's name. - Any values file passed with
-f. - Any
--setflags.
The subchart only sees its own slice of the parent's values. A parent key named cache becomes the root .Values inside the cache subchart. A parent value at cache.replicaCount is read as .Values.replicaCount in the subchart.
Overriding subchart values
Suppose the cache subchart defines replicaCount: 1 by default. The parent overrides it like this:
# values.yaml of the parent chart
global:
environment: staging
cache:
enabled: true
replicaCount: 2The cache.enabled key is not a setting the subchart needs. It matches the condition: cache.enabled field in Chart.yaml, which the next chapter section covers in detail. The subchart still receives it, but ignores it unless its templates read it.
You can also override from the command line:
helm install demo ./webapp --set cache.replicaCount=3To check what a subchart will receive, render the chart and inspect the output:
helm template demo ./webapp --set cache.replicaCount=3 | grep -n "replicas"Helm merges maps key by key. Setting cache.replicaCount does not erase other defaults in the subchart. Lists are different. A list in a higher-priority source replaces the whole list, so it is not merged item by item.
Global values
Some settings apply everywhere, such as an environment name or an image registry. Repeating them under every subchart key is error-prone. The reserved top-level key global solves this. Helm copies global into the parent and into every subchart, so each chart reads it as .Values.global.
In the parent's template:
env:
- name: APP_ENVIRONMENT
value: {{ .Values.global.environment | quote }}In a subchart's template, the same expression works without any change:
labels:
environment: {{ .Values.global.environment | quote }}A subchart can also define its own global block in its values.yaml to provide defaults. The parent's global values override those defaults.
| Aspect | Subchart-named key (`cache:`) | `global:` |
|---|---|---|
| Visible to | Only that subchart | Parent and all subcharts |
| Read in subchart as | `.Values.<key>` | `.Values.global.<key>` |
| Typical use | Replica count, image tag, resources | Environment name, registry, shared labels |
| Flows upward | No | No |
Values only flow downward
A subchart cannot read the parent's values, except through global. A subchart also cannot change them. If the parent needs a value that the subchart computes, you need another mechanism, such as import-values, which a later section covers.
This one-way flow explains a common mistake: a parent template that reads .Values.cache.replicaCount works, but a subchart template that reads .Values.cache.replicaCount gets nothing. Inside the subchart, the key is .Values.replicaCount.
Common errors
- The override has no effect. The key under the parent is spelled differently from the subchart's name (or alias). Helm does not warn about unknown keys. Check the
namefield inChart.yamland use exactly that. - A subchart template fails on
.Values.global.x. The parent did not defineglobal.x, and the subchart has no default. Give the subchart a default in its ownglobalblock, or usedefaultin the template. - A list override drops items. Lists are replaced as a whole. Copy the full list into your override.
- Stale subchart in
charts/. If you edit a local subchart but the packaged copy incharts/is old, rendering uses the old copy. Runhelm dependency update ./webappagain.
Exercise
A parent chart has a dependency named cache. Its values.yaml contains cache.replicaCount: 2 and global.environment: staging. How do the cache subchart's templates read each value?
Show answer
The replica count is .Values.replicaCount, because the cache key becomes the subchart's root values. The environment is .Values.global.environment, because global is copied unchanged into every subchart.
Exercise
You run helm install demo ./webapp --set cache.replicaCount=3, but the rendered Deployment still shows one replica. Name two likely causes.
Show answer
The dependency may be declared with an alias, so the key must use the alias instead of cache. Or the subchart's template may not read .Values.replicaCount at all, so the value is never used. A stale copy of the subchart in charts/ is a third possibility.