-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛 Fix custom defaulter: avoid deleting unknown fields #2931
Closed
alculquicondor
wants to merge
1
commit into
kubernetes-sigs:main
from
alculquicondor:defaulter_losing_fields
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||
package admission | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"net/http" | ||||||
|
||||||
. "github.com/onsi/ginkgo/v2" | ||||||
. "github.com/onsi/gomega" | ||||||
"gomodules.xyz/jsonpatch/v2" | ||||||
|
||||||
admissionv1 "k8s.io/api/admission/v1" | ||||||
"k8s.io/apimachinery/pkg/runtime" | ||||||
"k8s.io/apimachinery/pkg/runtime/schema" | ||||||
) | ||||||
|
||||||
var _ = Describe("CustomDefaulter Handler", func() { | ||||||
|
||||||
It("should should not lose unknown fields", func() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:) |
||||||
obj := &TestObject{} | ||||||
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{}) | ||||||
|
||||||
resp := handler.Handle(context.TODO(), Request{ | ||||||
AdmissionRequest: admissionv1.AdmissionRequest{ | ||||||
Operation: admissionv1.Create, | ||||||
Object: runtime.RawExtension{ | ||||||
Raw: []byte(`{"newField":"foo"}`), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
Expect(resp.Allowed).Should(BeTrue()) | ||||||
Expect(resp.Patches).To(Equal([]jsonpatch.JsonPatchOperation{{ | ||||||
Operation: "add", | ||||||
Path: "/replica", | ||||||
Value: 2.0, | ||||||
}})) | ||||||
Expect(resp.Result.Code).Should(Equal(int32(http.StatusOK))) | ||||||
}) | ||||||
|
||||||
It("should return ok if received delete verb in defaulter handler", func() { | ||||||
obj := &TestObject{} | ||||||
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{}) | ||||||
|
||||||
resp := handler.Handle(context.TODO(), Request{ | ||||||
AdmissionRequest: admissionv1.AdmissionRequest{ | ||||||
Operation: admissionv1.Delete, | ||||||
OldObject: runtime.RawExtension{ | ||||||
Raw: []byte("{}"), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
Expect(resp.Allowed).Should(BeTrue()) | ||||||
Expect(resp.Result.Code).Should(Equal(int32(http.StatusOK))) | ||||||
}) | ||||||
|
||||||
}) | ||||||
|
||||||
type TestCustomDefaulter struct{} | ||||||
|
||||||
func (d *TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error { | ||||||
tObj := obj.(*TestObject) | ||||||
if tObj.Replica < 2 { | ||||||
tObj.Replica = 2 | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
type TestObject struct { | ||||||
Replica int `json:"replica,omitempty"` | ||||||
} | ||||||
|
||||||
func (o *TestObject) GetObjectKind() schema.ObjectKind { return o } | ||||||
func (o *TestObject) DeepCopyObject() runtime.Object { | ||||||
return &TestObject{ | ||||||
Replica: o.Replica, | ||||||
} | ||||||
} | ||||||
|
||||||
func (o *TestObject) GroupVersionKind() schema.GroupVersionKind { | ||||||
return testDefaulterGVK | ||||||
} | ||||||
|
||||||
func (o *TestObject) SetGroupVersionKind(gvk schema.GroupVersionKind) {} | ||||||
|
||||||
type TestObjectList struct{} | ||||||
|
||||||
func (*TestObjectList) GetObjectKind() schema.ObjectKind { return nil } | ||||||
func (*TestObjectList) DeepCopyObject() runtime.Object { return nil } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think this can lead to invalid patches. Now we are calculating patches based on how original looks like after unmarshal + marshal. But the apiserver will have to apply the patches on the original JSON object
(We had problems like this when implementing our own patch calculation like it is suggested here on this PR)
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.
Example: Imagine an API type that through (probably incorrect) omitempty / pointer/non-pointer fields will add additional fields to marshalledOriginal. This could be e.g.:
{}
.Now the resulting patches will:
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 see your point, but both the existing and the proposed approaches are buggy.
Generally, the apiserver would have to go through the same unmarshal + marshal process, then the "raw" contents should not differ. A problem like the one you mention could arise when the webhook uses newer APIs than the API server has registered.
The problem I present arises when the API server has newer APIs than the webhook.
Not sure which can be more common.
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.
For CRDs the apiserver doesn't even have the Go API types :)
Yeah it's definitely a tricky problem. I just think that if we now switch from one buggy version to another we will break a lot of people that currently have (somewhat) working implementations with controller-runtime
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 please look at #2932?