Helm Template Sub Chart


Helm Template Sub Chart

In this tutorial we are going to discuss about helm template sub chart. Charts is a separate entity where It will be having collection of templates and values.

Charts can be packaged together, shared it within a repository as well as send it as a package to the external world. Within charts, I can have n number of sub charts. Again, the sub charts will be having a collection of templates and values and the same hierarchy how we have within the charts.

Example

Let’s understand through a example. Let me get into the sample chart that we have, within that we do have charts folder. I’m going to get into the charts folder, within charts folder I’m going to create a sub chart.

The name of the subchart I’ll give it is mysubchart and I will use helm create command, helm create mysubchart.

[email protected]:~/mychart/charts$ helm create mysubchart
Creating mysubchart

[email protected]:~/mychart/charts$ ls
mysubchart

So within charts it’s going to create a folder mysubchart and like this I can have n number of charts within it. And within the folder by default it is going to create a project. Within that it’s going to create the collection of required folders. And it’s going to create the sample templates And that will be available within the templates.

So within templates, I’m going to have all the sample manifest file for that particular sample chart.

[email protected]:~/mychart/charts/mysubchart/templates$ ls
NOTES.txt  _helpers.tpl  deployment.yaml  hpa.yaml  ingress.yaml  service.yaml  serviceaccount.yaml  tests

Now I’m going to remove all this so that I can create right from the beginning. Everything is a removed.

[email protected]:~/mychart/charts/mysubchart/templates$ rm -rf *
[email protected]:~/mychart/charts/mysubchart/templates$ ls

So everything is a removed in templates folder. So Within mychart I am having mysubchart as another subchart. Within mysubchart, I’m going to add some values. Now I delete all the values within the values file as well. Here I’m going to add a sample key value pair.

[email protected]:~/mychart/charts/mysubchart$ cat values.yaml 
dbhostname: waytoeasylearn-db

Now let me go ahead and create a template. Right now I don’t have any files. I’m going to create a file called configmap.yaml, let me add some sample content within it.

[email protected]:~/mychart/charts/mysubchart/templates$ cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-innerconfig
data:
  dbhost: {{ .Values.dbhostname }}

So this is going to create a config map where the name, it will have the release name and as a part of the dbhost key its going to have the value from the values file so it will read the value with the key dbhost name.

Now I can try only this specific subchart. Let me get into the parent directory mychart. Here I do have the project mychart. I’m going to do a dry run on mysubchart.

[email protected]:~$ helm install --debug --dry-run mysubchart mychart/charts/mysubchart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart/charts/mysubchart
NAME: mysubchart
LAST DEPLOYED: Mon May 24 15:30:23 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
dbhostname: waytoeasylearn-db
HOOKS:
MANIFEST:
Source: mysubchart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysubchart-innerconfig
data:
  dbhost: waytoeasylearn-db
Explanation

If you observe over here, here I have given the entire path within the chart that mysubchart directory I have provided.

So it’s going to do a dry run and process the values file and generate the manifest file and it replaced the dbhost with the value that we provided as a part of the values file.

So this is going to work as a separate entity. And like this, I can have n number of subcharts.

Overriding Values from a Parent Chart

In case, if I wanted to override this particular value from the parent directory I can very well do that.

Let me give a try. Within mychart, I am going to update the values file. So within values file I’m going to add or inject values to the subchart.

So I’m having a collection, mysubchart the key dbhostname. And I’m providing the value as prodwaytoeasylearn-db as the value.

[email protected]:~$ cat mychart/values.yaml 
message: Welcome to waytoeasylearn
website: www.waytoeasylearn.com
owner:
  name: Ashok Kumar
  place: Hyderabad
  qualification: mca
tags:
  programming: java
  devops: kubernetes
  deployment: helm
  bigdata: hadoop
LangUsed:
 - Python
 - Ruby
 - Java
 - Scala
mysubchart:
  dbhostname: prodwaytoeasylearn-db 

Now, let me go ahead and build the mychart and see how the value is getting generated. So this time I’m going to do a dry run against mychart and mychart is providing the value to the subchart as well.

[email protected]:~$ helm install --debug --dry-run mysubchartoverride ./mychart
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: mysubchartoverride
LAST DEPLOYED: Mon May 24 15:41:03 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
LangUsed:
 - Python
 - Ruby
 - Java
 - Scala
message: Welcome to waytoeasylearn
mysubchart:
dbhostname: prodwaytoeasylearn-db
global: {}
owner:
  name: Ashok Kumar
  place: Hyderabad
  qualification: mca
tags:
  bigdata: hadoop
  deployment: helm
  devops: kubernetes
  programming: java
  website: www.waytoeasylearn.com 
HOOKS:
MANIFEST:
Source: mychart/charts/mysubchart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysubchartoverride-innerconfig
data:
  dbhost: prodwaytoeasylearn-db
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysubchartoverride-configmap
  labels:
    name: "AshokKumar"
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: "mysubchartoverride"
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: "Helm"
    app_name: mychart
    app_version: "0.1.0"
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  app_name: mychart
  app_version: "0.1.0"
NOTES:
Thank you for installing mychart.
Your release is named mysubchartoverride.
To learn more about the release, try:
  $ helm status mysubchartoverride
  $ helm get all mysubchartoverride
  $ helm uninstall mysubchartoverride

If you observe carefully in the above output you will find the computed values for mysubchart.

Source: mychart/charts/mysubchart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysubchartoverride-innerconfig
data:
  dbhost: prodwaytoeasylearn-db

It is having prodwaytoeasylearn-db. This is the value that it calculated within the sub chart because that is coming from the parent. So the computed value, it’s being provided from the parent chart and that will be fed into the mysubchart.

This is how I can override the value that needs to be provided to the subchart as well and there is another concept called global values that we will be discussing it in the next tutorial.

So in a quick summary we have seen how to create subchart in helm chart and override the value of the subchart from the parent chart.

Helm Template Sub Chart


Scroll to top