Skip to content

values-templating

[BETA] Values & Templates Example

This example demonstrates Zarf’s beta values templating system, including support for Sprig functions for advanced template processing, Helm chart value overrides, and cluster state access via .State.

Features Demonstrated

  • Basic templating with {{ .Values.* }}, {{ .Build.* }}, and {{ .Metadata.* }}
  • Cluster state with {{ .State.Registry.Address }}, {{ .State.StorageClass }}, {{ .State.IPFamily }}, and other non-sensitive runtime fields via .State
  • Package object access via .Pkg, including {{ ((.Pkg.GetComponent "values-with-manifest").GetImageByID "nginx").Reference }} to look up an image by its ID
  • Sprig functions for string manipulation, lists, math, encoding, and more
  • File templating with both simple substitution and complex transformations
  • Dynamic configuration using template functions for practical Kubernetes deployments
  • Helm chart value overrides mapping Zarf values to Helm chart values

Sprig Functions Showcased

The example includes demonstrations of popular Sprig functions:

  • String functions: upper, lower, title, kebabcase, snakecase, quote
  • List functions: join, len, first, last, sortAlpha, reverse
  • Default functions: default for fallback values
  • Math functions: add, mul, max, min
  • Encoding functions: b64enc, sha256sum
  • Utility functions: repeat, indent, trunc, toString

Try It Out

Deploy this example to see values and templates in action:

Terminal window
# Optional: Inspect the manifests and chart values-files
zarf dev inspect manifests
zarf dev inspect values-files
# Create and deploy the package
zarf package create . --confirm
zarf package deploy zarf-package-values-templating-*.tar.zst --confirm
# View the nginx results
kubectl get configmap nginx-configmap -n nginx -o yaml
zarf connect nginx
# View the helm chart results
kubectl get configmap -n helm-overrides -o yaml
# Remove the package with values templating in remove actions
# Feel free to change --set-values to whatever you want!
zarf package remove values-templating --confirm --set-values="site.name=Example,app.environment=test,site.organization=ZarfDev"

zarf.yaml

apiVersion: zarf.dev/v1beta1
kind: ZarfPackageConfig
metadata:
name: values-templating
description: Example nginx package to demonstrate Zarf Values templating
values:
files:
- values/values.yaml
schema: values/values.schema.json
components:
- name: values-with-manifest
description: This component demonstrates templating manifest files by deploying an nginx page to the cluster
images:
- name: "docker.io/library/nginx:1.29.2"
id: nginx
actions:
onRemove:
before:
- cmd: |
echo "============================================"
echo "Removing {{ .Values.site.name }}"
echo "Environment: {{ .Values.app.environment }}"
echo "Organization: {{ .Values.site.organization }}"
echo "============================================"
enableTemplating: true
manifests:
- name: values-with-nginx
# Enables go-templating within the files.
enableTemplating: true
files:
- nginx-deployment.yaml
- nginx-service.yaml
- nginx-configmap.yaml
- name: values-with-helm-chart
description: This component demonstrates helm chart value overrides using Zarf values
charts:
- name: example-chart
local:
path: "charts/example-chart"
namespace: "helm-overrides"
values:
- sourcePath: ".app.name"
targetPath: ".appName"
- sourcePath: ".app.replicas"
targetPath: ".replicaCount"
- sourcePath: ".database.host"
targetPath: ".config.database.host"
# Multiple mappings are ok. They are evaluated first to last, so the latest mapping will take priority.
- sourcePath: ".database.host2"
targetPath: ".config.database.host"
# excludePaths omit sourcePath sub-objects to prevent them from being mapped to the target.
# Here we omit the image reference and hardcode it to the chart values file since it will always be the image used on create.
- sourcePath: ".app.image"
targetPath: ".image"
excludePaths:
- ".app.image.reference"
- name: actions-with-templates
description: "This component demonstrates using enableTemplating: true to enable go-template processing in actions"
actions:
onDeploy:
before:
# By default, templating is disabled in actions to avoid conflicts with external tools
# Set enableTemplating: true to enable Zarf's go-template processing
- cmd: |
echo "Organization: {{ .Values.site.organization }}"
echo "Environment: {{ .Values.app.environment }}"
enableTemplating: true
# Without enableTemplating: true, go-template syntax is passed through unchanged
- cmd: |
echo "This {{ .wontBeProcessed }} stays as-is"
attachments:
readme: readme.md