Skip to content

Commit c22b351

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 6533aa8 commit c22b351

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

Lines changed: 15 additions & 9 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
- resource
322322
- scope
323323
- subresource
324+
- system_client
324325
- verb
325326
- version
326327
- name: requested_deprecated_apis

test/integration/metrics/metrics_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,42 +417,42 @@ func TestAPIServerMetricsPods(t *testing.T) {
417417
executor: func() {
418418
callOrDie(c.Create(context.TODO(), makePod("foo"), metav1.CreateOptions{}))
419419
},
420-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="POST", version="v1"}`,
420+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
421421
},
422422
{
423423
name: "update pod",
424424
executor: func() {
425425
callOrDie(c.Update(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
426426
},
427-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="PUT", version="v1"}`,
427+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
428428
},
429429
{
430430
name: "update pod status",
431431
executor: func() {
432432
callOrDie(c.UpdateStatus(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
433433
},
434-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", verb="PUT", version="v1"}`,
434+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
435435
},
436436
{
437437
name: "get pod",
438438
executor: func() {
439439
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
440440
},
441-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="GET", version="v1"}`,
441+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
442442
},
443443
{
444444
name: "list pod",
445445
executor: func() {
446446
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
447447
},
448-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", verb="LIST", version="v1"}`,
448+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", system_client="", verb="LIST", version="v1"}`,
449449
},
450450
{
451451
name: "delete pod",
452452
executor: func() {
453453
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
454454
},
455-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="DELETE", version="v1"}`,
455+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
456456
},
457457
} {
458458
t.Run(tc.name, func(t *testing.T) {
@@ -525,42 +525,42 @@ func TestAPIServerMetricsNamespaces(t *testing.T) {
525525
executor: func() {
526526
callOrDie(c.Create(context.TODO(), makeNamespace("foo"), metav1.CreateOptions{}))
527527
},
528-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="POST", version="v1"}`,
528+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
529529
},
530530
{
531531
name: "update namespace",
532532
executor: func() {
533533
callOrDie(c.Update(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
534534
},
535-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="PUT", version="v1"}`,
535+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
536536
},
537537
{
538538
name: "update namespace status",
539539
executor: func() {
540540
callOrDie(c.UpdateStatus(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
541541
},
542-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", verb="PUT", version="v1"}`,
542+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
543543
},
544544
{
545545
name: "get namespace",
546546
executor: func() {
547547
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
548548
},
549-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="GET", version="v1"}`,
549+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
550550
},
551551
{
552552
name: "list namespace",
553553
executor: func() {
554554
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
555555
},
556-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", verb="LIST", version="v1"}`,
556+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", system_client="", verb="LIST", version="v1"}`,
557557
},
558558
{
559559
name: "delete namespace",
560560
executor: func() {
561561
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
562562
},
563-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="DELETE", version="v1"}`,
563+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
564564
},
565565
} {
566566
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)