Skip to main content
Version: 0.17.0

Features enterprise

Guide

Currently rendering templates operates in two phases:

  • Generate all template parameters from the configured generators
  • Render all the templates for each set of template parameters

Please read the security information below before using this.

Generation

The simplest generator is the list generator.

apiVersion: templates.weave.works/v1alpha1
kind: GitOpsSet
metadata:
name: gitopsset-sample
spec:
generators:
- list:
elements:
- env: dev
team: dev-team
- env: production
team: ops-team
- env: staging
team: ops-team

The elements in there are a set JSON of objectsyaml, there are three in this example, and each of them has two keys, env and team.

Other generators provide different sets of keys and values.

The generators documentation below provides more information on what the other generators output.

Rendering templates

Templates are Kubernetes resources in YAML format.

Each template is rendered for each element generated by the generators

apiVersion: templates.weave.works/v1alpha1
kind: GitOpsSet
metadata:
name: gitopsset-sample
spec:
generators:
- list:
elements:
- env: dev
team: dev-team
- env: production
team: ops-team
- env: staging
team: ops-team
templates:
- content:
kind: Kustomization
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
metadata:
name: "{{ .element.env }}-demo"
labels:
app.kubernetes.io/name: go-demo
app.kubernetes.io/instance: "{{ .element.env }}"
com.example/team: "{{ .element.team }}"
spec:
interval: 5m
path: "./examples/kustomize/environments/{{ .element.env }}"
prune: true
sourceRef:
kind: GitRepository
name: go-demo-repo

The generated elements are provided to the template in the element scope, so .element.dev refers to the dev field from the List element.

The output from all generators is exposed in the element scope, not just List generators.

Repeating templates

The output from a generator is an array of JSON objectsyaml, the keys of which can contain repeating elements, either further JSON objects, or scalar values.

It can be desirable to repeat a template for a repeated element in a generated value.

apiVersion: templates.weave.works/v1alpha1
kind: GitOpsSet
metadata:
name: repeated-gitopsset-sample
spec:
generators:
- list:
elements:
- env: dev
team: dev-team
teams:
- name: "team1"
- name: "team2"
- name: "team3"
- env: staging
team: staging-team
teams:
- name: "team4"
- name: "team5"
- name: "team6"
templates:
- repeat: "{ .teams }"
content:
kind: ConfigMap
apiVersion: v1
metadata:
name: "{{ .repeat.name }}-demo"
data:
name: "{{ .repeat.name }}-demo"
team: "{{ .element.team }}"

The template repeat field is a JSONPath expression that is applied to each element during the template rendering.

Templates that use repeat will have two separate scopes for the template params, .element which is the top-level element generated by the generator, an additional .repeat scope is provided, which is the repeating element.

In this case, six different ConfigMaps are generated, three for the "dev-team" and three for the "staging-team".


  1. These are written as YAML mappings