crossplane-contrib / function-patch-and-transform

A patch & transform composition function
https://crossplane.io
Apache License 2.0
23 stars 24 forks source link

fix: allow using function only to patch to and from the environment #122

Closed phisco closed 3 months ago

phisco commented 3 months ago

From https://crossplane.slack.com/archives/CEG3T90A1/p1716529989392019:

Details

Hello, hopefully someone here can point me in the right direction. I'm struggling a bit to understan pipeline context. My current assumption is that I should be able to run a function-patch-and-transform with a ToEnvironmentFieldPath patch to write a value to the context. This is my setup: $ crossplane version Client Version: v1.16.0 Server Version: v1.15.2-up.1 [XR from function-patch-and-transform multistep example](https://github.com/crossplane-contrib/function-patch-and-transform/blob/main/example/multistep/xr.yaml) [functions from function-patch-and-transform multistep example](https://github.com/crossplane-contrib/function-patch-and-transform/blob/main/example/multistep/functions.yaml) [composition from function-patch-and-transform multistep example](https://github.com/crossplane-contrib/function-patch-and-transform/blob/main/example/multistep/composition.yaml) with the addition below: ... - step: patch-and-transform-to-env functionRef: name: function-patch-and-transform input: apiVersion: pt.fn.crossplane.io/v1beta1 kind: Resources resources: - name: bucketACL patches: - type: ToEnvironmentFieldPath fromFieldPath: "spec.acl" toFieldPath: key1 $ crossplane beta render xr.yaml composition.yaml function.yaml --include-context ... 54 │ --- 55 │ apiVersion: render.crossplane.io/v1beta1 56 │ fields: 57 │ [apiextensions.crossplane.io/environment](http://apiextensions.crossplane.io/environment): {} 58 │ kind: Context I'm expecting to see a key1 in apiextensions.crossplane.io/environment, but maybe this is not how it works?

The right approach was to instead define the following:

    - step: patch-and-transform-to-env
      functionRef:
        name: function-patch-and-transform
      input:
        apiVersion: pt.fn.crossplane.io/v1beta1
        kind: Resources
        environment:
          patches:
            - type: FromCompositeFieldPath
              fromFieldPath: spec.acl
              toFieldPath: key1

But it didn't work firstly because not specifying spec.resources wasn't allowed, which shouldn't be the case. But after faking a resource it was still failing because the environment was missing the gvk we expect and that crossplane would have set if it had passed an environment:

crossplane: error: cannot render composite resource: pipeline step "patch-and-transform-to-env" returned a fatal result: cannot apply the "FromCompositeFieldPath" environment patch at index 0: cannot patch to object: Object 'Kind' is missing in '{"key1":"private"}'

This PR fixes both issues.