Helm – Part-4: Helm Chart Template Functions and Pipelines with examples.

Helm is a package manager for Kubernetes. Like yum and apt for the Linux. As the usage of the Kubernetes goes higher for seamless application, learning Helm seems important to fit in the future of Software Development. Hence we are going to see the series of tutorials on Helm in digitalvarys.com. In our previous article, we discussed what is Helm Chart Built-in Objects and Values For Template. Along with that, we have discussed a sample Deployment using The Helm chart. In this article, we will discuss what is Helm Template Functions and Pipelines with examples.

Workspace Creation

Create a folder structure using CLI as the first step and it will create the folders as we discussed in our previous article.

  • Chart.yaml
  • values.yaml
  • template/
  • charts/

Then, we are going to create a sample deployment by deploying ConfigMaps into the Kubernetes cluster.

Template Functions

Edit the value file and add some additional values.

projectNumber: 12345
systemuser: cicd-admin
infra:
  availability-zone: 1, 2, 3
  regions: us-east, us-west, ap-south

And those can be accessed by using the derivatives in the Template file as discussed in our previous article.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  regions: {{ upper .Values.regions }}

So, Here quote and upper are the inbuilt helm template function. Where quote function will add and surrounded with quotes on the value that is going to derive from the values.yaml file. So, That will look like ‘1, 2, 3’. And the upper will convert the derived variable into Uppercase. So, That will look like US-EAST, US-WEST, AP-SOUTH. So like this we have many functions and they are available in Go Templates (https://pkg.go.dev/text/template#hdr-Functions) and Sprig Templates (https://masterminds.github.io/sprig/). A few Important functions are:

  • trim – This will remove any space in the string value.
  • add – This will add the values and if you call this function, you need to pass minimum two integer.
  • mul – This will multiply the values and if you call this function, you need to pass minimum two integer Float Variables.
  • addf – This will add the values and if you call this function, you need to pass minimum two Float Variables.
  • mulf – This will multiply the values and if you call this function, you need to pass minimum two Float Variables.
  • now – This will show current date. Use this in conjunction with other date functions.
  • date – This will formats the date as YEAR-MONTH-DAY
  • fromJson – decodes a JSON document into a structure.
  • toJson – encodes an item into a JSON string
  • toPrettyJson – encodes an item into a pretty (indented) JSON string.
  • toRawJson – encodes an item into JSON string with HTML characters
  • b64enc – Encode with Base64
  • b64dec – decode with Base64
  • list – Create a list of integers, it expects list of values.
  • uniq – Generate a list with all of the duplicates removed.
  • env – This one reads an environment variable:

So, Run a --dry-run and check the manifest and you can find the values are replaced as per the function we mentioned in the values section.

Template Pipeline.

Pipeline and Default Values are another useful feature in Templating. Pipelines are derived from the Unix Pipeline concept to chaining together a set of commands and functions to process in sequences. Let us see how this is done.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  regions: {{ upper .Values.regions }}
    derived-variable: {{ now | date “2021-09-21” | quote  }}

In the above example, derived-variable is an example of the pipeline concept. For example, now is the function that will give you the current DateTime then it will be formatted in the given format and then the function quote will add the quotes to the DateTime.

Now, let us see the default values in the pipeline concept. Assume we are going to add the environment name from the values file and add the default value in the pipeline.

Environment: {{ .Values.Env | default “stage” | quote}}

So in the above line, the Env value will be taken from the Variable file. And if the value file is not having the value in the file, then the default value will be appended. That is stage. If you add the Env Value while running the command, That will be appended in the template file but not the default. Then, the same will be quoted by quote function.

So, Run a --dry-run and check the manifest and you can find the values are replaced as per the function we mentioned in the values section.

Conclusion

In this article, we discussed discuss what is Helm Template Functions and Pipelines with examples. In our upcoming article, we will discuss dry run and debug mode, Template, Subcharts, control flow, scoping, variables, and more. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps, and App Development.

Leave a Reply

Copy link
Powered by Social Snap