Helm Template Sub Chart Global


Helm Template Sub Chart Global

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

Helm Template Sub Chart Global

Whenever I wanted to have a common global value across the charts and its sub-charts, it would be difficult to inject or override the value for each sub chart.

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.

We can access Global values from any chart or sub chart by the same name. Globals require explicit declaration. So you can’t use an existing non-global as if it were global.

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

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

Example

Within the 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 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 the domain’s value.

[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 sub chart as well. For that, I’d be getting into the charts folder and then the sub chart 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 sub-charts 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 and within the sub chart. The sub chart we did not add manually. It got injected from the main chart and should reflect the same thing within the generated YAML file. In the YAML file, this is accessible.

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

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

So this is a quick demonstration of how to create or have a global value for the sub chart so that I can have some common values across the main chart and its sub-charts.

Helm Template Sub Chart Global

Scroll to top