Sub topic 3
Built-in objects: .Values, .Release, .Chart and friends
This chapter assumes you can read Kubernetes YAML and have seen the basic {{ }} template syntax from the previous chapter. You should also know the layout of a chart: Chart.yaml, values.yaml and templates/.
What built-in objects are
When Helm renders a template, it passes in a data structure called the root context. It is written as a single dot (.). Helm fills it with built-in objects. Each object is a top-level key on the dot, and you read its fields with dotted paths such as .Release.Name.
| Object | Source | Typical use |
|---|---|---|
| `.Values` | `values.yaml`, `-f` files and `--set` | User-configurable settings |
| `.Release` | The `helm install` or `helm upgrade` command | Name, namespace, revision |
| `.Chart` | `Chart.yaml` | Chart name, version, appVersion |
| `.Capabilities` | The target cluster | Kubernetes version, available API versions |
| `.Template` | The file being rendered | Template name and base path |
.Values
.Values holds the merged result of the chart defaults and every override. Nested keys become nested paths. With the values.yaml from this tutorial, .Values.image.repository is nginx and .Values.service.port is 80.
replicas: {{ .Values.replicaCount }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}".Release
.Release describes this particular installation. Its common fields are:
.Release.Name: the name you gave tohelm install..Release.Namespace: the namespace the release goes into..Release.Revision: starts at 1 and increases with each upgrade..Release.IsInstalland.Release.IsUpgrade: booleans for which operation is running..Release.Service: alwaysHelm.
Using .Release.Name in resource names lets you install the same chart several times in one namespace without name clashes.
.Chart
.Chart exposes the fields of Chart.yaml. The field names are capitalized in templates: .Chart.Name, .Chart.Version and .Chart.AppVersion. The version key is the chart version. The appVersion key is the version of the application, so it is a good default image tag.
.Capabilities and .Template
.Capabilities.KubeVersion.Version gives the cluster version. .Capabilities.APIVersions.Has "networking.k8s.io/v1" tells you whether an API exists. .Template.Name is the path of the current template file, and .Template.BasePath is the templates directory.
{{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1" }}
apiVersion: networking.k8s.io/v1
{{- else }}
apiVersion: networking.k8s.io/v1beta1
{{- end }}Putting them together
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-{{ .Chart.Name }}
namespace: {{ .Release.Namespace }}
labels:
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
data:
logLevel: {{ .Values.env.LOG_LEVEL | quote }}
installedBy: {{ .Release.Service | quote }}Render it with helm template my-release ./webapp --namespace demo. The output name is my-release-webapp in namespace demo.
Common errors
nil pointer evaluating interface {}.repositorymeans a parent key such asimageis missing from the values. Check spelling and indentation..chart.namerenders nothing useful or fails. Built-in names are case-sensitive, so use.Chart.Name.- Inside
rangeorwith, the dot changes..Release.Namethen fails. Use$.Release.Name, which is covered in the chapter on variables and scope. helm templateuses the release namespacedefaultunless you pass--namespace.
Exercise
A template contains name: {{ .Release.Name }}-{{ .Chart.Name }}. You run helm template shop ./webapp, and Chart.yaml has name: webapp. What name is rendered, and why does including the release name help?
Show answer
It renders name: shop-webapp. The release name makes each installation's resources unique, so you can install the chart twice in one namespace, for example as shop and blog, without name collisions.
Exercise
Which built-in object would you use to choose between two apiVersions depending on what the cluster supports?
Show answer
.Capabilities, for example with .Capabilities.APIVersions.Has "networking.k8s.io/v1".