Helm Template Sub Chart Global


Helm Template Sub Chart Global

In this tutorial, we are going to discuss about global values in helm template sub chart. In the earlier tutorial we discussed about how to provide value for a subchart from the main chart. We need to refer to the individual sub chart name and provide the value or overwrite the value.

Whenever I wanted to have a common global value across the charts as well as its sub charts, it’s going to be difficult to inject or override the value for each and every subchart.

For that, we can use a concept called global value. I can provide a global value as a part of the main chart values file.

Global values are values that can be accessed from any chart or subchart by exactly the same name. Globals require explicit declaration. You can’t use an existing non-global as if it were a global.

The Values data type has a reserved section called Values.global where global values can be set.

Let me get in to the main chart values file and see through a small example.

Example

Within values file I’m going to add a key called global. Within that I am going to have a collection of values.

[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
global:
  orgdomain: waytoeasylearn.com

So I do have a key called orgdomain and a value waytoeasylearn.com. Now I can access this global value from the main chart or from any of the sub chart.

Let me modify the main chart template. As a part of the main chart template, I’m going to add value of domain.

[email protected]:~$ cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
{{- template "mychart.systemlables" $ }}
{{ include "mychart.version" $ | indent 4 }}
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}
  orgdomain: {{ .Values.global.orgdomain }}

So I’m going to print the key or domain and the value from global that is values.global.the key that I had provided within the values file.

Sub chart changes

Now let me go ahead and modify the subchart as well. For that, I’d be getting into the charts folder and then subchart folder. Let me go ahead and add this specific same value.

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

I’m adding the key and accessing the value using values.global.orgdomain, dot (.) means, that’s going to access the context specific to this particular template. And within the template, global will also be accessible.

Now I can get into the parent directory and do a dry run on the parent directory. That’s going to build the subcharts as well.

[email protected]:~$ helm install --debug --dry-run globaldemo ./mychart
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: globaldemo
LAST DEPLOYED: Mon May 24 16:36:34 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
LangUsed:
 - Python
 - Ruby
 - Java
 - Scala
global:
  orgdomain: waytoeasylearn.com
message: Welcome to waytoeasylearn
mysubchart:
  dbhostname: prodwaytoeasylearn-db
  global:
    orgdomain: waytoeasylearn.com
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: globaldemo-innerconfig
data:
  dbhost: prodwaytoeasylearn-db
  orgdomain: waytoeasylearn.com
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: globaldemo-configmap
  labels:
    name: "AshokKumar"
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: "globaldemo"
    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"
  orgdomain: waytoeasylearn.com
NOTES:
Thank you for installing mychart.

Your release is named globaldemo.

To learn more about the release, try:
  $ helm status globaldemo
  $ helm get all globaldemo
  $ helm uninstall globaldemo

So here I do have the global and the global is available in the main computed value as well as within the sub chart also. The sub chart we did not add it manually. It got injected from the main chart and the same thing should be reflected within generated yaml file as well. In the yaml file this is accessible.

Calculating the computed values automatically It’s going to inject all the values that is put within the global in the main chart into its subchart as well and because of that, the values will be accessible using the keys within the template as well.

The same value will be accessible within the main chart also.

So this is a quick demonstration on how to create or have a global value for the subchart so that I can have some common values across the main chart as well as its subcharts.

Helm Template Sub Chart Global


Scroll to top