Helm Template Sub Chart


Helm Template Sub Chart

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

Helm Template Sub Chart

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

Example

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

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

ashok@waytoeasylearn:~/mychart/charts$ helm create mysubchart
Creating mysubchart

ashok@waytoeasylearn:~/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 make 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 will have all the sample manifest files for that particular sample chart.

ashok@waytoeasylearn:~/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 removed.

ashok@waytoeasylearn:~/mychart/charts/mysubchart/templates$ rm -rf *
ashok@waytoeasylearn:~/mychart/charts/mysubchart/templates$ ls

So everything is removed in the templates folder. So Within mychart I am having mysubchart as another sub chart. 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.

ashok@waytoeasylearn:~/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 make a file called configmap.yaml; let me add some sample content within it.

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

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

Now I can try only this specific sub chart. 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.

ashok@waytoeasylearn:~$ 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, 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 sub-charts.

Overriding Values from a Parent Chart

If I wanted to override this particular value from the parent directory, I could very well do that.

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

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

ashok@waytoeasylearn:~$ 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 also providing the value to the sub chart.

ashok@waytoeasylearn:~$ 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 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 is calculated within the sub chart because that is coming from the parent. So the computed value is being provided from the parent chart, which will feed into the mysubchart.

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

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

Helm Template Sub Chart
Scroll to top