TExcelTAD

Built-in Objects

The dot in {{ .Values.x }} is the root context Helm passes to every template. Besides Values, it holds objects describing the release, the chart, the target cluster and the template itself. Their field names start with a capital letter.

Object Fields you will use Comes from
.Values everything in values.yaml + -f files + --set the user
.Release Name, Namespace, Revision, IsInstall, IsUpgrade, Service (always Helm) the install/upgrade command
.Chart Name, Version, AppVersion, Description, … (any Chart.yaml field, capitalised) Chart.yaml
.Capabilities KubeVersion.Version, .Major, .Minor; APIVersions.Has "group/version" or "group/version/Kind"; HelmVersion the cluster (or flags, for helm template)
.Template Name (path of the current file), BasePath Helm
.Files Get, Glob, Lines, AsConfig, AsSecrets non-template files in the chart, see Files, ConfigMaps & Secrets

Seeing them

The session renders a ConfigMap that prints most of these, first with defaults, then as an upgrade into a namespace on an older cluster:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.3.1\nappVersion: "1.4.0"\n' > Chart.yaml
echo "{}" > values.yaml
cat > templates/info.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-info
  namespace: {{ .Release.Namespace }}
data:
  release: {{ printf "%s rev %d install=%t upgrade=%t by %s" .Release.Name .Release.Revision .Release.IsInstall .Release.IsUpgrade .Release.Service | quote }}
  chart: {{ printf "%s-%s (app %s)" .Chart.Name .Chart.Version .Chart.AppVersion | quote }}
  kube: {{ .Capabilities.KubeVersion.Version | quote }}
  minor: {{ .Capabilities.KubeVersion.Minor | quote }}
  template: {{ .Template.Name | quote }}
EOF
helm template demo . | sed -n '/^data:/,$p'
helm template web . -n linkly-prod --is-upgrade --kube-version 1.33.4 | grep -E 'namespace|release|kube|minor'
data:
  release: "demo rev 1 install=true upgrade=false by Helm"
  chart: "linkly-0.3.1 (app 1.4.0)"
  kube: "v1.37.0"
  minor: "37"
  template: "linkly/templates/info.yaml"
  namespace: linkly-prod
  release: "web rev 1 install=false upgrade=true by Helm"
  kube: "v1.33.4"
  minor: "33"

Two details worth knowing:

  • helm template renders as if this were revision 1 of a fresh install, against a default Kubernetes version (the one Helm 4.3 was built with). During a real helm install/upgrade, these come from the cluster and the release history. Templates that depend on them should be tested with the flags: --kube-version, --api-versions, --is-upgrade, -n.
  • .Release.Namespace is only the namespace Helm installs into. Writing namespace: {{ .Release.Namespace }} in metadata is still good practice: it makes the rendered output explicit when it's applied by other tools (kubectl apply, a GitOps controller).

Capabilities: adapting to the cluster

.Capabilities.APIVersions.Has lets a chart render an object only when the cluster can accept it. linkly's chart should create an HTTPRoute only if the Gateway API CRDs are installed, because applying an object of an unknown kind fails the whole release:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.1.0\n' > Chart.yaml
printf 'route:\n  host: lnk.example.com\n' > values.yaml
cat > templates/httproute.yaml <<'EOF'
{{- if .Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1/HTTPRoute" }}
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: {{ .Release.Name }}
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  hostnames: [{{ .Values.route.host | quote }}]
  rules:
    - backendRefs: [{name: {{ .Release.Name }}, port: 80}]
{{- end }}
EOF
echo "--- default capabilities:"
helm template demo . | grep -c '^kind:'
echo "--- cluster with Gateway API:"
helm template demo . --api-versions gateway.networking.k8s.io/v1/HTTPRoute | kubeconform -strict -summary
--- default capabilities:
0
--- cluster with Gateway API:
Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0

The default capability set only contains built-in APIs, so with no flag the template produced no object at all. With --api-versions it rendered and validated. On a real install Helm asks the API server for this list.

Note: Capability checks make a chart quietly do less on clusters that lack an API, which is easy to miss. Many charts prefer an explicit switch (httpRoute.enabled: true) and let the install fail loudly if the CRD is missing. A good compromise is both: render when enabled, and fail with a clear message when enabled but unsupported. Flow Control & Scope shows fail.

Version-dependent fields

semverCompare against .Capabilities.KubeVersion.Version handles APIs that changed between Kubernetes versions. The -0 keeps provider suffixes such as -eks-1234 in range:

yaml
{{- if semverCompare ">=1.33-0" .Capabilities.KubeVersion.Version }}
      # sidecar containers (restartPolicy: Always init containers) are stable from 1.33
{{- end }}

Use this sparingly. Supporting the Kubernetes versions that are still maintained (roughly the last three minor releases) and setting kubeVersion in Chart.yaml is simpler than branching on every version.

Test yourself

During helm template with no flags, what is .Release.IsInstall?

  1. false
  2. true
  3. It is undefined
  4. It depends on the cluster

How do you test a template that checks .Capabilities.APIVersions.Has "monitoring.coreos.com/v1/ServiceMonitor" offline?

  1. You can't
  2. helm template --api-versions monitoring.coreos.com/v1/ServiceMonitor
  3. helm lint --strict
  4. helm template --kube-version 1.34

Where does .Chart.AppVersion come from?

  1. The image tag in values.yaml
  2. The appVersion field of Chart.yaml
  3. The cluster
  4. The release name