controller-tools icon indicating copy to clipboard operation
controller-tools copied to clipboard

πŸ› Fix normalize {} to [] for slice defaults in CRDs

Open kvaps opened this issue 3 months ago β€’ 2 comments

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 [].

kvaps avatar Sep 17 '25 18:09 kvaps

[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.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

k8s-ci-robot avatar Sep 17 '25 18:09 k8s-ci-robot

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.

k8s-ci-robot avatar Sep 24 '25 17:09 k8s-ci-robot