@@ -37,17 +37,17 @@ type CustomDefaulter interface {
3737}
3838
3939type defaulterOptions struct {
40- removeUnknownFields bool
40+ removeUnknownOrOmitableFields bool
4141}
4242
4343// DefaulterOption defines the type of a CustomDefaulter's option
4444type DefaulterOption func (* defaulterOptions )
4545
46- // DefaulterRemoveUnknownFields makes the defaulter prune fields that are in the json object retrieved by the
47- // webhook but not in the local go type. This happens for example when the CRD in the apiserver has fields that
48- // our go type doesn't know about, because it's outdated.
49- func DefaulterRemoveUnknownFields (o * defaulterOptions ) {
50- o .removeUnknownFields = true
46+ // DefaulterRemoveUnknownOrOmitableFields makes the defaulter prune fields that are in the json object retrieved by the
47+ // webhook but not in the local go type json representation . This happens for example when the CRD in the apiserver has
48+ // fields that our go type doesn't know about, because it's outdated, or the field has a zero value and is `omitempty` .
49+ func DefaulterRemoveUnknownOrOmitableFields (o * defaulterOptions ) {
50+ o .removeUnknownOrOmitableFields = true
5151}
5252
5353// WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
@@ -57,15 +57,20 @@ func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter C
5757 o (options )
5858 }
5959 return & Webhook {
60- Handler : & defaulterForType {object : obj , defaulter : defaulter , decoder : NewDecoder (scheme ), removeUnknownFields : options .removeUnknownFields },
60+ Handler : & defaulterForType {
61+ object : obj ,
62+ defaulter : defaulter ,
63+ decoder : NewDecoder (scheme ),
64+ removeUnknownOrOmitableFields : options .removeUnknownOrOmitableFields ,
65+ },
6166 }
6267}
6368
6469type defaulterForType struct {
65- defaulter CustomDefaulter
66- object runtime.Object
67- decoder Decoder
68- removeUnknownFields bool
70+ defaulter CustomDefaulter
71+ object runtime.Object
72+ decoder Decoder
73+ removeUnknownOrOmitableFields bool
6974}
7075
7176// Handle handles admission requests.
@@ -100,7 +105,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
100105
101106 // Keep a copy of the object if needed
102107 var originalObj runtime.Object
103- if ! h .removeUnknownFields {
108+ if ! h .removeUnknownOrOmitableFields {
104109 originalObj = obj .DeepCopyObject ()
105110 }
106111
@@ -120,7 +125,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
120125 }
121126
122127 handlerResponse := PatchResponseFromRaw (req .Object .Raw , marshalled )
123- if ! h .removeUnknownFields {
128+ if ! h .removeUnknownOrOmitableFields {
124129 handlerResponse = h .dropSchemeRemovals (handlerResponse , originalObj , req .Object .Raw )
125130 }
126131 return handlerResponse
0 commit comments