Skip to content

Feature: ObjectTemplate variables #101

@erikgb

Description

@erikgb

After my POC on dynamic review environments, I encountered unavoidable duplications in my ObjectTemplate:

---
apiVersion: templates.kluctl.io/v1alpha1
kind: ObjectTemplate
metadata:
  name: stas-tenant-review
spec:
  serviceAccountName: gitops-reconciler
  prune: true
  matrix:
    - name: mr
      object:
        ref:
          apiVersion: templates.kluctl.io/v1alpha1
          kind: ListGitlabMergeRequests
          name: stas-tenant
        jsonPath: status.mergeRequests
        expandLists: true
  templates:
    - object:
        apiVersion: accurate.cybozu.com/v2
        kind: SubNamespace
        metadata:
          name: "stas-rev-{{ matrix.mr.source_branch | slugify }}"
    - object:
        apiVersion: source.toolkit.fluxcd.io/v1
        kind: GitRepository
        metadata:
          name: "stas-rev-{{ matrix.mr.source_branch | slugify }}"
        spec:
          interval: 1m0s
          url: https://gitlab.statnett.no/stas/stas-tenant.git
          ref:
            branch: "{{ matrix.mr.source_branch }}"
    - object:
        apiVersion: kustomize.toolkit.fluxcd.io/v1
        kind: Kustomization
        metadata:
          name: "stas-rev-{{ matrix.mr.source_branch | slugify }}"
        spec:
          force: false
          interval: 60m0s
          path: ./review
          prune: true
          retryInterval: 2m0s
          sourceRef:
            kind: GitRepository
            name: "stas-rev-{{ matrix.mr.source_branch | slugify }}"
          targetNamespace: "stas-rev-{{ matrix.mr.source_branch | slugify }}"
          timeout: 3m0s
          wait: true

While this works, I had to repeat the Jinja expression for review namespace multiple times. I wonder if we could/should add a new feature to extract variables that the templates could use. With this example, it could be as simple as:

---
apiVersion: templates.kluctl.io/v1alpha1
kind: ObjectTemplate
metadata:
  name: stas-tenant-review
spec:
  serviceAccountName: gitops-reconciler
  prune: true
  matrix:
    - name: mr
      object:
        ref:
          apiVersion: templates.kluctl.io/v1alpha1
          kind: ListGitlabMergeRequests
          name: stas-tenant
        jsonPath: status.mergeRequests
        expandLists: true
  vars:
    branch: "{{ matrix.mr.source_branch }}"
    namespace:  "stas-rev-{{ matrix.mr.source_branch | slugify }}"   
  templates:

This will make the templates easier to read, reduce the risk of minor errors/typos (Jinjia is hard), and allow for easier patching of OTs with e.g. Kustomize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions