Helm Charts for K8s from scratch – Part2

This post is the second part of the Helm Charts from scratch article. The first part handled the start until creating the first K8s resource and can be found here:

Helm Charts for K8s from scratch – Part1

Now let’s continue with parametrization. Already created in the first part, we will modify the values file.

Paramter (values) file

To replace hardcoded values in the ConfigMap resource, change Values.yaml with following content:

config1: Hello 
config2: Folks
config3: How are you?

Futher, change Configmap.yaml with following content in the data section (config3 used later) to use parameters:

data:
    config1: {{ .Values.config1 }}
    config2: {{ .Values.config2 }}

  • Now we read the values from a central file. The “.” after the first bracket pair indicates that the values file is in the same directory like the Configmap.yaml.

If you try to refer a not defined value, you would see something like following:

Overwriting values for environments

To simulate a little different value setting, e.g. for another environment, we can work with additional files representing the environment:

+ add file env.yaml with following content:

config2: World

With the help of the “-f” parameter (yes, only a single “-“) we can override the value for config2:

helm install mychart . --dry-run --debug -f env.yaml

Another option to override the values is direct in the terminal order with the “–set” parameter:

helm install mychart . --dry-run --debug -f env.yaml --set config1="Gruezi"

Build-in functions

Values in files can be modified with build-in function, simply useable with a pipe operator.

Change Configmap.yaml with following content:

data:
    config1: {{ .Values.config1 | upper}}
    config2: {{ .Values.config2 | quote}}

helm install mychart . --dry-run --debug -f env.yaml --set config1="Gruezi"

Template import

To clean up a file and outsource complex content, sections from templates can be imported in the file:

+ add file templates/_helpers.tpl with following content:

{{- define "upperquote" -}}
{{- .Values.config3 | upper | quote -}}
{{- end }}
  • Files starting with underscore are disregarded by helm in general, but can be used for functions etc.

Change configmap.yaml, add:

config3: {{ include "upperquote" . }}

helm install mychart . --dry-run --debug -f env.yaml --set config1="Gruezi"

Summary Notes

A summarizing notes file can display useful information after the chart was installed. Parameter values can be read and conditions are supported, for example.

+ add file /templates/NOTES.txt with following content:

Special text to view after install...
Config1: {{ .Values.config1 }}
Config2: {{ .Values.config2 }}

{{- if .Values.config3 }}
Config3 (in IF statement): {{ .Values.config3 }}
{{- end }}
{{- if .Values.config4 }}
You will never see this section until Config4 is defined
{{- end }}

helm install mychart . --dry-run --debug -f env.yaml --set config1="Gruezi"

Generate sample structure:

Finally, to have a look into a sample helm chart structure, use

helm create .

to generate a new helm chart folder structure and files.

For deeper diving, check https://helm.sh/

Enjoy your charts (-:

Leave a comment

Your email address will not be published. Required fields are marked *