function-sdk-go
function-sdk-go copied to clipboard
Filtering Empty Objects in SDK to Avoid Invalid Resource Generation/Apply
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: