Skip to content

Latest commit

 

History

History
222 lines (177 loc) · 5.28 KB

8.template_functions&pipelines.md

File metadata and controls

222 lines (177 loc) · 5.28 KB

template-func-helm helm-pipeline

assume the following is function.yaml which is a part of my_chart/ in your helm

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  lables:
data:
  myvalue: "Hello World"
  who: {{ required "A valid .Values.who entry required!" .Values.who }}
  drink: {{  .Values.favorite.drink }}
  food: {{ quote .Values.favorite.food }}
  city: {{.Values.favorite.city | default "Bangalore"}}
helm install --debug --dry-run my_chart/

When you do this it will show what it is deploying in your cluster and you will notice that the values will be replaced from values.yaml

Helm functions

  1. quote

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

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ quote .Values.favorite.drink }}     -------> this will bring quotes around the value replaced for favourite drink
  food: {{ quote .Values.favorite.food }}

example output:

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

also we can use pipeline to achieve the same

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favorite.drink | quote }}     -------> this will bring quotes around the value replaced for favourite drink
  food: {{ .Values.favorite.food | quote}}
  1. 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"
  1. 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"
  1. 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"
  1. 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 }}
  1. required
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  app.properties: |
    database.url={{ required "Database URL is required" .Values.database.url }}
    database.username={{ required "Database username is required" .Values.database.username }}
    database.password={{ required "Database password is required" .Values.database.password }}
  config.yaml: |
    server:
      port: {{ required "Server port is required" .Values.server.port }}
      debug: {{ required "Debug mode is required" .Values.server.debug | default "false" }}
      logLevel: {{ required "Log level is required" .Values.server.logLevel }}

Now if you try installing the chart containing the file and you haven't give any pf the values: port, logLevel it will fail.

  1. default

If user doesn't provide any value default value will be used.

  1. nindent

The nindent function in Helm templates is used to add a specified number of spaces (indentation) to each line of a multi-line string. This function is particularly useful when you want to indent a block of text, such as a YAML configuration file, within a Helm template.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  config.yaml: |
{{- $config := .Files.Get "config.yaml" }}
{{- $indentedConfig := nindent 4 $config }}
{{ $indentedConfig }}

  1. indent: It's like telling someone to start each new line with a specific word or symbol. You choose what that word or symbol is.

nindent: It's like telling someone to add a certain number of spaces at the beginning of each new line. You specify how many spaces to add.

  1. The toYaml function in Helm templates is used to convert a value to YAML format. It's particularly useful when you want to convert complex data structures, such as maps or lists, into YAML format for inclusion in your Helm templates.
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  config.yaml: |
{{- $data := toYaml .Values.myData }}
{{ $data | nindent 4 }}