crossplane-contrib / function-go-templating

A Go templating composition function
https://crossplane.io
Apache License 2.0
37 stars 26 forks source link

feat: add extra resources support #83

Open phisco opened 2 months ago

phisco commented 2 months ago

Description of your changes

Fixes #77

I have:

phisco commented 2 months ago

misses an example, I'll try to find a meaningful one.

danielloader commented 1 month ago

misses an example, I'll try to find a meaningful one.

If you want an example the ability to provision an s3 bucket external to the composition (in a composition of its own or not) and be able to add bucket notifications from another composition would be a good way to demonstrate a one to many relationship.

Often one bucket will have a dozen notification handlers into SQS queues etc.

danielloader commented 1 month ago

Comments from my day of using this branch:

Ideas to track, if they should become issues or not is up to you:

Otherwise it's worked as advertised and enabled the workflows I hoped it would!

dariozachow commented 1 month ago

Is it planned to merge this one soon?

markussiebert commented 1 month ago

Can you provide a full working example? I can't get this running ...

OK - you have to use Crossplane > 15 in order to use extraResources :-/

markussiebert commented 1 month ago

My problem is, that I am able to retrieve extraResources with the function, but it's not possible to use them as described:

cannot compose resources: pipeline step "go-templating" returned a fatal result: cannot execute template: template: manifests:10:26: executing "manifests" at <index .extraResources "namespaces">

with this template:

apiVersion: 'meta.gotemplating.fn.crossplane.io/v1alpha1'
kind: 'ExtraResources'
requirements:
  namespaces:
    apiVersion: 'kubernetes.crossplane.io/v1alpha2'
    kind: 'Object'
    matchLabels:
      'something/claimRefNamespace': 'string'
---
{{ $someExtraResources := index .extraResources "namespaces" }}
{{- range $i, $extraResource := $someExtraResources.items }}
apiVersion: kubernetes.crossplane.io/v1alpha1
kind: Object
metadata:
  annotations:
    gotemplating.fn.crossplane.io/composition-resource-name: default-serviceaccount-prod
spec:
  forProvider:
    manifest:
      apiVersion: v1
      kind: Configmap
      metadata:
        name: $extraResource.resource.metadata.name
        namespace: default
      data:
        somekey: value
  providerConfigRef:
    name: "kubernetes"
{{- end }}

So we do encapsulate everything in {{ with .extreResources }} to make it work

So all in all - nice work - in our tests this just works.

take-five commented 1 month ago

This seems super useful! Will this be merged soon?

markussiebert commented 1 month ago

Sorry - have the question, does this PR has any future?

phisco commented 1 month ago

Yes, @markussiebert, I only have to find the time to refine it a bit further.