crossplane / crossplane/function-sdk-go

Filtering Empty Objects in SDK to Avoid Invalid Resource Generation/Apply

Open
#154 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
36
Forks
29
Avg merge
1d 8h
Merged PRs (30d)
10

Description

### What happened?

When using for example function-kcl to create resources, the following pattern can lead to the generation of invalid resources that result in client-side `crossplane beta render` or server-side errors during the application:

```
_items = [{
apiVersion: "s3.aws.upbound.io/v1beta1"
kind: "Bucket"
metadata: _metadata("bucket")
spec: _defaults
}]

_items += [{
apiVersion: "s3.aws.upbound.io/v1beta1"
kind: "BucketVersioning"
metadata: _metadata("bucketVersioning")
spec: _defaults | {
forProvider: {
bucketSelector: {
matchControllerRef: True
},
versioningConfiguration: [
{
status: "Enabled"
},
],
},
}
} if get(oxr, "spec.parameters.versioning", "False") else {}]

items = _items
```

This approach can produce an invalid resource object like the one shown below if the conditional logic produces an empty object (`{}`):

```
---
metadata:
annotations:
crossplane.io/composition-resource-name: ""
generateName: configuration-
labels:
crossplane.io/composite: configuration
ownerReferences:
- apiVersion: example.org/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Template
name: configuration
uid: ""
```

The issue arises because the condition for else produces `{}`, which is added to the `_items` list. This can lead to errors during the resource application process.

To mitigate this, every individual composition currently requires an additional filtering step:

```
items = [
i for r in [_items]
for i in r if i
]
```

Can the SDK be enhanced to automatically filter out these invalid empty object resources? This would simplify the composition logic and prevent potential errors during resource application.

This issue has been discussed in the KCL community. You can find the discussion here: https://cloud-native.slack.com/archives/C05TC96NWN8/p1723020486395609

### How can we reproduce it?

### What environment did it happen in?
Crossplane version:

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.