Debian Codegraph @

pkg/admission/metrics/metrics_test.go

raw
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package metrics
18
19 import (
20 "context"
21 "fmt"
22 "testing"
23 "time"
24
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/runtime/schema"
28 "k8s.io/apiserver/pkg/admission"
29 "k8s.io/component-base/metrics/legacyregistry"
30 )
31
32 var (
33 kind = schema.GroupVersionKind{Group: "kgroup", Version: "kversion", Kind: "kind"}
34 resource = schema.GroupVersionResource{Group: "rgroup", Version: "rversion", Resource: "resource"}
35 attr = admission.NewAttributesRecord(nil, nil, kind, "ns", "name", resource, "subresource", admission.Create, &metav1.CreateOptions{}, false, nil)
36 )
37
38 func TestObserveAdmissionStep(t *testing.T) {
39 defer Metrics.reset()
40 defer legacyregistry.Reset()
41 handler := WithStepMetrics(&mutatingAndValidatingFakeHandler{admission.NewHandler(admission.Create), true, true})
42 if err := handler.(admission.MutationInterface).Admit(context.TODO(), attr, nil); err != nil {
43 t.Errorf("Unexpected error in admit: %v", err)
44 }
45 if err := handler.(admission.ValidationInterface).Validate(context.TODO(), attr, nil); err != nil {
46 t.Errorf("Unexpected error in validate: %v", err)
47 }
48 wantLabels := map[string]string{
49 "operation": string(admission.Create),
50 "type": "admit",
51 "rejected": "false",
52 }
53 expectHistogramCountTotal(t, "apiserver_admission_step_admission_duration_seconds", wantLabels, 1)
54 expectFindMetric(t, "apiserver_admission_step_admission_duration_seconds_summary", wantLabels)
55
56 wantLabels["type"] = "validate"
57 expectHistogramCountTotal(t, "apiserver_admission_step_admission_duration_seconds", wantLabels, 1)
58 expectFindMetric(t, "apiserver_admission_step_admission_duration_seconds_summary", wantLabels)
59 }
60
61 func TestObserveAdmissionController(t *testing.T) {
62 defer Metrics.reset()
63 defer legacyregistry.Reset()
64 handler := WithControllerMetrics(&mutatingAndValidatingFakeHandler{admission.NewHandler(admission.Create), true, true}, "a")
65 if err := handler.(admission.MutationInterface).Admit(context.TODO(), attr, nil); err != nil {
66 t.Errorf("Unexpected error in admit: %v", err)
67 }
68 if err := handler.(admission.ValidationInterface).Validate(context.TODO(), attr, nil); err != nil {
69 t.Errorf("Unexpected error in validate: %v", err)
70 }
71 wantLabels := map[string]string{
72 "name": "a",
73 "operation": string(admission.Create),
74 "type": "admit",
75 "rejected": "false",
76 }
77 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", wantLabels, 1)
78
79 wantLabels["type"] = "validate"
80 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", wantLabels, 1)
81 }
82
83 func TestObserveWebhook(t *testing.T) {
84 defer Metrics.reset()
85 defer legacyregistry.Reset()
86 Metrics.ObserveWebhook(context.TODO(), "x", 2*time.Second, false, attr, stepAdmit, 200)
87 Metrics.ObserveWebhook(context.TODO(), "x", 2*time.Second, true, attr, stepValidate, 654)
88 wantLabelsCounterAdmit := map[string]string{
89 "name": "x",
90 "operation": string(admission.Create),
91 "type": "admit",
92 "code": "200",
93 "rejected": "false",
94 }
95 wantLabelsCounterValidate := map[string]string{
96 "name": "x",
97 "operation": string(admission.Create),
98 "type": "validate",
99 "code": "600",
100 "rejected": "true",
101 }
102 wantLabelsHistogramAdmit := map[string]string{
103 "name": "x",
104 "operation": string(admission.Create),
105 "type": "admit",
106 "rejected": "false",
107 }
108 wantLabelsHistogramValidate := map[string]string{
109 "name": "x",
110 "operation": string(admission.Create),
111 "type": "validate",
112 "rejected": "true",
113 }
114
115 expectCounterValue(t, "apiserver_admission_webhook_request_total", wantLabelsCounterAdmit, 1)
116 expectCounterValue(t, "apiserver_admission_webhook_request_total", wantLabelsCounterValidate, 1)
117
118 expectHistogramCountTotal(t, "apiserver_admission_webhook_admission_duration_seconds", wantLabelsHistogramAdmit, 1)
119 expectHistogramCountTotal(t, "apiserver_admission_webhook_admission_duration_seconds", wantLabelsHistogramValidate, 1)
120 }
121
122 func TestObserveWebhookRejection(t *testing.T) {
123 Metrics.reset()
124 Metrics.ObserveWebhookRejection(context.TODO(), "x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 500)
125 Metrics.ObserveWebhookRejection(context.TODO(), "x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 654)
126 Metrics.ObserveWebhookRejection(context.TODO(), "x", stepValidate, string(admission.Update), WebhookRejectionCallingWebhookError, 501)
127 Metrics.ObserveWebhookRejection(context.TODO(), "x", stepValidate, string(admission.Update), WebhookRejectionAPIServerInternalError, 0)
128 wantLabels := map[string]string{
129 "name": "x",
130 "operation": string(admission.Create),
131 "type": "admit",
132 "error_type": "no_error",
133 "rejection_code": "500",
134 }
135 wantLabels600 := map[string]string{
136 "name": "x",
137 "operation": string(admission.Create),
138 "type": "admit",
139 "error_type": "no_error",
140 "rejection_code": "600",
141 }
142 wantLabelsCallingWebhookError := map[string]string{
143 "name": "x",
144 "operation": string(admission.Update),
145 "type": "validate",
146 "error_type": "calling_webhook_error",
147 "rejection_code": "501",
148 }
149 wantLabelsAPIServerInternalError := map[string]string{
150 "name": "x",
151 "operation": string(admission.Update),
152 "type": "validate",
153 "error_type": "apiserver_internal_error",
154 "rejection_code": "0",
155 }
156 expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels, 1)
157 expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels600, 1)
158 expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabelsCallingWebhookError, 1)
159 expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabelsAPIServerInternalError, 1)
160 }
161
162 func TestObserveWebhookFailOpen(t *testing.T) {
163 defer Metrics.reset()
164 defer legacyregistry.Reset()
165 Metrics.ObserveWebhookFailOpen(context.TODO(), "x", stepAdmit)
166 Metrics.ObserveWebhookFailOpen(context.TODO(), "x", stepValidate)
167 wantLabelsCounterAdmit := map[string]string{
168 "name": "x",
169 "type": "admit",
170 }
171 wantLabelsCounterValidate := map[string]string{
172 "name": "x",
173 "type": "validate",
174 }
175 expectCounterValue(t, "apiserver_admission_webhook_fail_open_count", wantLabelsCounterAdmit, 1)
176 expectCounterValue(t, "apiserver_admission_webhook_fail_open_count", wantLabelsCounterValidate, 1)
177 }
178
179 func TestObserveMatchConditionEvalError(t *testing.T) {
180 defer Metrics.reset()
181 defer legacyregistry.Reset()
182 Metrics.ObserveMatchConditionEvalError(context.TODO(), "x", kindWebhook, stepAdmit, string(admission.Create))
183 Metrics.ObserveMatchConditionEvalError(context.TODO(), "x", kindWebhook, stepValidate, string(admission.Create))
184 Metrics.ObserveMatchConditionEvalError(context.TODO(), "x", kindPolicy, stepAdmit, string(admission.Create))
185 Metrics.ObserveMatchConditionEvalError(context.TODO(), "x", kindPolicy, stepValidate, string(admission.Create))
186 wantLabelsCounterPolicyAdmit := map[string]string{
187 "name": "x",
188 "type": "admit",
189 "kind": "policy",
190 "operation": string(admission.Create),
191 }
192 wantLabelsCounterPolicyValidate := map[string]string{
193 "name": "x",
194 "type": "validate",
195 "kind": "policy",
196 "operation": string(admission.Create),
197 }
198 wantLabelsCounterWebhookAdmit := map[string]string{
199 "name": "x",
200 "type": "admit",
201 "kind": "webhook",
202 "operation": string(admission.Create),
203 }
204 wantLabelsCounterWebhookValidate := map[string]string{
205 "name": "x",
206 "type": "validate",
207 "kind": "webhook",
208 "operation": string(admission.Create),
209 }
210 expectCounterValue(t, "apiserver_admission_match_condition_evaluation_errors_total", wantLabelsCounterPolicyAdmit, 1)
211 expectCounterValue(t, "apiserver_admission_match_condition_evaluation_errors_total", wantLabelsCounterPolicyValidate, 1)
212 expectCounterValue(t, "apiserver_admission_match_condition_evaluation_errors_total", wantLabelsCounterWebhookAdmit, 1)
213 expectCounterValue(t, "apiserver_admission_match_condition_evaluation_errors_total", wantLabelsCounterWebhookValidate, 1)
214 }
215
216 func TestObserveMatchConditionExclusion(t *testing.T) {
217 defer Metrics.reset()
218 defer legacyregistry.Reset()
219 Metrics.ObserveMatchConditionExclusion(context.TODO(), "x", kindWebhook, stepAdmit, string(admission.Create))
220 Metrics.ObserveMatchConditionExclusion(context.TODO(), "x", kindWebhook, stepValidate, string(admission.Create))
221 Metrics.ObserveMatchConditionExclusion(context.TODO(), "x", kindPolicy, stepAdmit, string(admission.Create))
222 Metrics.ObserveMatchConditionExclusion(context.TODO(), "x", kindPolicy, stepValidate, string(admission.Create))
223 wantLabelsCounterPolicyAdmit := map[string]string{
224 "name": "x",
225 "type": "admit",
226 "kind": "policy",
227 "operation": string(admission.Create),
228 }
229 wantLabelsCounterPolicyValidate := map[string]string{
230 "name": "x",
231 "type": "validate",
232 "kind": "policy",
233 "operation": string(admission.Create),
234 }
235 wantLabelsCounterWebhookAdmit := map[string]string{
236 "name": "x",
237 "type": "admit",
238 "kind": "webhook",
239 "operation": string(admission.Create),
240 }
241 wantLabelsCounterWebhookValidate := map[string]string{
242 "name": "x",
243 "type": "validate",
244 "kind": "webhook",
245 "operation": string(admission.Create),
246 }
247 metrics, _ := legacyregistry.DefaultGatherer.Gather()
248 for _, mf := range metrics {
249 t.Logf("%v", *mf.Name)
250 }
251 expectCounterValue(t, "apiserver_admission_match_condition_exclusions_total", wantLabelsCounterPolicyAdmit, 1)
252 expectCounterValue(t, "apiserver_admission_match_condition_exclusions_total", wantLabelsCounterPolicyValidate, 1)
253 expectCounterValue(t, "apiserver_admission_match_condition_exclusions_total", wantLabelsCounterWebhookAdmit, 1)
254 expectCounterValue(t, "apiserver_admission_match_condition_exclusions_total", wantLabelsCounterWebhookValidate, 1)
255 }
256
257 func TestObserveMatchConditionEvaluationTime(t *testing.T) {
258 defer Metrics.reset()
259 defer legacyregistry.Reset()
260 Metrics.ObserveMatchConditionEvaluationTime(context.TODO(), 2*time.Second, "x", kindPolicy, stepAdmit, string(admission.Create))
261 Metrics.ObserveMatchConditionEvaluationTime(context.TODO(), 2*time.Second, "x", kindPolicy, stepValidate, string(admission.Create))
262 Metrics.ObserveMatchConditionEvaluationTime(context.TODO(), 2*time.Second, "x", kindWebhook, stepAdmit, string(admission.Create))
263 Metrics.ObserveMatchConditionEvaluationTime(context.TODO(), 2*time.Second, "x", kindWebhook, stepValidate, string(admission.Create))
264 wantLabelsCounterPolicyAdmit := map[string]string{
265 "name": "x",
266 "type": "admit",
267 "kind": "policy",
268 "operation": string(admission.Create),
269 }
270 wantLabelsCounterPolicyValidate := map[string]string{
271 "name": "x",
272 "type": "validate",
273 "kind": "policy",
274 "operation": string(admission.Create),
275 }
276 wantLabelsCounterWebhookAdmit := map[string]string{
277 "name": "x",
278 "type": "admit",
279 "kind": "webhook",
280 "operation": string(admission.Create),
281 }
282 wantLabelsCounterWebhookValidate := map[string]string{
283 "name": "x",
284 "type": "validate",
285 "kind": "webhook",
286 "operation": string(admission.Create),
287 }
288 expectHistogramCountTotal(t, "apiserver_admission_match_condition_evaluation_seconds", wantLabelsCounterPolicyAdmit, 1)
289 expectHistogramCountTotal(t, "apiserver_admission_match_condition_evaluation_seconds", wantLabelsCounterPolicyValidate, 1)
290 expectHistogramCountTotal(t, "apiserver_admission_match_condition_evaluation_seconds", wantLabelsCounterWebhookAdmit, 1)
291 expectHistogramCountTotal(t, "apiserver_admission_match_condition_evaluation_seconds", wantLabelsCounterWebhookValidate, 1)
292 }
293
294 func TestWithMetrics(t *testing.T) {
295 defer Metrics.reset()
296 defer legacyregistry.Reset()
297 type Test struct {
298 name string
299 ns string
300 operation admission.Operation
301 options runtime.Object
302 handler admission.Interface
303 admit, validate bool
304 }
305 for _, test := range []Test{
306 {
307 "both-interfaces-admit-and-validate",
308 "some-ns",
309 admission.Create,
310 &metav1.CreateOptions{},
311 &mutatingAndValidatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), true, true},
312 true, true,
313 },
314 {
315 "both-interfaces-dont-admit",
316 "some-ns",
317 admission.Create,
318 &metav1.CreateOptions{},
319 &mutatingAndValidatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), false, true},
320 false, true,
321 },
322 {
323 "both-interfaces-admit-dont-validate",
324 "some-ns",
325 admission.Create,
326 &metav1.CreateOptions{},
327 &mutatingAndValidatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), true, false},
328 true, false,
329 },
330 {
331 "validate-interfaces-validate",
332 "some-ns",
333 admission.Create,
334 &metav1.CreateOptions{},
335 &validatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), true},
336 true, true,
337 },
338 {
339 "validate-interfaces-dont-validate",
340 "some-ns",
341 admission.Create,
342 &metav1.CreateOptions{},
343 &validatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), false},
344 true, false,
345 },
346 {
347 "mutating-interfaces-admit",
348 "some-ns",
349 admission.Create,
350 &metav1.CreateOptions{},
351 &mutatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), true},
352 true, true,
353 },
354 {
355 "mutating-interfaces-dont-admit",
356 "some-ns",
357 admission.Create,
358 &metav1.CreateOptions{},
359 &mutatingFakeHandler{admission.NewHandler(admission.Create, admission.Update), false},
360 false, true,
361 },
362 } {
363 Metrics.reset()
364
365 h := WithMetrics(test.handler, Metrics.ObserveAdmissionController, test.name)
366
367 // test mutation
368 err := h.(admission.MutationInterface).Admit(context.TODO(), admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{}, test.ns, "", schema.GroupVersionResource{}, "", test.operation, test.options, false, nil), nil)
369 if test.admit && err != nil {
370 t.Errorf("expected admit to succeed, but failed: %v", err)
371 continue
372 } else if !test.admit && err == nil {
373 t.Errorf("expected admit to fail, but it succeeded")
374 continue
375 }
376
377 filter := map[string]string{"type": "admit", "rejected": "false"}
378 if !test.admit {
379 filter["rejected"] = "true"
380 }
381 if _, mutating := test.handler.(admission.MutationInterface); mutating {
382 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", filter, 1)
383 } else {
384 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", filter, 0)
385 }
386
387 if err != nil {
388 // skip validation step if mutation failed
389 continue
390 }
391
392 // test validation
393 err = h.(admission.ValidationInterface).Validate(context.TODO(), admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{}, test.ns, "", schema.GroupVersionResource{}, "", test.operation, test.options, false, nil), nil)
394 if test.validate && err != nil {
395 t.Errorf("expected admit to succeed, but failed: %v", err)
396 continue
397 } else if !test.validate && err == nil {
398 t.Errorf("expected validation to fail, but it succeeded")
399 continue
400 }
401
402 filter = map[string]string{"type": "validate", "rejected": "false"}
403 if !test.validate {
404 filter["rejected"] = "true"
405 }
406 if _, validating := test.handler.(admission.ValidationInterface); validating {
407 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", filter, 1)
408 } else {
409 expectHistogramCountTotal(t, "apiserver_admission_controller_admission_duration_seconds", filter, 0)
410 }
411 }
412 }
413
414 type mutatingAndValidatingFakeHandler struct {
415 *admission.Handler
416 admit bool
417 validate bool
418 }
419
420 func (h *mutatingAndValidatingFakeHandler) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
421 if h.admit {
422 return nil
423 }
424 return fmt.Errorf("don't admit")
425 }
426
427 func (h *mutatingAndValidatingFakeHandler) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
428 if h.validate {
429 return nil
430 }
431 return fmt.Errorf("don't validate")
432 }
433
434 type validatingFakeHandler struct {
435 *admission.Handler
436 validate bool
437 }
438
439 func (h *validatingFakeHandler) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
440 if h.validate {
441 return nil
442 }
443 return fmt.Errorf("don't validate")
444 }
445
446 type mutatingFakeHandler struct {
447 *admission.Handler
448 admit bool
449 }
450
451 func (h *mutatingFakeHandler) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
452 if h.admit {
453 return nil
454 }
455 return fmt.Errorf("don't admit")
456 }