From 646053d73dbda9cae5df5fd105a720661ff051dc Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:24:13 +0800 Subject: [PATCH] Add api-version to apiserver tracing --- .../pkg/endpoints/handlers/helpers.go | 18 ++++++++++++++++++ .../pkg/endpoints/handlers/helpers_test.go | 12 ++++++++++++ .../pkg/endpoints/handlers/trace_util.go | 1 + 3 files changed, 31 insertions(+) diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go index 4bc9967726d..465d62c2c2c 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -95,6 +95,24 @@ func (lazy *lazyAPIGroup) String() string { return "unknown" } +// lazyAPIVersion implements String() string and it will +// lazily get Group from request info. +type lazyAPIVersion struct { + req *http.Request +} + +func (lazy *lazyAPIVersion) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.APIVersion + } + } + + return "unknown" +} + // lazyAuditID implements Stringer interface to lazily retrieve // the audit ID associated with the request. type lazyAuditID struct { diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go index 1a7ef4c490b..9fb24aaa807 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go @@ -97,6 +97,18 @@ func TestLazyApiGroup(t *testing.T) { assert.Equal(t, "apps", fmt.Sprintf("%v", scopeWithReq)) } +func TestLazyApiVersion(t *testing.T) { + assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIVersion{})) + + scopeWithEmptyReq := &lazyAPIVersion{&http.Request{}} + assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) + + req := &http.Request{} + ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIVersion: "v1"}) + scopeWithReq := &lazyAPIVersion{req: req.WithContext(ctx)} + assert.Equal(t, "v1", fmt.Sprintf("%v", scopeWithReq)) +} + func TestLazyResource(t *testing.T) { assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{})) diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go index 437c577c4e0..2fddf7f4101 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go @@ -28,6 +28,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("audit-id", &lazyAuditID{req: req}), attribute.Stringer("client", &lazyClientIP{req: req}), attribute.Stringer("api-group", &lazyAPIGroup{req: req}), + attribute.Stringer("api-version", &lazyAPIVersion{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}),