Helm Template Level 3: Functions, Multiple Values & Files - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki

Define/Include:

To copy a literal or a value ( which is produced by executing a block of code) at multiple places in templates.

Define:

To assign a value or a block of code to a variable/identity. Should be defined in _helpers.tpl

Include:

To specify a variable in templates where its value needs to be substitued.

Example:

Define a variable with template file (_helpers.tpl):

  • A simple example:
{{- define "helmtest.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
  • Example-2: Define & Refer variables -

{{/*
Create a default fully qualified app name.
If there is a value defined for "fullnameOverride", the first 63 characters only are printed & 
added with a suffix "-", else it concatenates & prints ".Chart.name" & ".Values.nameOverride" 
If release name contains chart name it will be used as a full name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).

*/}}

{{- define "helmtest.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
  • Referred the variable in deployment.yaml template as below:

name: {{ include "helmtest.fullname" . }}

Note:

Generally the template helper file does not have to be always "_helpers.tpl" only, it could be named differently also. In that case, it should be specified explicitly like below:

helm template test ./multiple-env/ -f ./multiple-env/values-qa.yaml

Multiple value files can also be passed in the above. In case, if a same value defined in both the files, the value in the second file will be used. For example,

helm template test ./multiple-env/ -f ./multiple-env/values-qa.yaml -f ./multiple-env/values-common.yaml

==========

Helm functions and examples

Refer official Helm Documentation: https://helm.sh/docs/chart_template_guide/function_list/

Note: the vaules.yaml for this is at https://github.com/DeekshithSN/Helm_charts/blob/master/Tem_functions/values.yaml

1. quote

wraps with double quotes to given string, if you give pizza as input the output will be "pizza"

usage:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ quote .Values.favorite.drink }}
  food: {{ quote .Values.favorite.food }}

output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: trendsetting-p-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "Pizza"

2. repeat

The repeat function will echo the given string the given number of times

usage:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | repeat 5 | quote }}

output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: melting-porcup-configmap
data:
  myvalue: "Hello World"
  drink: "coffeecoffeecoffeecoffeecoffee"

3. upper

converts given string to uppercase

usage:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | quote }}
  food: {{ .Values.favorite.food | upper | quote }}

output:

  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: trendsetting-p-configmap
  data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "PIZZA"

4. lower

converts given string to lowercase

usage:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | quote }}
  food: {{ .Values.favorite.food | lower | quote }}

output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: trendsetting-p-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "pizza"

include

The include function allows you to bring in another template, and then pass the results to other template functions.

For example, this template snippet includes a template called mytpl, then lowercases the result, then wraps that in double quotes.

value: {{ include "mytpl" . | lower | quote }}

Network Functions

  • getHostByName receives a domain name and returns the ip address.

===