Please enable Javascript to view the contents

 ·  ☕ 3 分钟

https://helm.sh/docs/howto/charts_tips_and_tricks/

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.

1
value: {{ include "mytpl" . | lower | quote }}

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:

1
value:  {{  required "A valid .Values.who entry required!" .Values.who }}

Using the ’tpl’ Function

1
2
3
4
5
6
7
8
9
# values
template: "{{ .Values.name }}"
name: "Tom"

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

# output
Tom

Rendering an external configuration file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# external configuration file conf/app.conf
firstName={{ .Values.firstName }}
lastName={{ .Values.lastName }}

# values
firstName: Peter
lastName: Parker

# template
{{ tpl (.Files.Get "conf/app.conf") . }}

# output
firstName=Peter
lastName=Parker

helper template

First, assume that the credentials are defined in the values.yaml file like so:

1
2
3
4
5
imageCredentials:
  registry: quay.io
  username: someone
  password: sillyness
  email: someone@host.com

We then define our helper template as follows:

1
2
3
4
5
{{- define "imagePullSecret" }}
{{- with .Values.imageCredentials }}
{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
{{- end }}
{{- end }}

Finally, we use the helper template in a larger template to create the Secret manifest:

1
2
3
4
5
6
7
apiVersion: v1
kind: Secret
metadata:
  name: myregistrykey
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template "imagePullSecret" . }}

_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

https://helm.sh/docs/chart_template_guide/debugging/

There are a few commands that can help you debug.

  • helm lint is your go-to tool for verifying that your chart follows best practices
  • helm install --dry-run --debug or helm 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.
分享

Mark Zhu
作者
Mark Zhu
An old developer