π Fix normalize {} to [] for slice defaults in CRDs
What this fixes
Right now if you set a default with an inline object that contains a slice, and use {} as the value, the generator ends up treating it as an object instead of an empty list.
For example, if I use:
// +kubebuilder:default:={"md0":{"ephemeralStorage":"20Gi","gpus":{},"instanceType":"u1.medium","maxReplicas":10,"minReplicas":0,"resources":{},"roles":{"ingress-nginx"}}}
the generated CRD gives me:
default:
md0:
ephemeralStorage: 20Gi
gpus: {}
instanceType: u1.medium
maxReplicas: 10
minReplicas: 0
resources: {}
roles:
- ingress-nginx
but since gpus is a slice, what I actually want (and what users would expect) is:
default:
md0:
ephemeralStorage: 20Gi
gpus: []
instanceType: u1.medium
maxReplicas: 10
minReplicas: 0
resources: {}
roles:
- ingress-nginx
What changed
- Added a normalization step in ApplyToSchema that looks at the target schema type and coerces {} β [] when the field is an array (and vice versa for objects).
- This runs recursively, so nested defaults inside maps/objects are also fixed.
- Extended the testdata with cases for nested slices inside maps/objects to make sure the behavior is covered.
Why
Itβs a small thing, but it avoids confusion when writing defaults for slices. Before this patch, the CRD schema would show gpus: {}, which is wrong and makes clients unhappy. Now it consistently produces [].
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by: kvaps Once this PR has been reviewed and has the lgtm label, please assign vincepri for approval. For more information see the Code Review Process.
The full list of commands accepted by this bot can be found here.
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
PR needs rebase.
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.