crossplane-contrib / function-patch-and-transform

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

Issues appending to an array with patch policy #127

Open annapurnachunduri opened 3 months ago

annapurnachunduri commented 3 months ago

Hi all,

I am new to crossplane and usage of functions so looking for some guidance here. Thank you in advance!

In our composition, we have a resource created in the first pipeline step using go-templating. The resource has spec.forProvider.items field which expects an array input. When the resource is first created in the go-templating step there exists one item in the array (lets call it item-1). There after, we have a 2nd step in the pipeline which implements function P&T to append spec.forProvider.items field/array with one more item listed in the XR/Claim (lets call it item-2).

Here is the code for the function P&T step:

step: policy patch functionRef: name: function-patch-and-transform input: apiVersion: pt.fn.crossplane.io/v1beta1 kind: Resources resources: - name: resource-1 patches: - type: CombineFromComposite toFieldPath: spec.forProvider.items combine: variables: - fromFieldPath: spec.parameters.newItem strategy: string string: fmt: "%s-2" transforms: - type: string string: type: Convert convert: "ToJson" - type: convert convert: toType: array format: json policy: toFieldPath: ForceMergeObjectsAppendArrays

Snapshot of the expected output in the resource: spec: forProvider: items: - item-1 - item-2

Output/Errors encountered:

  1. Without the transforms applied to the patch we see the error cannot apply composed resource "resource-1": failed to create typed patch object: .spec.forProvider.items: expected list, got &{item-2}
  2. With the transforms applied we see the error cannot compose resources: pipeline step returned a fatal result: cannot render composed resource "resource-1" "CombineFromComposite" patch at index 0 : transform at index 1 returned error: convert transform could not resolve: json: cannot unmarshal string into Go value of type []interface {}
  3. When trying to look around the repo I noticed patches_test.go doesn't test this case. Just one test case to test de-duplication.
  4. When testing this with crossplane beta render we noticed when the transforms are not applied the output is generated but with the default Replace policy applied to the patch (output is items: item-2) instead of the output items: [item-1, item-2].
gschei commented 1 month ago

The mergeOptions are not considered for CombineFromComposite, I created a PR for this: https://github.com/crossplane-contrib/function-patch-and-transform/pull/141

@phisco I think it was forgotten, please check