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

Provide a way to easily manage custom helpers in templates #168

Open
dukanto opened this issue Feb 18, 2025 · 0 comments
Open

Provide a way to easily manage custom helpers in templates #168

dukanto opened this issue Feb 18, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@dukanto
Copy link

dukanto commented Feb 18, 2025

What problem are you facing?

Sometimes, when developing a composition that creates several MRs (or multiple compositions, for the case), you'll find yourself creating some helper functions in every definition, leading to having the same helper templates over and over on your compositions.
You could try and create a template and use it in FileSystem mode, but then you'll need a pod running for each different object you want to create.

Imagine, for instance:

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: example-function-to-yaml
spec:
  compositeTypeRef:
    apiVersion: example.crossplane.io/v1beta1
    kind: XR
  mode: Pipeline
  pipeline:
    - step: render-deployment-1
      functionRef:
        name: function-go-templating
      input:
        apiVersion: gotemplating.fn.crossplane.io/v1beta1
        kind: GoTemplate
        source: Inline
        inline:
          template: |
            # Defining helper
            {{- define "labels" -}}
            some-text: {{.val1}}
            other-text: {{.val2}}
            {{- end }}
            ---
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              name: nginx-deployment-1
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: test1
              labels:
                {{- include "labels" $vals | nindent 4}}
            spec:
              replicas: 3
              selector:
                matchLabels:
                  {{- include "labels" $vals | nindent 6}}
              [...]

    - step: render-deployment-2
      functionRef:
        name: function-go-templating
      input:
        apiVersion: gotemplating.fn.crossplane.io/v1beta1
        kind: GoTemplate
        source: Inline
        inline:
          template: |
            # Defining helper again
            {{- define "labels" -}}
            some-text: {{.val1}}
            other-text: {{.val2}}
            {{- end }}
            ---
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              name: nginx-deployment-2
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: test2
              labels:
                {{- include "labels" $vals | nindent 4}}
            spec:
              replicas: 3
              selector:
                matchLabels:
                  {{- include "labels" $vals | nindent 6}}
              [...]

As you can see, you are defining "labels" everytime.

How could this Function help solve your problem?

Perhaps, by having some kind of mix, where you could include a helpers path in the FS and then using your inline function on top of that, we can have the best of both ways.
Something like this I think could be a nice approach:

    - step: render-deployment-1
      functionRef:
        name: function-go-templating
      input:
        apiVersion: gotemplating.fn.crossplane.io/v1beta1
        kind: GoTemplate
        source: Inline
        # load all helpers from this directory (which can be loaded into an image)
        helpers:
           dirPath: /helpers
        inline:
          template: |
            ---
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              name: nginx-deployment-1
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: test1
              labels:
                {{- include "labels" $vals | nindent 4}}
            spec:
              replicas: 3
              selector:
                matchLabels:
                  {{- include "labels" $vals | nindent 6}}
              [...]

Let me know if this could be interesting and I am happy to put a PR in place to implement it :)

@dukanto dukanto added the enhancement New feature or request label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant