Skip to main content
Version: main 🚧

Use Go Template Functions

Overview​

A Go template function is a built-in or user-defined helper function. It can be used within Go templates to transform data when rendering text. For example, in a Helm chart (which uses Go templates), functions such as default, upper, and quote are commonly used to process values dynamically.

Templates in vCluster Platform standardize configurations for virtual clusters, namespaces, and applications. You can create templates through the vCluster Platform UI or by applying manifests directly to your cluster using vCluster CLI or Helm charts. The platform provides the VirtualClusterTemplate CRD for defining templates. For more information, see Creating Templates.

When defining a VirtualClusterTemplate CRD, use Go template functions to enable conditional logic and dynamic configuration within your template values and customize deployments based on context or user input.

Use Go template operators​

Go template operators let you inject variables, conditions, and loops into text files to generate YAML or HTML dynamically. They don't use == syntax but use prefix operators like and, eq, and gt as follows:

Use eq operator
{{- if and (eq .Values.env "prod") (gt .Values.replicas 2) }}
replicas: {{ .Values.replicas }}
{{- else }}
replicas: 1
{{- end }}

You can find more information here. Below is an example of using eq operator:

Use eq operator to enable or disable ingresses synchronization
apiVersion: management.loft.sh/v1
kind: VirtualClusterTemplate
metadata:
name: vcluster-pro-template
spec:
displayName: Virtual Cluster Pro Template
template:
helmRelease:
values:
sync:
toHost:
ingresses:
{{ if eq .Values.loft.project "auth-core" }}
enabled: true
{{ else }}
enabled: false
{{ end }}

Use template variables​

The vCluster Platform provides platform-specific parameter values and you can use these parameter values to render the template file dynamically:

Use template variables to fill the configuration
metadata:
name: "{{ .Values.loft.virtualClusterName }}"
spec:
ingress:
host: "{{ .Values.loft.virtualClusterName }}.example.com"
parameters:
cpu: "{{ .Values.cpuLimit }}"

For example, You can use connected host cluster parameter values to specify the sleep mode timezone automatically:

sleepMode:
enabled: true
autoSleep:
afterInactivity: "35m"
timeZone: '{{ .Values.loft.clusterAnnotations.sleepTimeZone }}'