-
Notifications
You must be signed in to change notification settings - Fork 425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Add support for empty maps or lists #863
🐛 Add support for empty maps or lists #863
Conversation
What is the motivation here, it's unclear to me why you'd want to default to an empty struct? |
The motivation here is to fix a small bug here.
This is more wasteful to the storage space, but if this is needed, controller-gen would support this. |
You must default to an empty object to activate the defaults deep in the object. This is intentionally the way this works in CRDs. Compare https://github.com/kube-bind/kube-bind/blob/main/deploy/patches/kube-bind.io_apiserviceexportrequests.yaml-patch. |
This is kinda what I was looking for, some proof that defaulting to the empty object would allow recursive defaults to work. I wasn't sure if that's how the API server worked or not. As we have an example of that, I have no objection to fixing this bug. |
I think we should update the cronjob types and the tests that rely on them to cover this case at the integration layer as well |
681f4d1
to
000c63f
Compare
Added a test case in the cronjobs testdata, hope this look good. |
@@ -297,6 +309,11 @@ type MinMaxObject struct { | |||
Baz string `json:"baz,omitempty"` | |||
} | |||
|
|||
type EmpiableObject struct { | |||
Foo string `json:"foo,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a default to the field so that we can prove that the empty default on the parent triggers the inner field to be defaulted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So with a modified version of CRD, where only these 2 new fields are present, this is how the applied CR looks like:
kind: CronJob
apiVersion: testdata.kubebuilder.io/v1
metadata:
name: test
namespace: default
spec: {}
results in
apiVersion: testdata.kubebuilder.io/v1
kind: CronJob
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"testdata.kubebuilder.io/v1","kind":"CronJob","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{}}
creationTimestamp: "2023-11-24T16:21:31Z"
generation: 1
name: test
namespace: default
resourceVersion: "23165"
uid: d86f95e1-54be-4e04-b789-cb997bc3dd17
spec:
defaultedEmptyObject:
foo: forty-two
defaultedEmptySlice: []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JoelSpeed Do you think all the comments are addressed? This is quite useful functionality, and it would be best to avoid manually patching resource, and rely on the annotation instead.
@@ -120,6 +120,18 @@ type CronJobSpec struct { | |||
// +kubebuilder:example={{nested: {foo: "baz", bar: true}},{nested: {bar: false}}} | |||
DefaultedObject []RootObject `json:"defaultedObject"` | |||
|
|||
// This tests that empty slice defaulting can be performed. | |||
// +kubebuilder:default={} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is {}
a valid default for slice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, this needs to be fixed. CRD does not complain for deafult: [] though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything is fixed in the current commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is strange. But I guess fine given it's not JSON but our custom syntax (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's strange, but too late to change it now? Probably a reason for not using the obvious []
for arrays in marker syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup seems definitely too late to change it now. Given that non-empty arrays worked already in the past :)
000c63f
to
5af596e
Compare
5af596e
to
2fac91e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Danil-Grigorev! This looks like an important fix to me.
/lgtm
@@ -120,6 +120,18 @@ type CronJobSpec struct { | |||
// +kubebuilder:example={{nested: {foo: "baz", bar: true}},{nested: {bar: false}}} | |||
DefaultedObject []RootObject `json:"defaultedObject"` | |||
|
|||
// This tests that empty slice defaulting can be performed. | |||
// +kubebuilder:default={} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm Thanks @Danil-Grigorev |
/approve Thanks! |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Danil-Grigorev, joelanford The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes: #550
This PR adds support for
kubebuilder:default={}
type of annotations in controller tools.