Templates and Values
Helm Chart templates are written in theGo template language, with theaddition of 50 or so add-on templatefunctions from the Sprig library and afew other specialized functions.
All template files are stored in a chart’s templates/
folder. WhenHelm renders the charts, it will pass every file in that directorythrough the template engine.
Values for the templates are supplied two ways:
- Chart developers may supply a file called
values.yaml
inside of achart. This file can contain default values. - Chart users may supply a YAML file that contains values. This can beprovided on the command line with
helm install
.When a user supplies custom values, these values will override thevalues in the chart’svalues.yaml
file.
Template Files
Template files follow the standard conventions for writing Go templates(see the text/template Go package documentationfor details).An example template file might look something like this:
apiVersion: v1
kind: ReplicationController
metadata:
name: deis-database
namespace: deis
labels:
app.kubernetes.io/managed-by: deis
spec:
replicas: 1
selector:
app.kubernetes.io/name: deis-database
template:
metadata:
labels:
app.kubernetes.io/name: deis-database
spec:
serviceAccount: deis-database
containers:
- name: deis-database
image: {{.Values.imageRegistry}}/postgres:{{.Values.dockerTag}}
imagePullPolicy: {{.Values.pullPolicy}}
ports:
- containerPort: 5432
env:
- name: DATABASE_STORAGE
value: {{default "minio" .Values.storage}}
The above example, based loosely on https://github.com/deis/charts, is a template for a Kubernetes replication controller.It can use the following four template values (usually defined in avalues.yaml
file):
imageRegistry
: The source registry for the Docker image.dockerTag
: The tag for the docker image.pullPolicy
: The Kubernetes pull policy.storage
: The storage backend, whose default is set to"minio"
All of these values are defined by the template author. Helm does notrequire or dictate parameters.
To see many working charts, check out the Helm Chartsproject
Predefined Values
Values that are supplied via a values.yaml
file (or via the —set
flag) are accessible from the .Values
object in a template. But thereare other pre-defined pieces of data you can access in your templates.
The following values are pre-defined, are available to every template, andcannot be overridden. As with all values, the names are casesensitive.
Release.Name
: The name of the release (not the chart)Release.Time
: The time the chart release was last updated. This willmatch theLast Released
time on a Release object.Release.Namespace
: The namespace the chart was released to.Release.Service
: The service that conducted the release. Usuallythis isTiller
.Release.IsUpgrade
: This is set to true if the current operation is an upgrade or rollback.Release.IsInstall
: This is set to true if the current operation is aninstall.Release.Revision
: The revision number. It begins at 1, and increments witheachhelm upgrade
.Chart
: The contents of theChart.yaml
. Thus, the chart version isobtainable asChart.Version
and the maintainers are inChart.Maintainers
.Files
: A map-like object containing all non-special files in the chart. Thiswill not give you access to templates, but will give you access to additionalfiles that are present (unless they are excluded using.helmignore
). Files can beaccessed using{{index .Files "file.name"}}
or using the{{.Files.Get name}}
or{{.Files.GetString name}}
functions. You can also access the contents of the fileas[]byte
using{{.Files.GetBytes}}
Capabilities
: A map-like object that contains information about the versionsof Kubernetes ({{.Capabilities.KubeVersion}}
, Tiller({{.Capabilities.TillerVersion}}
, and the supported Kubernetes API versions({{.Capabilities.APIVersions.Has "batch/v1"
)NOTE: Any unknown Chart.yaml fields will be dropped. They will notbe accessible inside of theChart
object. Thus, Chart.yaml cannot beused to pass arbitrarily structured data into the template. The valuesfile can be used for that, though.
Values files
Considering the template in the previous section, a values.yaml
filethat supplies the necessary values would look like this:
imageRegistry: "quay.io/deis"
dockerTag: "latest"
pullPolicy: "Always"
storage: "s3"
A values file is formatted in YAML. A chart may include a defaultvalues.yaml
file. The Helm install command allows a user to overridevalues by supplying additional YAML values:
$ helm install --values=myvals.yaml wordpress
When values are passed in this way, they will be merged into the defaultvalues file. For example, consider a myvals.yaml
file that looks likethis:
storage: "gcs"
When this is merged with the values.yaml
in the chart, the resultinggenerated content will be:
imageRegistry: "quay.io/deis"
dockerTag: "latest"
pullPolicy: "Always"
storage: "gcs"
Note that only the last field was overridden.
NOTE: The default values file included inside of a chart must be namedvalues.yaml
. But files specified on the command line can be namedanything.
NOTE: If the —set
flag is used on helm install
or helm upgrade
, thosevalues are simply converted to YAML on the client side.
NOTE: If any required entries in the values file exist, they can be declaredas required in the chart template by using the ‘required’ function
Any of these values are then accessible inside of templates using the.Values
object:
apiVersion: v1
kind: ReplicationController
metadata:
name: deis-database
namespace: deis
labels:
app.kubernetes.io/managed-by: deis
spec:
replicas: 1
selector:
app.kubernetes.io/name: deis-database
template:
metadata:
labels:
app.kubernetes.io/name: deis-database
spec:
serviceAccount: deis-database
containers:
- name: deis-database
image: {{.Values.imageRegistry}}/postgres:{{.Values.dockerTag}}
imagePullPolicy: {{.Values.pullPolicy}}
ports:
- containerPort: 5432
env:
- name: DATABASE_STORAGE
value: {{default "minio" .Values.storage}}
Scope, Dependencies, and Values
Values files can declare values for the top-level chart, as well as forany of the charts that are included in that chart’s charts/
directory.Or, to phrase it differently, a values file can supply values to thechart as well as to any of its dependencies. For example, thedemonstration WordPress chart above has both mysql
and apache
asdependencies. The values file could supply values to all of thesecomponents:
title: "My WordPress Site" # Sent to the WordPress template
mysql:
max_connections: 100 # Sent to MySQL
password: "secret"
apache:
port: 8080 # Passed to Apache
Charts at a higher level have access to all of the variables definedbeneath. So the WordPress chart can access the MySQL password as.Values.mysql.password
. But lower level charts cannot access things inparent charts, so MySQL will not be able to access the title
property. Nor,for that matter, can it access apache.port
.
Values are namespaced, but namespaces are pruned. So for the WordPresschart, it can access the MySQL password field as .Values.mysql.password
. Butfor the MySQL chart, the scope of the values has been reduced and thenamespace prefix removed, so it will see the password field simply as.Values.password
.
Global Values
As of 2.0.0-Alpha.2, Helm supports special “global” value. Considerthis modified version of the previous example:
title: "My WordPress Site" # Sent to the WordPress template
global:
app: MyWordPress
mysql:
max_connections: 100 # Sent to MySQL
password: "secret"
apache:
port: 8080 # Passed to Apache
The above adds a global
section with the value app: MyWordPress
.This value is available to all charts as .Values.global.app
.
For example, the mysql
templates may access app
as {{.Values.global.app}}
, andso can the apache
chart. Effectively, the values file above isregenerated like this:
title: "My WordPress Site" # Sent to the WordPress template
global:
app: MyWordPress
mysql:
global:
app: MyWordPress
max_connections: 100 # Sent to MySQL
password: "secret"
apache:
global:
app: MyWordPress
port: 8080 # Passed to Apache
This provides a way of sharing one top-level variable with allsubcharts, which is useful for things like setting metadata
propertieslike labels.
If a subchart declares a global variable, that global will be passeddownward (to the subchart’s subcharts), but not upward to the parentchart. There is no way for a subchart to influence the values of theparent chart.
Also, global variables of parent charts take precedence over the global variables from subcharts.
References
When it comes to writing templates and values files, there are severalstandard references that will help you out.