@@ -113,6 +113,12 @@ var (
113
113
gvr ("" , "v1" , "services/proxy" ): {"*" : testSubresourceProxy },
114
114
}
115
115
116
+ // admissionExemptResources lists objects which are exempt from admission validation/mutation,
117
+ // only resources exempted from admission processing by API server should be listed here.
118
+ admissionExemptResources = map [schema.GroupVersionResource ]bool {
119
+ gvr ("admissionregistration.k8s.io" , "v1beta1" , "mutatingwebhookconfigurations" ): true ,
120
+ gvr ("admissionregistration.k8s.io" , "v1beta1" , "validatingwebhookconfigurations" ): true ,
121
+ }
116
122
// excludedResources lists resources / verb combinations that are not yet tested. this set should trend to zero.
117
123
excludedResources = map [schema.GroupVersionResource ]sets.String {
118
124
// TODO: verify non-persisted review objects work with webhook admission in place (and determine whether they should be sent to admission)
@@ -126,10 +132,6 @@ var (
126
132
gvr ("authorization.k8s.io" , "v1beta1" , "subjectaccessreviews" ): sets .NewString ("*" ),
127
133
gvr ("authorization.k8s.io" , "v1beta1" , "selfsubjectaccessreviews" ): sets .NewString ("*" ),
128
134
gvr ("authorization.k8s.io" , "v1beta1" , "selfsubjectrulesreviews" ): sets .NewString ("*" ),
129
-
130
- // TODO: webhook config objects are not subject to admission, verify CRUD works and webhooks do not observe them
131
- gvr ("admissionregistration.k8s.io" , "v1beta1" , "mutatingwebhookconfigurations" ): sets .NewString ("*" ),
132
- gvr ("admissionregistration.k8s.io" , "v1beta1" , "validatingwebhookconfigurations" ): sets .NewString ("*" ),
133
135
}
134
136
135
137
parentResources = map [schema.GroupVersionResource ]schema.GroupVersionResource {
@@ -142,11 +144,12 @@ type holder struct {
142
144
143
145
t * testing.T
144
146
145
- expectGVR metav1.GroupVersionResource
147
+ recordGVR metav1.GroupVersionResource
148
+ recordOperation v1beta1.Operation
149
+ recordNamespace string
150
+ recordName string
151
+
146
152
expectGVK schema.GroupVersionKind
147
- expectOperation v1beta1.Operation
148
- expectNamespace string
149
- expectName string
150
153
expectObject bool
151
154
expectOldObject bool
152
155
@@ -157,11 +160,11 @@ func (h *holder) reset(t *testing.T) {
157
160
h .lock .Lock ()
158
161
defer h .lock .Unlock ()
159
162
h .t = t
160
- h .expectGVR = metav1.GroupVersionResource {}
163
+ h .recordGVR = metav1.GroupVersionResource {}
161
164
h .expectGVK = schema.GroupVersionKind {}
162
- h .expectOperation = ""
163
- h .expectName = ""
164
- h .expectNamespace = ""
165
+ h .recordOperation = ""
166
+ h .recordName = ""
167
+ h .recordNamespace = ""
165
168
h .expectObject = false
166
169
h .expectOldObject = false
167
170
h .recorded = map [string ]* v1beta1.AdmissionRequest {
@@ -177,11 +180,11 @@ func (h *holder) expect(gvr schema.GroupVersionResource, gvk schema.GroupVersion
177
180
178
181
h .lock .Lock ()
179
182
defer h .lock .Unlock ()
180
- h .expectGVR = metav1.GroupVersionResource {Group : gvr .Group , Version : gvr .Version , Resource : gvr .Resource }
183
+ h .recordGVR = metav1.GroupVersionResource {Group : gvr .Group , Version : gvr .Version , Resource : gvr .Resource }
181
184
h .expectGVK = gvk
182
- h .expectOperation = operation
183
- h .expectName = name
184
- h .expectNamespace = namespace
185
+ h .recordOperation = operation
186
+ h .recordName = name
187
+ h .recordNamespace = namespace
185
188
h .expectObject = object
186
189
h .expectOldObject = oldObject
187
190
h .recorded = map [string ]* v1beta1.AdmissionRequest {
@@ -203,22 +206,22 @@ func (h *holder) record(phase string, request *v1beta1.AdmissionRequest) {
203
206
if len (request .SubResource ) > 0 {
204
207
resource .Resource += "/" + request .SubResource
205
208
}
206
- if resource != h .expectGVR {
209
+ if resource != h .recordGVR {
207
210
if debug {
208
- h .t .Log (resource , "!=" , h .expectGVR )
211
+ h .t .Log (resource , "!=" , h .recordGVR )
209
212
}
210
213
return
211
214
}
212
215
213
- if request .Operation != h .expectOperation {
216
+ if request .Operation != h .recordOperation {
214
217
if debug {
215
- h .t .Log (request .Operation , "!=" , h .expectOperation )
218
+ h .t .Log (request .Operation , "!=" , h .recordOperation )
216
219
}
217
220
return
218
221
}
219
- if request .Namespace != h .expectNamespace {
222
+ if request .Namespace != h .recordNamespace {
220
223
if debug {
221
- h .t .Log (request .Namespace , "!=" , h .expectNamespace )
224
+ h .t .Log (request .Namespace , "!=" , h .recordNamespace )
222
225
}
223
226
return
224
227
}
@@ -227,9 +230,9 @@ func (h *holder) record(phase string, request *v1beta1.AdmissionRequest) {
227
230
if name == "" && request .Object .Object != nil {
228
231
name = request .Object .Object .(* unstructured.Unstructured ).GetName ()
229
232
}
230
- if name != h .expectName {
233
+ if name != h .recordName {
231
234
if debug {
232
- h .t .Log (name , "!=" , h .expectName )
235
+ h .t .Log (name , "!=" , h .recordName )
233
236
}
234
237
return
235
238
}
@@ -250,6 +253,14 @@ func (h *holder) verify(t *testing.T) {
250
253
}
251
254
252
255
func (h * holder ) verifyRequest (request * v1beta1.AdmissionRequest ) error {
256
+ // Check if current resource should be exempted from Admission processing
257
+ if admissionExemptResources [gvr (h .recordGVR .Group , h .recordGVR .Version , h .recordGVR .Resource )] {
258
+ if request == nil {
259
+ return nil
260
+ }
261
+ return fmt .Errorf ("admission webhook was called, but not supposed to" )
262
+ }
263
+
253
264
if request == nil {
254
265
return fmt .Errorf ("no request received" )
255
266
}
0 commit comments