Skip to content

Commit 52c4c34

Browse files
committed
Make it opt-out, DefaulterRemoveUnknownFields
1 parent 85e50cf commit 52c4c34

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

pkg/webhook/admission/defaulter_custom.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ type CustomDefaulter interface {
3737
}
3838

3939
type defaulterOptions struct {
40-
preserveUnknownFields bool
40+
removeUnknownFields bool
4141
}
4242

4343
type defaulterOption func(*defaulterOptions)
4444

45-
// DefaulterPreserveUnknownFields stops the defaulter from pruning the fields that are not recognized in the local scheme.
46-
func DefaulterPreserveUnknownFields(o *defaulterOptions) {
47-
o.preserveUnknownFields = true
45+
// DefaulterRemoveUnknownFields makes the defaulter prune the fields that are not recognized in the local scheme.
46+
func DefaulterRemoveUnknownFields(o *defaulterOptions) {
47+
o.removeUnknownFields = true
4848
}
4949

5050
// WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
@@ -54,15 +54,15 @@ func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter C
5454
o(options)
5555
}
5656
return &Webhook{
57-
Handler: &defaulterForType{object: obj, defaulter: defaulter, decoder: NewDecoder(scheme), preserveUnknownFields: options.preserveUnknownFields},
57+
Handler: &defaulterForType{object: obj, defaulter: defaulter, decoder: NewDecoder(scheme), removeUnknownFields: options.removeUnknownFields},
5858
}
5959
}
6060

6161
type defaulterForType struct {
62-
defaulter CustomDefaulter
63-
object runtime.Object
64-
decoder Decoder
65-
preserveUnknownFields bool
62+
defaulter CustomDefaulter
63+
object runtime.Object
64+
decoder Decoder
65+
removeUnknownFields bool
6666
}
6767

6868
// Handle handles admission requests.
@@ -97,7 +97,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
9797

9898
// Keep a copy of the object if needed
9999
var originalObj runtime.Object
100-
if h.preserveUnknownFields {
100+
if !h.removeUnknownFields {
101101
originalObj = obj.DeepCopyObject()
102102
}
103103

@@ -117,7 +117,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
117117
}
118118

119119
handlerResponse := PatchResponseFromRaw(req.Object.Raw, marshalled)
120-
if h.preserveUnknownFields {
120+
if !h.removeUnknownFields {
121121
handlerResponse = h.dropSchemeRemovals(handlerResponse, originalObj, req.Object.Raw)
122122
}
123123
return handlerResponse

pkg/webhook/admission/defaulter_custom_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var _ = Describe("Defaulter Handler", func() {
3131

3232
It("should not preserve unknown fields by default", func() {
3333
obj := &TestDefaulter{}
34-
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{})
34+
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{}, DefaulterRemoveUnknownFields)
3535

3636
resp := handler.Handle(context.TODO(), Request{
3737
AdmissionRequest: admissionv1.AdmissionRequest{
@@ -63,7 +63,7 @@ var _ = Describe("Defaulter Handler", func() {
6363

6464
It("should preserve unknown fields when DefaulterPreserveUnknownFields is passed", func() {
6565
obj := &TestDefaulter{}
66-
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{}, DefaulterPreserveUnknownFields)
66+
handler := WithCustomDefaulter(admissionScheme, obj, &TestCustomDefaulter{})
6767

6868
resp := handler.Handle(context.TODO(), Request{
6969
AdmissionRequest: admissionv1.AdmissionRequest{

0 commit comments

Comments
 (0)