Skip to content

Commit d3fdba4

Browse files
stttsbertinatto
authored andcommitted
UPSTREAM: <carry>: apiserver: add system_client=kube-{apiserver,cm,s} to apiserver_request_total
UPSTREAM: <carry>: apiserver: add cluster-policy-controller to system client in apiserver_request_total OpenShift-Rebase-Source: d86823d UPSTREAM: <carry>: apiserver: add system_client=kube-{apiserver,cm,s} to apiserver_request_total Fix TestOpenAPIRequestMetrics unit test.
1 parent 43e8d85 commit d3fdba4

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3636
utilsets "k8s.io/apimachinery/pkg/util/sets"
3737
"k8s.io/apiserver/pkg/audit"
38-
"k8s.io/apiserver/pkg/authentication/user"
3938
"k8s.io/apiserver/pkg/endpoints/request"
4039
"k8s.io/apiserver/pkg/endpoints/responsewriter"
4140
compbasemetrics "k8s.io/component-base/metrics"
@@ -82,7 +81,7 @@ var (
8281
Help: "Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.",
8382
StabilityLevel: compbasemetrics.STABLE,
8483
},
85-
[]string{"verb", "dry_run", "group", "version", "resource", "subresource", "scope", "component", "code"},
84+
[]string{"verb", "dry_run", "group", "version", "resource", "subresource", "scope", "component", "code", "system_client"},
8685
)
8786
longRunningRequestsGauge = compbasemetrics.NewGaugeVec(
8887
&compbasemetrics.GaugeOpts{
@@ -501,9 +500,9 @@ func RecordDroppedRequest(req *http.Request, requestInfo *request.RequestInfo, c
501500
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), "", req, requestInfo)
502501

503502
if requestInfo.IsResourceRequest {
504-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests)).Inc()
503+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests), "").Inc()
505504
} else {
506-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, "", "", "", requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests)).Inc()
505+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, "", "", "", requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests), "").Inc()
507506
}
508507
}
509508

