Helm — Data Sharing Between Parent and Child Chart
Data exchange between parent and child chart in helm
Previous Article: Helm — Dependencies
In helm, we can exchange data between the child chart and the parent chart. And also we can manipulate child chart values from parent chart. The various ways to achieve that are discussed in this article.
Overriding Values from a Parent Chart
In helm, a sub-chart/child chart is also considered “stand-alone”, which means a sub-chart/child-chart can never explicitly depend on its parent chart. For that reason, a sub-chart cannot access the values of its parent. But a parent chart can override values for sub-charts/child charts.
Let’s see how we can overwrite some configurations of the child chart.
Assume that we have a dependency defined in the ‘parentChart/ Chart.yaml’
# parentchart/Charts.yamldependencies:
- name: mysql
version: "9.3.4"
repository: https://charts.bitnami.com/bitnami
To overwrite or pass values to the MySQL child chart defined as a dependency, we have to use the ‘parentChart/values.yaml’ file to define the values we wanted to pass into the child chart. For overriding values into the MySQL chart, we have to find out what type of values we are allowed to override.
helm show values [CHART]
>> helm show values bitnami/mysql---# subChart/values.yaml...
primary:
service:
type: ClusterIP
...
In the above demonstration, we can see that by default MySQL chart will configure a ClusterIP type of service after the installation.
But if we want to override the above configurations we can set the values in the ‘parentchart/values.file’
Define the values we want to override in the child chart (MySQL) :
# parentchart/values.yaml
...
...mysql:
primary:
service:
type: NodePort
See the following image for a better understanding:
Now, if we install the parentChart then as a dependency MySQL chart will be installed with a NodePort-type service.
Make Child Chart Data Available to Parent Chart
There are 2 ways to make sub-chart/child-chart values available on the parent chart.
● Using Exports and Imports-Values :
If a child chart’s values.yaml
file contains an exports
field, Then its contents can be imported directly into the parent's values by specifying the import-values
field under the ‘dependencies ’ block of the ‘parentChart/
Chart.yaml’
# subChart/values.yaml...
exports:
data:
db_info:
username: admin
dbname: userinfo
Then import the values from the sub-chart by defining import-values
field under the ‘dependencies ’ block of the ‘parentChart/Chart.yaml’
# parentChart/Chart.yamldependencies:
- name: mysql
version: "9.3.4"
repository: https://charts.bitnami.com/bitnami
import-values:
- data
Now the above-defined child chart data can be used by the parent chart:
# parentChart/templates/configMaps.yamlapiVersion: v1
data:
{{- toYaml .Values.db_info | nindent 2 }}
kind: ConfigMap
metadata:
creationTimestamp: null
name: test
If we generate the template of the parent chart then we will get an output like this:
>> helm template webserver ~/webserver---
apiVersion: v1
data:
dbname: userinfo
username: admin
kind: ConfigMap
metadata:
creationTimestamp: null
name: test
Following is the visualization of the data sharing between the child chart to the parent chart.
● Using child and parent mapping under the “imports-values” field :
# parentChart/Chart.yamldependencies:
- name: mysql
version: "9.3.4"
repository: https://charts.bitnami.com/bitnami
import-values:
- child: < import-source >
parent: < export-destination > - child: < import-source >
parent: < export-destination >
...
To access values that are not under the exports
key of the child chart's values, we can still access child chart data by specifying the source key of the values to be imported in the child
field and the destination path in the parent
field.
Suppose, the child chart contains the following entries in its values.yaml file :
# subChart/values.yaml...config:
data:
db_info:
username: admin
dbname: userinfo
As we can see that there is no exports
keyword specified. To access the above-defined data from the parent chart we can configure the dependencies block in the following way :
# subChart/values.yamldependencies:
- name: mysql
version: "9.3.4"
repository: https://charts.bitnami.com/bitnami
import-values:
- child: config.data.db_info
parent: configmaps
Now we can access child chart data from the parent chart though the child data is not specified under the exports
keyword.
# parentChart/templates/configMaps.yamlapiVersion: v1
data:
{{- toYaml .Values.configmaps | nindent 2 }}
kind: ConfigMap
metadata:
creationTimestamp: null
name: test
In both cases, the parent chart always assumes that the data it is accessing resides in its own values.yaml file (Though it is actually coming from the child’s values.yaml file )
Global Chart Values
Global values are values that can be accessed from any chart or sub-chart by exactly the same name. Globals require explicit declaration using global
keyword.
Suppose a global value is defined in the parent’s values.yaml file:
# parentChart/values.yamlglobal:
env: prod
Now, this value can be accessed across any chart and sub-charts.
# subChart/templates/configmaps.yamlapiVersion: v1
data:
name: {{ .Values.global.env }}
kind: ConfigMap
metadata:
creationTimestamp: null
name: test
Sharing Templates with Subcharts
Parent charts and sub-charts can share templates. Any template defined in any chart is available to other charts.
Suppose a named template is defined in the parent’s chart :
{{- define "parentchart.labels" -}}
app: {{ .Chart.Name }}
{{- end }}
The above-defined template can be used by any of the sub-charts. A named template can be embedded using include
or template
keyword.
# inside the sub-chart{{- include "parentchart.labels" . | nindent 8 }}
To learn more about the Named Templates, read this article: — Helm — Named Templates.
If you found this article helpful, please don’t forget to hit the Follow 👉 and Clap 👏 buttons to help me write more articles like this.
Thank You 🖤