Helm uses Go templates for templating your resource files. While Go ships several built-in functions, we have added many others.
First, we added all of the functions in the Sprig library, except env
and expandenv
, for security reasons.
We also added two special template functions: include
and required
. The include
function allows you to bring in another template, and then pass the results to other template functions.
Using the ‘include’ Function
For example, this template snippet includes a template called mytpl
, then lowercases the result, then wraps that in double quotes.
|
|
Using the ‘required’ function
The required
function allows you to declare a particular values entry as required for template rendering. If the value is empty, the template rendering will fail with a user submitted error message.
The following example of the required
function declares an entry for .Values.who
is required, and will print an error message when that entry is missing:
|
|
Using the ’tpl’ Function
|
|
Rendering an external configuration file:
|
|
helper template
First, assume that the credentials are defined in the values.yaml
file like so:
|
|
We then define our helper template as follows:
|
|
Finally, we use the helper template in a larger template to create the Secret manifest:
|
|
_helpers.tpl
Using “Partials” and Template Includes
Sometimes you want to create some reusable parts in your chart, whether they’re blocks or template partials. And often, it’s cleaner to keep these in their own files.
In the templates/
directory, any file that begins with an underscore(_
) is not expected to output a Kubernetes manifest file. So by convention, helper templates and partials are placed in a _helpers.tpl
file.
Debug template
There are a few commands that can help you debug.
helm lint
is your go-to tool for verifying that your chart follows best practiceshelm install --dry-run --debug
orhelm template --debug
: We’ve seen this trick already. It’s a great way to have the server render your templates, then return the resulting manifest file.helm get manifest
: This is a good way to see what templates are installed on the server.