@@ -37,17 +37,17 @@ type CustomDefaulter interface {
37
37
}
38
38
39
39
type defaulterOptions struct {
40
- removeUnknownFields bool
40
+ removeUnknownOrOmitableFields bool
41
41
}
42
42
43
43
// DefaulterOption defines the type of a CustomDefaulter's option
44
44
type DefaulterOption func (* defaulterOptions )
45
45
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
51
51
}
52
52
53
53
// WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
@@ -57,15 +57,20 @@ func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter C
57
57
o (options )
58
58
}
59
59
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
+ },
61
66
}
62
67
}
63
68
64
69
type 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
69
74
}
70
75
71
76
// Handle handles admission requests.
@@ -100,7 +105,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
100
105
101
106
// Keep a copy of the object if needed
102
107
var originalObj runtime.Object
103
- if ! h .removeUnknownFields {
108
+ if ! h .removeUnknownOrOmitableFields {
104
109
originalObj = obj .DeepCopyObject ()
105
110
}
106
111
@@ -120,7 +125,7 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
120
125
}
121
126
122
127
handlerResponse := PatchResponseFromRaw (req .Object .Raw , marshalled )
123
- if ! h .removeUnknownFields {
128
+ if ! h .removeUnknownOrOmitableFields {
124
129
handlerResponse = h .dropSchemeRemovals (handlerResponse , originalObj , req .Object .Raw )
125
130
}
126
131
return handlerResponse
0 commit comments