Helm Templates: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(44 intermediate revisions by the same user not shown)
Line 17: Line 17:
=The templates/ Directory=
=The templates/ Directory=


The 'templates' directory contains templates that, after combination with [[Helm_Concepts#Values_and_Chart_Configuration|values]], will the Kubernetes manifests. When Tiller evaluates a chart, it will send all of the files in the directory - with a few exceptions - through the template rendering engine, then collect the results and send them to to Kubernetes. Note that the <span id='NOTES.txt'>[[Helm Chart NOTES.txt|NOTES.txt]] and  <span id='_helpers.tpl'></span>[[Helm Chart _helpers.tpl|_helpers.tpl]] files are also subject to template rendering, but they are not sent to Kubernetes as manifests.
The 'templates' directory contains templates that, after combination with [[Helm_Concepts#Values_and_Chart_Configuration|values]], referred to as "rendering", become Kubernetes manifests. Helm sends all files in the directory through the template rendering engine, then collect the results for the files that contain manifests, and send them to to Kubernetes. Note that the <span id='NOTES.txt'>[[Helm Chart NOTES.txt|NOTES.txt]] and  <span id='_helpers.tpl'></span>[[Helm Chart _helpers.tpl|_helpers.tpl]] files are also subject to template rendering, but they are not sent to Kubernetes as manifests.


The files whose names begin with an underscore ('_') are assumed to not have a manifest inside, so they are not turned into Kubernetes API resource definitions. However, they are available everywhere within other chart templates for use. These files are conventionally used to store [[Helm_Named_Templates#Sub-Template_Files|sub-templates]] and helpers. [[Helm Chart _helpers.tpl|_helpers.tpl]] is the default location for small sub-templates. If a sub-template is large enough, it can be stored in its own '_'-prefixed file. For more details about sub-templates, see: {{Internal|Helm_Named_Templates#Sub-Template_Files|Helm Named Templates}}
==Template Name==
Template names do not follow a rigid naming pattern. It is, however, recommended to use the suffix .yaml for YAML files and .tpl for helpers.
Template names do not follow a rigid naming pattern. It is, however, recommended to use the suffix .yaml for YAML files and .tpl for helpers.
The files whose name begins with an underscore ('_') are assumed to not have a manifest inside, so they are not rendered into manifest definitions. However, they are available everywhere within other chart templates for use. These files are conventionally used to store [[#Partial|partials]] and helpers. [[Helm Chart _helpers.tpl|_helpers.tpl]] is the default location for template [[#Partial|partials]].
==<span id='Order'></span>Installation and De-Installation Order==
==<span id='Order'></span>Installation and De-Installation Order==
During installation, Helm collects all of the resources in a given chart and its dependences, groups them by resource type, and installs them in the order specified here https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L29-L57. Upon de-installation the order is reversed: https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L62-L90
During installation, Helm collects all of the resources in a given chart and its dependences, groups them by resource type, and installs them in the order specified here https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L29-L57. Upon de-installation the order is reversed: https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L62-L90


Line 125: Line 124:
For details related to whitespace handling when declaring named templates, see [[Helm_Templates#Named_Templates_and_Whitespace_Handling|Named Templates and Whitespace Handling]], below.
For details related to whitespace handling when declaring named templates, see [[Helm_Templates#Named_Templates_and_Whitespace_Handling|Named Templates and Whitespace Handling]], below.


=<span id='Scope'></span>Scopes=
=<span id='Scopes'></span>Scope=
 
A scope, in this context, is a data structure, specifically a [[sprig_dict#Overview|dict]] instance.
 
The "." root scope is a dict instance that carries by default the following sub-scopes, containing the "[[Helm_Templates#Built-in_Objects|built-in objects]]":
 
.
├─ [[#Values|Values]]
├─ [[#Chart|Chart]]
├─ [[#Release|Release]]
├─ [[#Capabilities|Capabilities]]
├─ [[#Files|Files]]
└─ [[#Template|Template]]


Scopes are declared with '[[#with|with]]'.  
A scope must be specified when a [[Helm_Named_Templates#Sub-Template_Scope|sub-template]] is rendered with [[Helm_Named_Templates#include_Function|include]] or [[Helm_Named_Templates#template|template]], otherwise the built-in objects referred from the sub-template may not be resolvable.


<font color=darkgray>TODO.</font>
The root scope can be manipulates as follows:
<syntaxhighlight lang='yaml'>
{{- $Args := dict "Name" "example" -}}
{{- $_0 :=  set . "Args" $Args -}}
</syntaxhighlight>
Scopes are declared with '[[#with|with]]'.


=<span id='Template_Object'></span>Template Objects=
=<span id='Template_Object'></span>Template Objects=
Line 197: Line 213:
Exposes the namespace the release made into:
Exposes the namespace the release made into:
  &#123;{ .Release.Namespace }}
  &#123;{ .Release.Namespace }}
This value is specified on command line with [[Helm_install#-n.2C--namespace|-n|--namespace]]. If not specified, it explicitly defaults to "default".
====Release.Revision====
====Release.Revision====
Exposes the [[Helm_Concepts#Release_Revision|release revision]]:
Exposes the [[Helm_Concepts#Release_Revision|release revision]]:
Line 253: Line 272:


The easiest way to debug an object's type is to pass it through <code>printf "%t"</code> in a template, which will print the type. Also see the [[Helm Template Function typeOf|typeOf]] and [[Helm Template Function kindOf|kindOf]] functions.
The easiest way to debug an object's type is to pass it through <code>printf "%t"</code> in a template, which will print the type. Also see the [[Helm Template Function typeOf|typeOf]] and [[Helm Template Function kindOf|kindOf]] functions.
=Action vs. Functions=
Helm templates use both actions and [[#Template_Functions|functions]]. For an action, the data is simply inserted in-line. For a function, the output of a function can be passed to another function. [[#Template_Control_Structures|Template control structures]], such as [[Helm_Templates#if.2Felse|if/else]], [[#with|with]], [[#range|range]], [[Helm_Named_Templates#define|define]] and [[Helm_Named_Templates#template|template]] are actions.


=<span id='Template_Function'></span>Template Functions=
=<span id='Template_Function'></span>Template Functions=
Line 280: Line 302:
* trunc 63  
* trunc 63  
* trimSuffix "-"  
* trimSuffix "-"  
* indent 4
* [[Helm Template Function indent|indent]]
* [[Helm Template Function nindent|nindent]]
* [[Helm Template Function nindent|nindent]]
* [[#Accessing_Array_Elements|index]]
* [[#Accessing_Array_Elements|index]]
Line 288: Line 310:
* <span id='typeOf'></span>[[Helm Template Function typeOf|typeOf]]
* <span id='typeOf'></span>[[Helm Template Function typeOf|typeOf]]
* <span id='kindOf'></span>[[Helm Template Function kindOf|kindOf]]
* <span id='kindOf'></span>[[Helm Template Function kindOf|kindOf]]
* [[Sprig_list#join|join]]


===Operators===
===Operators===
Line 301: Line 324:
   &#123;{ or }}
   &#123;{ or }}
   &#123;{ not }}
   &#123;{ not }}
===Storage===
* [[sprig_dict#Overview|dict]]
* [[sprig_list#Overview|list]]


===Accessing Array Elements===
===Accessing Array Elements===
Line 339: Line 365:


This pattern can be extrapolated to more complex data structures.
This pattern can be extrapolated to more complex data structures.
==include Function==


=Template Pipelines=
=Template Pipelines=
Line 376: Line 404:
{{Internal|Helm Template range|range}}
{{Internal|Helm Template range|range}}


==<span id='Named_Template_Actions'></span><span id='define'></span><span id='Named_Templates_and_Whitespace_Handling'></span><span id='Named_Template_Recipes'></span><span id='template_2'></span><span id='block'></span><span id='The_include_Function'></span>Named Template Actions (Partials)==
==<span id='Named_Template_Actions'></span><span id='define'></span><span id='Named_Templates_and_Whitespace_Handling'></span><span id='Named_Template_Recipes'></span><span id='template_2'></span><span id='block'></span><span id='The_include_Function'></span>Named Templates (Sub-templates, Partials)==


{{Internal|Helm Named Templates|Helm Named Templates}}
{{Internal|Helm Named Templates|Helm Named Templates}}


=Template Variables=
=<span id='Template_Variables'></span>Variables=
 
{{Internal|Helm Variables|Variables}}
{{External|[https://helm.sh/docs/chart_template_guide/variables/ Variables]}}
<font color=darkgray>TODO.</font>


=Configmap and Secrets Utility Functions=
=Configmap and Secrets Utility Functions=

Revision as of 22:38, 19 October 2020

External

Internal

Overview

Templates are a set of Kubernetes parameterized manifests that form an application. They live under a chart's templates/ directory. They are written in YAML with Helm templates extensions. Upon processing by Helm, they become Kubernetes manifest files. Helm template extensions are written in the Help template language, which is based on Go templates.

The templates/ Directory

The 'templates' directory contains templates that, after combination with values, referred to as "rendering", become Kubernetes manifests. Helm sends all files in the directory through the template rendering engine, then collect the results for the files that contain manifests, and send them to to Kubernetes. Note that the NOTES.txt and _helpers.tpl files are also subject to template rendering, but they are not sent to Kubernetes as manifests.

The files whose names begin with an underscore ('_') are assumed to not have a manifest inside, so they are not turned into Kubernetes API resource definitions. However, they are available everywhere within other chart templates for use. These files are conventionally used to store sub-templates and helpers. _helpers.tpl is the default location for small sub-templates. If a sub-template is large enough, it can be stored in its own '_'-prefixed file. For more details about sub-templates, see:

Helm Named Templates

Template Name

Template names do not follow a rigid naming pattern. It is, however, recommended to use the suffix .yaml for YAML files and .tpl for helpers.

Installation and De-Installation Order

During installation, Helm collects all of the resources in a given chart and its dependences, groups them by resource type, and installs them in the order specified here https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L29-L57. Upon de-installation the order is reversed: https://github.com/helm/helm/blob/release-2.14/pkg/tiller/kind_sorter.go#L62-L90

Template Comments

 # This is a comment
{{/* Generate basic labels */}}

Multi-Line Comments

 {{- /*
 This is another 
 multi-line
 comment
 */ -}}

Template Directives

A template directive, sometimes also referred as tag, is enclosed in {{ and }} blocks, and it is recommended to pad the directive with space at its left and right. The simplest directive renders a value. A value is a namespaced object, where each dot (.) separates each namespaced element. A leading dot indicates that we start with the top-most namespace for the scope.

kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap

Directives may also include functions and other constructs.

With the exception of the multi-line comments, all directives must be specified on one line only. Space present inside a directive is irrelevant, the following formats are equivalent:

{{.Values.color}}
{{ .Values.color }}
{{            .Values.color               }}

The recommended format is to allow for a space after '{{' and before '}}':

{{ .Values.color }}

Directives and Whitespace Handling

Controlling Whitespace

The spaces inside a directive is irrelevant (we cannot have newlines inside a directive, all directives must be specified on a single line). The '{{' and '}}' directive delimiters, without any other modifications, leave the template whitespace surrounding them alone, and do not interfere with it in any way. A hyphen '-' placed after the '{{' delimiter or before the '}}' delimiter instructs the rendering engine to trim the whitespace preceding, respectively trailing the delimiter. Whitespace includes spaces, tabs, newline ('\n') and carriage return ('\r'). When encountering "-", the template engine will simply drop the corresponding whitespace until a non-whitespace character is found.

metadata:
  color: {{ .Values.color }}
spec:

will produce (assuming that "color" is declared to be "blue" in values.yaml:

metadata:
  color: blue
spec:

To trim preceding whitespace, use '{{-'.

metadata:
  color: {{- .Values.color }}
spec:

will produce:

metadata:
  color:blue
spec:

To trim trailing whitespace (whitespace includes newlines), use '-}}'.

metadata:
  color: {{ .Values.color -}}
spec:

will produce:

metadata:
  color: bluespec:

(which is something you most like don't want).

For details related to whitespace handling when declaring named templates, see Named Templates and Whitespace Handling, below.

Scope

A scope, in this context, is a data structure, specifically a dict instance.

The "." root scope is a dict instance that carries by default the following sub-scopes, containing the "built-in objects":

.
├─ Values
├─ Chart
├─ Release
├─ Capabilities
├─ Files
└─ Template

A scope must be specified when a sub-template is rendered with include or template, otherwise the built-in objects referred from the sub-template may not be resolvable.

The root scope can be manipulates as follows:

{{- $Args := dict "Name" "example" -}}
{{- $_0 :=  set . "Args" $Args -}}

Scopes are declared with 'with'.

Template Objects

Objects are passed into a template from the template engine. The template directives can create new objects and pass them around. There are also built-in objects, which are made available by default. Objects can be simple - have just one value -, or they can contain other objects or functions. For example the "Release" built-in object contains several other objects (like "Release.Name"). The "Files" object contains functions.

Built-in Objects

Built-in Objects

Built-in objects are a way to access several types of values, some of which are directly configured by operators - in values.yaml, for example -, while others are generated dynamically by Helm or taken from other parts of the chart.

The built-in values always begin with a capital letter, based on Go's naming convention. For a fully working examples of built-in objects replacement see:

https://github.com/ovidiuf/playground/tree/master/helm/bactrosaurus

Chart

This object contains value passed into the template from the Chart.yaml file. An existing field is available as (note leading dot) .Chart.<UpperCasedFirstLetterFieldName>. It is important to capitalize the first letter of the field name, otherwise the directive evaluation fails.

Example:

{{ .Chart.Name }}
{{ .Chart.Version }}

Values

This object provides access the effective values of all configuration element, as present in the runtime configuration tree, and it essentially exposes the chart configuration to templates. The value of an existing configuration element can be access using the following syntax: (note leading dot) .Values.<fieldName>. Unlike in Chart's case, the fields are allowed to keep their original capitalization. For example, a value declared as such in values.yaml:

size: 10

can be references in a template as:

kind: ConfigMap
...
data:
  size: {{ .Values.size }}

Values may contain structured content:

characteristics:
  size: 10
  shape: "large"

can be referenced in template as:

kind: ConfigMap
 ...
data:
  size: {{ .Values.characteristics.size }}
  shape: {{ .Values.characteristics.shape }}

While structuring data this way is possible, the recommendation is to keep values trees shallow, favoring flatness.

In case the structure contains an array, individual elements can be referred from the template with the index function.

Release

This object describes the release itself.

Release.Name

Exposes the release name:

{{ .Release.Name }}

Release.Namespace

Exposes the namespace the release made into:

{{ .Release.Namespace }}

This value is specified on command line with -n|--namespace. If not specified, it explicitly defaults to "default".

Release.Revision

Exposes the release revision:

{{ .Release.Revision }}

Release.Time

Exposes the time of the release:

{{ .Release.Time }}

Release.IsUpgrade

This is set to true if the current operation is an upgrade or rollback.

{{ .Release.IsUpgrade }}

Release.IsInstall

This is set to true if the current operation is an install.

{{ .Release.IsInstall }}

Release.Service

Exposes the releasing service - always Tiller

Files

The object provide access to all non-special files in the chart. It cannot be used to access templates. The access is provided via several functions:

Files.Get

{{ .Files.Get <file-name> }}

Files.GetBytes

Accessing Files inside Templates

Accessing Arbitrary Files inside Templates

Capabilities

Provides information about the capabilities of the Kubernetes cluster:

{{ .Capabilities.APIVersions }}
{{ .Capabilities.APIVersions.Has }}
{{ .Capabilities.KubeVersion }}
{{ .Capabilities.KubeVersion.Major|
 Minor|GitVersion|GitCommit|GitTreeState|BuildDate|GoVersion|Compiler|Platform}}
{{ .Capabilities.TillerVersion }}

Template

Contains information about the current template that is being executed:

{{ .Template.Name }}
{{ .Template.BasePath }}

Data Types

https://helm.sh/docs/chart_template_guide/data_types/
  • string: A string of text
  • bool: a true or false
  • int: an integer value
  • float64: a 64-bit floating point value
  • a byte slice ([]byte), used to hold potentially binary data
  • struct: an object with properties and methods
  • a slice (indexed list) of one of the previous types
  • a string-keyed map (map[string]interface{}) where the value is one of the previous types.

The easiest way to debug an object's type is to pass it through printf "%t" in a template, which will print the type. Also see the typeOf and kindOf functions.

Action vs. Functions

Helm templates use both actions and functions. For an action, the data is simply inserted in-line. For a function, the output of a function can be passed to another function. Template control structures, such as if/else, with, range, define and template are actions.

Template Functions

Template Functions and Pipelines
Go Templates
sprig Template Functions

A template function modifies data provided to the template via template objects, and it is declared inside the template, in a template directive. Template functions follow the syntax:

functionName arg1 arg2 ...

Example:

{{ quote .Values.color }}

Helm Template Function Reference

Operators

Operators are Functions

Operators are implemented as functions that return a boolean value:

 {{ eq Values.color "blue" }}
 {{ ne }}
 {{ lt }}
 {{ gt }}
 {{ and }}
 {{ or }}
 {{ not }}

Storage

Accessing Array Elements

To access a specific element of an array data structure, use the index function. The index function is 0-based.

Assuming we declare a simple array:

colors:
  - 'blue'
  - 'red'
  - 'green'

then the first element of the array can be accessed with:

{{ index .Values.colors 0 }}

The directive is rendered to "blue".

When the array contains maps:

colors:
  - name: blue
    shade: dark
  - name: red
    shade: light

the fields of the maps can be accessed with - note (...):

{{ (index .Values.colors 0).name }}

This pattern can be extrapolated to more complex data structures.

include Function

Template Pipelines

Pipelines
{{ <object> | <function1> | <function2>  }}
{{ .Values.color | upper | repeat 5 }}

Template Control Structures

Flow control structures are called "actions".

if/else

if/else

with

Modifying scope using with

with narrows the scope of the context for better readability and more expressive template blocks. with specifies a scope:

{{- with .Values.deployment }}
strategy:
  rollungUpdate:
    maxUnavailable: {{ .maxUnavailable }}
    maxSurge: {{ .maxSurge }}
revisionHistoryLimit: {{ .revisionHistoryLimit }}
minReadySeconds: {{ .minReadySeconds }}
{{- end }}

TODO.

range

range

Named Templates (Sub-templates, Partials)

Helm Named Templates

Variables

Variables

Configmap and Secrets Utility Functions

Configmap and Secrets Utility Functions

TODO.

Debugging Templates

Debugging Templates
https://github.com/databus23/schelm

TODO.

Template Recipes

Transfer the Content of a Map from values.yaml to a Template

values.yaml:

data:
  color: 'red'
  shape: 'triangle'

template:

metadata:
  {{- if .Values.data }}
  annotations:
  {{- range $key, $value := .Values.data}}
    {{ $key }}: {{ $value | quote }}
  {{- end}}
  {{- end}}

For more details see:

range

Transfer the Content of a List from values.yaml to a Template

Iterating over a .Values List

TODO