Helm charts

Helm get:

  •  helm get values <release name> 
  • - It will get all the values user passed using --set option when using helm install command
  • helm get values <release name> -a
  •  - It will give all computed values.
More info here

To dry run:
helm install <name> --debug --dry-run <chartsfolder>/

Eg:
helm create test
 helm install my-app --debug --dry-run test/


Named Template: video

indent vs nindent:

indent

The indent function indents every line in a given string to the specified indent width. This is useful when aligning multi-line strings:

indent 4 $lots_of_text

The above will indent every line of text by 4 space characters.

nindent

The nindent function is the same as the indent function, but prepends a new line to the beginning of the string.

nindent 4 $lots_of_text

The above will indent every line of text by 4 space characters and add a new line to the beginning.

'-' (dash symbol) in curly  braces:
Explanation: here

Note: only for define block use - in the end of the closing flower braces and for the rest use only staring -
Eg:
{{- define "test.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 }}


Named templates:
  • You can define named templated using define(within define block as shown below) in tpl file
{{- define "custom.labels" -}}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}
  • The extension could be any thing .tpl is widely used as a best coding practice.We should prepend name with '_ ' else helm rendering engine  treats it as normal yaml manifest file
  • We can call about defined block of  code, using 2 ways
1.template(It is outdated and  no one uses it)
2.include(Everyone uses it)

Every one  uses include  over temmplate  because, we can pipe output  of include as shown below.Which  is munch helpful in  intending
{{- include (printf "%s%s" .Chart.Name ".labels" ) . | indent 4 }}

As you can see, we piped result to  indent by 4  spaces.If we move the  include block and place it at any indent level,It doesn't mater.It will take iondendation from define block which is at level 0 indent and we specified required indent while calling using include

Most widelu used helm functions:
1. trunc -> trucates string to given numner of characters
trunc 5 "hello world"
out: hello
It doesn't remove 5 characters..It truncates all extra characters from end and makes the final
string length to 5
If you want to trucate from the beginning use -5
trunc 5 "hello world"
world

2.Default
To set a simple default value, use default:
default "foo" .Bar

In the above, if .Bar evaluates to a non-empty value, it will be used. But if it is empty, foo will be returned instead.

The definition of "empty" depends on type:
Numeric: 0
String: ""
Lists: []
Dicts: {}
Boolean: false
And always nil (aka null)

For structs, there is no definition of empty, so a struct will never return the default.

3.quote and squote

These functions wrap a string in double quotes (quote) or single quotes (squote).


Imp notes:

Here is the first named template in _helpers.tpl :

{{- define "myhelm1.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

On the first line we give the template snippet a name. We then refer to this name to include it.

The second line gives "myhelm1.name" a default value: .Chart.Name

If the default value does not exist "myhelm1.name" gets the value of .Values.nameOverride

trunc 63 truncates it to 63 characters in length.

trimSuffix "-" removes ONE trailing - if one exists.

but

trimSuffix "--" removes only two trailing -- if it exists.

( It does not work as in some programming languages where trim will remove all trailing characters )

app.kubernetes.io/name: {{ include "myhelm1.name" . }}

render as

app.kubernetes.io/name: myhelm1

Next: the template code

helm.sh/chart: {{ include "myhelm1.chart" . }}

renders as

helm.sh/chart: myhelm1-0.1.0

This is the template function :

{{- define "myhelm1.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

printf "%s-%s" .Chart.Name .Chart.Version concatenates .Chart.Name and .Chart.Version - plus it places a hyphen between the two.

replace "+" "_" replaces plus characters with underscores.

Now that you can understand those two one-liner functions you should be able to understand the 10-liner define "myhelm1.fullname" with ease.










Comments

Popular posts from this blog

Docker Imp points

Docker volumes backup and restore & Docker networks