Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ObjectTemplate variables #101

Open
erikgb opened this issue Jul 9, 2024 · 0 comments
Open

Feature: ObjectTemplate variables #101

erikgb opened this issue Jul 9, 2024 · 0 comments

Comments

@erikgb
Copy link
Contributor

erikgb commented Jul 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant