Sub topic 6
Using alias and import-values
This chapter assumes you can write a Chart.yaml with a dependencies list, run helm dependency update, and override subchart values from the parent values.yaml. Earlier chapters covered those steps. Here you will learn two optional fields on a dependency entry. alias lets you rename a dependency, which also lets you include the same chart more than once. import-values copies values from a subchart up into the parent.
Renaming a dependency with alias
By default, a subchart's values live under a key equal to the chart name. A dependency named cache is configured under cache:. The alias field replaces that key with a name you choose.
Aliases matter most when you need two copies of the same chart. Without them, two entries named cache would both read from the cache: key and could not be configured separately.
apiVersion: v2
name: webapp
description: Example web application chart
type: application
version: 0.1.0
appVersion: "1.0.0"
dependencies:
- name: cache
version: "~1.2.0"
repository: https://charts.example.com/stable
alias: session-cache
condition: session-cache.enabled
- name: cache
version: "~1.2.0"
repository: https://charts.example.com/stable
alias: page-cache
condition: page-cache.enabledEach alias is now an independent subchart instance with its own values key:
# values.yaml of the parent chart
session-cache:
enabled: true
replicaCount: 1
page-cache:
enabled: true
replicaCount: 3Two details are easy to miss:
- The alias replaces the chart name everywhere in the parent's values. A
conditionmust therefore use the alias (session-cache.enabled), notcache.enabled. - Keys with a hyphen cannot be reached with dot syntax in a template. Write
{{ index .Values "session-cache" "replicaCount" }}. Choosing aliases likesessionCacheavoids the problem.
Pulling values up with import-values
Normally values flow down: the parent sets them and the subchart reads them. import-values goes the other way. It copies values defined in a subchart into the parent's values, so the parent's templates can read them without hard-coding a duplicate.
There are two forms.
Importing from the exports key
The subchart author marks what is safe to share by placing it under a top-level exports key in the subchart's values.yaml:
# values.yaml of the cache subchart
exports:
connection:
port: 6379
protocol: tcpThe parent lists the name of a key under exports:
dependencies:
- name: cache
version: "~1.2.0"
repository: https://charts.example.com/stable
import-values:
- connectionThe contents of exports.connection are merged into the top level of the parent's values. A parent template can now use {{ .Values.port }} and {{ .Values.protocol }}.
Importing with child and parent paths
When the subchart has no exports key, or you want to choose the destination, use a child and parent pair. child is a path in the subchart's values. parent is where the copy lands in the parent's values:
dependencies:
- name: cache
version: "~1.2.0"
repository: https://charts.example.com/stable
import-values:
- child: service
parent: cacheServiceIf the subchart's values.yaml contains service.port: 6379, a parent template can read it as {{ .Values.cacheService.port }}.
A parent template can use the imported value to build a connection string:
env:
- name: CACHE_PORT
value: {{ .Values.cacheService.port | quote }}Common errors
| Symptom | Likely cause |
|---|---|
| `condition` has no effect on an aliased dependency | The condition path uses the chart name instead of the alias |
| Values set under `cache:` do nothing | The dependency has an alias, so the key must be the alias |
| `.Values.session-cache` fails to parse | The hyphen needs `index .Values "session-cache"` |
| Imported value is missing in the parent | The `exports` key name or the `child` path does not match the subchart's `values.yaml` |
| Imported value is not the one you expected | The parent defines the same key and overrides the import |
After changing Chart.yaml, run helm dependency update ./webapp so charts/ and Chart.lock match the new entries.
Exercise
A chart declares two dependencies named cache, with aliases sessionCache and pageCache. You want pageCache to be disabled by default. What do you add to the Chart.yaml entry and to values.yaml?
Show answer
In Chart.yaml, add condition: pageCache.enabled to the entry with alias: pageCache. In the parent values.yaml, set pageCache: with enabled: false. The condition path uses the alias, not the chart name cache.
Exercise
A subchart's values.yaml has service.port: 6379. Write the import-values entry that makes this available as .Values.cacheService.port in the parent chart.
Show answer
Add this under the dependency: import-values: followed by a list item with child: service and parent: cacheService. The whole service map is copied to cacheService, so .Values.cacheService.port is 6379.