Skip to main content
Version: v4.12 Stable

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 tenant clusters, namespaces, applications, and Stacks. 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 template resources for each workload type, including VirtualClusterTemplate and StackTemplate. For more information, see Creating Templates.

When defining a VirtualClusterTemplate resource, 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 control plane cluster parameter values to specify the sleep mode timezone automatically:

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

Template values and outputs in Stacks​

A Stack task can use StackInstance parameters through {{ .Values.<name> }}. It can also consume a captured value from an earlier task through {{ .Outputs.<task>.<output> }}. The consuming task must list the producing task in dependsOn.

tasks:
- name: database
app:
templateRef:
name: postgresql
outputs:
- name: address
fromResource:
apiVersion: v1
kind: Service
namespace: database
name: postgresql
jsonPath: "{.spec.clusterIP}"
- name: api
dependsOn:
- database
app:
templateRef:
name: example-api
parameters:
environment: "{{ .Values.environment }}"
databaseAddress: "{{ .Outputs.database.address }}"

Stack parameters and outputs are rendering contexts, not general Go template functions. See What are Stacks for the task orchestration concept. For their validation rules and supported locations, see Create a Stack template.