@@ -582,12 +581,19 @@ func MonitorRequest(req *http.Request, verb, group, version, resource, subresour
582581

583582
dryRun := cleanDryRun(req.URL)
584583
elapsedSeconds := elapsed.Seconds()
585-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component, codeToString(httpCode)).Inc()
586-
// MonitorRequest happens after authentication, so we can trust the username given by the request
587-
info, ok := request.UserFrom(req.Context())
588-
if ok && info.GetName() == user.APIServerUser {
589-
apiSelfRequestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, resource, subresource).Inc()
584+
585+
systemClient := ""
586+
if uas := strings.SplitN(req.UserAgent(), "/", 2); len(uas) > 1 {
587+
switch uas[0] {
588+
case "kube-apiserver":
589+
apiSelfRequestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, resource, subresource).Inc()
590+
fallthrough
591+
case "kube-controller-manager", "kube-scheduler", "cluster-policy-controller":
592+
systemClient = uas[0]
593+
}
590594
}
595+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component, codeToString(httpCode), systemClient).Inc()
596+
591597
if deprecated {
592598
deprecatedRequestGauge.WithContext(req.Context()).WithLabelValues(group, version, resource, subresource, removedRelease).Set(1)
593599
audit.AddAuditAnnotation(req.Context(), deprecatedAnnotationKey, "true")

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func TestRecordDroppedRequests(t *testing.T) {
398398
want: `
399399
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
400400
# TYPE apiserver_request_total counter
401-
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="cluster",subresource="",verb="LIST",version="v1"} 1
401+
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="cluster",subresource="",system_client="",verb="LIST",version="v1"} 1
402402
`,
403403
},
404404
{
@@ -420,7 +420,7 @@ func TestRecordDroppedRequests(t *testing.T) {
420420
want: `
421421
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
422422
# TYPE apiserver_request_total counter
423-
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="resource",subresource="",verb="POST",version="v1"} 1
423+
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="resource",subresource="",system_client="",verb="POST",version="v1"} 1
424424
`,
425425
},
426426
{
@@ -445,7 +445,7 @@ func TestRecordDroppedRequests(t *testing.T) {
445445
want: `
446446
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
447447
# TYPE apiserver_request_total counter
448-
apiserver_request_total{code="429",component="apiserver",dry_run="All",group="batch",resource="jobs",scope="resource",subresource="status",verb="PATCH",version="v1"} 1
448+
apiserver_request_total{code="429",component="apiserver",dry_run="All",group="batch",resource="jobs",scope="resource",subresource="status",system_client="",verb="PATCH",version="v1"} 1
449449
`,
450450
},
451451
}

staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ func TestMetrics(t *testing.T) {
255255
expected := strings.NewReader(`
256256
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
257257
# TYPE apiserver_request_total counter
258-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/healthz",verb="GET",version=""} 1
259-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/livez",verb="GET",version=""} 1
260-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/readyz",verb="GET",version=""} 1
258+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/healthz",system_client="",verb="GET",version=""} 1
259+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/livez",system_client="",verb="GET",version=""} 1
260+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/readyz",system_client="",verb="GET",version=""} 1
261261
`)
262262
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, expected, "apiserver_request_total"); err != nil {
263263
t.Error(err)

staging/src/k8s.io/kube-aggregator/pkg/controllers/openapiv3/aggregator/aggregator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestOpenAPIRequestMetrics(t *testing.T) {
278278
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(`
279279
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
280280
# TYPE apiserver_request_total counter
281-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",verb="GET",version=""} 1
281+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",system_client="",verb="GET",version=""} 1
282282
`), "apiserver_request_total"); err != nil {
283283
t.Fatal(err)
284284
}
@@ -289,8 +289,8 @@ apiserver_request_total{code="200",component="",dry_run="",group="",resource="",
289289
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(`
290290
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
291291
# TYPE apiserver_request_total counter
292-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",verb="GET",version=""} 1
293-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3/",verb="GET",version=""} 1
292+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",system_client="",verb="GET",version=""} 1
293+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3/",system_client="",verb="GET",version=""} 1
294294
`), "apiserver_request_total"); err != nil {
295295
t.Fatal(err)
296296
}

test/instrumentation/testdata/stable-metrics-list.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308
- resource
309309
- scope
310310
- subresource
311+
- system_client
311312
- verb
312313
- version
313314
- name: requested_deprecated_apis

test/integration/metrics/metrics_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -408,42 +408,42 @@ func TestAPIServerMetricsPods(t *testing.T) {
408408
executor: func() {
409409
callOrDie(c.Create(context.TODO(), makePod("foo"), metav1.CreateOptions{}))
410410
},
411-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="POST", version="v1"}`,
411+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
412412
},
413413
{
414414
name: "update pod",
415415
executor: func() {
416416
callOrDie(c.Update(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
417417
},
418-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="PUT", version="v1"}`,
418+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
419419
},
420420
{
421421
name: "update pod status",
422422
executor: func() {
423423
callOrDie(c.UpdateStatus(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
424424
},
425-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", verb="PUT", version="v1"}`,
425+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
426426
},
427427
{
428428
name: "get pod",
429429
executor: func() {
430430
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
431431
},
432-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="GET", version="v1"}`,
432+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
433433
},
434434
{
435435
name: "list pod",
436436
executor: func() {
437437
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
438438
},
439-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", verb="LIST", version="v1"}`,
439+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", system_client="", verb="LIST", version="v1"}`,
440440
},
441441
{
442442
name: "delete pod",
443443
executor: func() {
444444
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
445445
},
446-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="DELETE", version="v1"}`,
446+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
447447
},
448448
} {
449449
t.Run(tc.name, func(t *testing.T) {
@@ -516,42 +516,42 @@ func TestAPIServerMetricsNamespaces(t *testing.T) {
516516
executor: func() {
517517
callOrDie(c.Create(context.TODO(), makeNamespace("foo"), metav1.CreateOptions{}))
518518
},
519-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="POST", version="v1"}`,
519+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
520520
},
521521
{
522522
name: "update namespace",
523523
executor: func() {
524524
callOrDie(c.Update(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
525525
},
526-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="PUT", version="v1"}`,
526+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
527527
},
528528
{
529529
name: "update namespace status",
530530
executor: func() {
531531
callOrDie(c.UpdateStatus(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
532532
},
533-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", verb="PUT", version="v1"}`,
533+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
534534
},
535535
{
536536
name: "get namespace",
537537
executor: func() {
538538
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
539539
},
540-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="GET", version="v1"}`,
540+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
541541
},
542542
{
543543
name: "list namespace",
544544
executor: func() {
545545
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
546546
},
547-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", verb="LIST", version="v1"}`,
547+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", system_client="", verb="LIST", version="v1"}`,
548548
},
549549
{
550550
name: "delete namespace",
551551
executor: func() {
552552
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
553553
},
554-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="DELETE", version="v1"}`,
554+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
555555
},
556556
} {
557557
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)