Skip to content

Commit

Permalink
Make it opt-out, DefaulterRemoveUnknownFields
Browse files Browse the repository at this point in the history
  • Loading branch information
trasc committed Oct 25, 2024
1 parent 822e4f5 commit e652815
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions pkg/webhook/admission/defaulter_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ type CustomDefaulter interface {
}

type defaulterOptions struct {
preserveUnknownFields bool
removeUnknownFields bool
}

type defaulterOption func(*defaulterOptions)

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

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

type defaulterForType struct {
defaulter CustomDefaulter
object runtime.Object
decoder Decoder
preserveUnknownFields bool
defaulter CustomDefaulter
object runtime.Object
decoder Decoder
removeUnknownFields bool
}

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

// Keep a copy of the object if needed
var originalObj runtime.Object
if h.preserveUnknownFields {
if !h.removeUnknownFields {
originalObj = obj.DeepCopyObject()
}

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

handlerResponse := PatchResponseFromRaw(req.Object.Raw, marshalled)
if h.preserveUnknownFields {
if !h.removeUnknownFields {
handlerResponse = h.dropSchemeRemovals(handlerResponse, originalObj, req.Object.Raw)
}
return handlerResponse
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/admission/defaulter_custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = Describe("Defaulter Handler", func() {

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

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

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

resp := handler.Handle(context.TODO(), Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Expand Down

0 comments on commit e652815

Please sign in to comment.