go composition function: observed XR managedFields leak into SSA status patch
- Dominant language
- Go
- Stars
- 19
- Forks
- 31
- Avg merge
- 3d 4h
- Merged PRs (30d)
- 52
Description
### What happened?
I am using the Crossplane project sturcture generating the go structs for the composed and composite
resources. The object retains the ManagedFields from the request and when set to the response via
```go
mapAny, err := runtime.DefaultUnstructuredConverter.ToUnstructured(goStructOfComposite)
unstr := unstructured.Unstructured{Object: mapAny}
response.SetDesiredCompositeResource(rsp, &resource.Composite{Resource: &composite.Unstructured{Unstructured: unstr}}),
```
the `metadata.managedFields` populated by the API server survive into the SSA patch
body, and Kubernetes rejects it
**Below is an analysis by Claude:**
When a Go composition function round-trips the observed XR through a generated
struct and calls `response.SetDesiredCompositeResource`, the
`metadata.managedFields` populated by the API server survive into the SSA patch
body, and Kubernetes rejects it:
```
cannot apply composite resource status: metadata.managedFields must be nil
```
[`AsStruct()` (L52–76)](https://github.com/crossplane/crossplane/blob/main/internal/xfn/utils.go#L52-L76)
copies the full observed XR backing map into the `RunFunctionRequest` proto
without stripping `managedFields`. Generated ObjectMeta structs include
`ManagedFields *[]ManagedFieldsEntry`, so the field survives the Go struct
round-trip and reaches the
[SSA patch in `composition_functions.go`](https://github.com/crossplane/crossplane/blob/main/internal/controller/apiextensions/composite/composition_functions.go),
where Kubernetes rejects it.
`crossplane render` does not populate `managedFields`
in the observed XR proto, so render passes while the cluster fails.
**Workaround:** call `unstr.SetManagedFields(nil)` before `SetDesiredCompositeResource`.
Related: crossplane/crossplane#7453
What environment did it happen in?
Crossplane version: v2.3.2, v2.3.
Contributor guide
Assessment
This issue has not been assessed yet.