Helm Charts

 Spacing in  helm charts:

  • When ever a go template  command  is executed, then  it will leave  an empty line. If we  want  to remove emtylines, or any secias characters to the  left we need  to use {{- similary  on the right - }}
  • Example {{- template , {{- include, {{- if, {{- end  etc
  • When we use named templates(common blocks of code in _name.tpl files), we  don't need  any  {{- in the declare command,  by we can see in many charts they  were  written(They  doesn't  make any sense)

Reference: video

Include vs template:

  • Both are helpful when we write  want to  call common block of code written in _helper.tpl file using define
  • I always recommend using include  over template because template doesn't support piping
  • Just know about template,  so if  you find any references of it old charts you can get the idea.
  • The template and include takes . as argument which means scope(i.e if we use . inside the define you can refer values using .Values)
  • The level of indentation is decided in the calling place i.e (temlate  or  include).So in the  define block never leave any spaces(Because they will get added up)
  • Conclusion from above oini is that we can place, template or include at any level.But care should be taken while  indenting code  in define block
    Note: If you render using Files.Get then care should be taken while indending unlike template and include.
Indent vs nindent:
see the example in the blog, here. Every thing was explained clearly
  • nident just adds new line before for read ablity we can use nindent and in functions like below 
    {{- tpl (.Files.Get "app.conf") . | nindent 2 }}

   We need to use nindent because it gets contents of the files and paste it in ConfigMap(or any object where it was called). So we need to make sure to insert new line else data copying will have conflict with above line (data: in case of confimap) which results in error

toYaml and fromYaml:

fromYaml: It converts normal yaml file entities into helm objects(refer helm buildin objects). So those values can be accessed using objectReference.yaml field. Just like how we access values.yaml files using .Values.blahblah

Example:

File at: yamls/person.yaml

name: Bob
age: 25
hobbies:
  - hiking
  - fishing
  - cooking
{{- $person := .Files.Get "yamls/person.yaml" | fromYaml }}
greeting: |
  Hi, my name is {{ $person.name }} and I am {{ $person.age }} years old.
  My hobbies are {{ range $person.hobbies }}{{ . }} {{ end }}.  
toYaml: If we want to convert helm objects back to normal yaml, we will use these 
Eg: In configmaps or in writing other kubernetes manifest, If we want to create a yaml file outof specific sectin in values.yaml file. Want we will do is we will refer that specific section using .Values.config etc.

Now the .Values.config is actually is helm object to need to convert that to noraml yaml to include them in configmaps.So we use toYaml with .Values more often

AsConfig or Files.AsConfig:
  • Files.AsConfig is a function that returns file bodies as a YAML map.
    This is very very important. This can be demonstrated using usecase only.
    Eg: When you create a configmap out of external file. 
    let say the external file is conf/app.conf
    test1: value1
    test2: value2
    test:
      - data1
      - data2
      - data3
# AsConfig function demo
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap3
{{- include "common.labels" . | indent 2 }}
data:
{{- (.Files.Glob "conf/app.conf").AsConfig | nindent 2 }}

Rendered output:


This create a single yaml app.(Because configmap expects only map in data section)
Map means it should contain one key and pair right. So here the key is filename and value is contents of the file in a single multi line string.
Lets say there are many files in the directory.The Files.glob pattern will identify all the files and creates a separate key for each file with the key as filename and value as contents of file

tpl function:
Refer this blog here or helm docs here
It takes 2 arguments.
The tpl function evaluates its first argument as a template in the context of its second argument, and returns the rendered result.

Eg:
# values
template: "{{ .Values.name }}"
name: "Tom"

# template
{{ tpl .Values.template . }}

# output
Tom


Here the template value is rendered using .Values.name and it outputs the same.
See example in helm doc for more clarification

kindOf and typeOf:
There are couple of ways using which you can determine the type of object returned after rendering.
See the link here
kindOf:
mapdata -> map
nestedmap -> map
listdata ->  slice 
nestedlist -> slice
string-> string

typeOf:
mapdata ->  map[string]interface {}
nestedmap ->  map[string]interface {}
string-> string
while debugging, we cannot pirnt them after semi colon,
listdata ->  []interface {}   
nestedlist ->  []interface {} 

Note Points:
fromYaml: Convert a YAML string to an object.
It strictly accepts string
{{ Files.Get finename }} -> returns file contents as string i.e multiline string
toYaml: Convert a YAML string to an object.
It converts anything to yaml string

Comments