How to Use Functions and Pipelines in Helm

Aman Jaiswal
DevOps Learners
Published in
3 min readMar 18, 2022

Helm provides us with different functions to modify the templates which we’re using.

Functions & Pipeline in Helm?

If we want to transform the supplied data, functions are really useful.

Pipeline is the powerful feature of templating. By using pipelines we can use functions in a more efficient way.

There are many types of functions that we can use in our templates. Below are all the functions that helm provides.

  • Cryptographic and Security
  • Date
  • Dictionaries
  • Encoding
  • File Path
  • Kubernetes and Chart
  • Logic and Flow Control
  • Lists
  • Math
  • Network
  • Reflection
  • Regular
  • Expressions
  • Semantic Versions
  • String
  • Type Conversion
  • URL
  • UUID

In this blog, we’ll see string functions that are majorly used in our templates.

Helm includes the following string functions:

abbrev, abbrevboth, camelcase, cat, contains, hasPrefix, hasSuffix, indent, initials, kebabcase, lower, nindent, nospace, plural, print, printf, println, quote, randAlpha, randAlphaNum, randAscii, randNumeric, repeat, replace, shuffle, snakecase, squote, substr, swapcase, title, trim, trimAll, trimPrefix, trimSuffix, trunc, untitle, upper, wrap, and wrapWith.

Let’s see some examples of these string functions. For example, let’s say below is our template for deployment file(templates/deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}
labels:
app: myapp
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: {{ .Values.image.repository }}
ports:
- containerPort: 80

And here we’re taking replicas and image name from the below values.yaml file

replicaCount: 1
image:
repository: nginx
tag: 1.21.6
pullPolicy: Always

Now if we want to modify our repo name in the actual deployment file we can use different helm functions according to our requirements.

{{ .Values.image.repository }} ---> image: nginx  
//actual transformation in our deployment file

Here is the output of functional transformation:

{{ upper .Values.image.repository }} ---> image: NGINX
//upper function
{{ quote .Values.image.repository }} ---> image: "nginx"
//quote function
{{ squote .Values.image.repository }} ---> image: 'nginx’
//squote function
{{ replace "x" "y" .Values.image.repository }} ---> image: nginy
//replace function
{{ repeat 2 .Values.image.repository }} ---> image: nginxnginx
//repeat function
{{ substr 0 2.Values.image.repository }} ---> image: ngi
//substr function
{{ shuffle .Values.image.repository }} ---> image: xnign
//shuffle function

Let’s see how can we use pipeline to use these functions.

{{ .Values.image.repository | upper }} ---> image: NGINX{{ .Values.image.repository | quote }} ---> image: "nginx"{{ .Values.image.repository | squote }} ---> image: 'nginx'{{ .Values.image.repository | replace "x" "y" }} ---> image: nginy{{ .Values.image.repository | repeat 2 }} ---> image: nginxnginx{{ .Values.image.repository | substr 0 2 }} ---> image: ngi{{ .Values.image.repository | shuffle }} ---> image: xnign

We can also use multiple functions using the pipeline.

{{ .Values.image.repository | upper | quote }} ---> image: "NGINX"{{ .Values.image.repository | upper | shuffle }} ---> image: XNIGN

Let’s see some other string functions and their use.

trim

It removes whitespace from both sides.

trim " myapp   " ---> myapp

nospace:

It removes all whitespace from a string.

nospace "my a p  p" ---> myapp

trunc:

It truncates a string.

trunc 2 "my app" ---> mytrunc -2 "my app" ---> pp

initials:

It combines the first letter of given multiple words.

initials "my app" ---> ma

contains:

It returns a boolean value if one string is contained inside of another string.

contains "my" "myapp" ---> true

hasPrefix:

It returns a boolean value whether a string has a given prefix.

hasPrefix "me" "myapp" ---> false

cat:

It concatenates multiple strings together into one, separating them with spaces.

cat "my" "app" ---> my app

snakecase:

It converts a string from camelcase to snakecase.

snakecase "MyApp" ---> my_app

camelcase:

It converts a string from snakecase to camelcase.

camelcase "my_app" ---> MyApp

kebabcase:

It converts a string from camelcase to kebab-case.

kebabcase "MyApp" ---> my-app

Go through the official helm documentation for other string functions use cases. Will be sharing more details and examples around other helm function types.

--

--