crossplane-contrib / function-go-templating

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

No namespaced MRs? #98

Closed milkpirate closed 4 months ago

milkpirate commented 4 months ago

What happened?

I tried to create a namespace scoped resouce.

How can we reproduce it?

With this (really) minimal example:

composition.yaml

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: xfoo
spec:
  compositeTypeRef:
    apiVersion: bar.private/v1alpha1
    kind: Foo

  mode: Pipeline
  pipeline:
  - step: sa
    functionRef:
      name: function-go-templating
    input:
      apiVersion: gotemplating.fn.crossplane.io/v1beta1
      kind: GoTemplate
      source: Inline
      inline:
        template: |
          {{ $xr := .observed.composite.resource }}
          {{ $name := $xr.metadata.name }}

          apiVersion: v1
          kind: ServiceAccount
          metadata:
            name: sa-{{ $name }}
            namespace: crossplane-system
            annotations:
              gotemplating.fn.crossplane.io/composition-resource-name: some-sa

  writeConnectionSecretsToNamespace: crossplane-system

xrd.yaml

apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: bar.private
spec:
  group: bar.private
  names:
    kind: Foo
    plural: foos
  versions:
  - name: v1alpha1
    served: true
    referenceable: true
    schema:
      openAPIV3Schema:  {}

claim.yaml

apiVersion: bar.private/v1alpha1
kind: Foo
metadata:
  name: example

Then I get:

$ kgf cfsetup/claim.yaml -oyaml
...
status:
  conditions:
  - lastTransitionTime: "2024-05-21T20:41:51Z"
    message: 'cannot compose resources: cannot update composite resource spec.resourceRefs:
      failed to create typed patch object (/example; bar.private/v1alpha1,
      Kind=Foo): .spec.resourceRefs[0].namespace: field not declared in schema'
    reason: ReconcileError
    status: "False"
    type: Synced

But if I remove the namespace from the SA it for sure complains about it missing. What am I doing wrong?

What environment did it happen in?

Function version:

phclark commented 4 months ago

I don't believe this is a bug, so much as a limitation to how Crossplane creates Managed Resources, to accomplish this, you'll want to wrap your Kube resource in a provider-kubernetes Object

phisco commented 4 months ago

Exactly as @phclark said. Closing this. 🙏