Advanced Helm Charts- How to create the Templates
Topics to be covered
Built In Objects
Read values for Template
Set Values to Template
Template functions
Template Pipelines and Control flow if Else
How to create Helm Charts
helm create mychart ( it will create a new chart)
rm -rf mychart/templates* (for removing the default template)
Now we will learn to create the helm charts, updating the templates. And we will also learn how to install and uninstall the chart.
Under Templates create a configmap.yaml as below:
apiVersion: v1
kind: ConfigMap
metadata:
name: mychart-configmap
data:
myvalue: “Sample Config Map”
helm install helm-demo-configmap ./mychart (for installing the helm charts)
helm uninstall helm-demo-configmap (for uninstalling the helm charts)
Built In Objects
Now we are going to introduce the template using double flower brackets. We can also pass the values in values.yaml but we have some builtin objects.
Ref Link: Click Here
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name}}-configmap
data:
myvalue: “Sample Config Map”
helm install releasename-test ./mychart (for installing the chart using .Release.Name as defined above)
helm get manifest releasename-test (use to list the charts)
Note: The derivative is replaced with the name which we have given(releasename-test) . Above is the template derivative(.Release.Name) example using the built in objects.
Read values for Template
Now we will learn how to use our own values
Remove the default field’s values in value.yaml and add your own. And it will substitute the new values in the configmap.yaml
In values.yaml we pass this value cat mychart/values.yaml
costCode: CC6721
helm install — debug — dry-run firstdryrun ./mychart (we are using dryrun so that it will just reflect the changes instead of applying)
Till now we have learned how to read values from templates. Also we got to know how to use dry run.
Set Values to Template
In the below example we have replaced the costcode with some other value. As set is having the more precedence so it will replace the value in configmap.yaml
helm install — dry-run — debug — set costCode=00000 valueteg ./mychart/
We can use this when we want to use the custom value for a specific release.
Template functions
Function plays a very important role which dynamically changes the values
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name}}-configmap
data:
myvalue: “Sample Config Map”
costCode: {{ .Values.costCode }}
Zone: {{ quote .Values.infra.zone }}
Region: {{ quote .Values.infra.region }}
ProjectCode: {{ upper .Values.projectCode }}
In which upper is used to convert the values from lower to upper and quote is used to add the quotes
Here is the Reference Link : Click Here
Here are the values for the above template: cat mychart/values.yaml
costCode: CC6721
projectCode: aacbbff
infra:
zone: a,b,c
region: us-e
helm install — dry-run — debug valuesteg ./mychart
Below is the output after applying the chart
Template Pipelines
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name}}-configmap
data:
myvalue: “Sample Config Map”
costCode: {{ .Values.costCode }}
Zone: {{ quote .Values.infra.zone }}
Region: {{ quote .Values.infra.region }}
ProjectCode: {{ upper .Values.projectCode }}
pipeline: {{ .Values.projectCode | upper | quote }}
now: {{ now | date “2021–06–02” | quote }}
contact: {{ .Values.contact | default “1–1800–5665–890” | quote }}
In the now field, now is used for date we can use this in above format and in contact field, default pick those values which we pass in template if it is available or not in values.yaml
helm install — dry-run — debug valuesteg ./mychart
We have used the values.yaml from the Template functions in this case but will get some more info in our output
Control flow if Else
Using the configmap.yaml from Template Pipelines and added one more parameter in it
{{ if eq .Values.infra.region “us-e”}}ha: true {{ end }}
if the condition is true then will get this output
And if the condition fails so we will not get this extra parameter(ha:true) in the output.
Hope it Helps 🙂