From 8c34208a62f09904cd270906889d57c7a613b5d6 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:11:08 +0800 Subject: [PATCH 1/5] Add group 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 7f85563699d..4bc9967726d 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -77,6 +77,24 @@ func (lazy *lazyAccept) String() string { return "unknown" } +// lazyAPIGroup implements String() string and it will +// lazily get Group from request info. +type lazyAPIGroup struct { + req *http.Request +} + +func (lazy *lazyAPIGroup) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.APIGroup + } + } + + 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 cdde785ea36..1a7ef4c490b 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 @@ -85,6 +85,18 @@ func TestLazyVerb(t *testing.T) { assert.Equal(t, "WATCH", fmt.Sprintf("%v", verbWithReq)) } +func TestLazyApiGroup(t *testing.T) { + assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIGroup{})) + + scopeWithEmptyReq := &lazyAPIGroup{&http.Request{}} + assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) + + req := &http.Request{} + ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIGroup: "apps"}) + scopeWithReq := &lazyAPIGroup{req: req.WithContext(ctx)} + assert.Equal(t, "apps", 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 7d273d62248..437c577c4e0 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 @@ -27,6 +27,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("accept", &lazyAccept{req: req}), attribute.Stringer("audit-id", &lazyAuditID{req: req}), attribute.Stringer("client", &lazyClientIP{req: req}), + attribute.Stringer("api-group", &lazyAPIGroup{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}), 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 2/5] 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}), From bcb59a03ebb50a966cc6921dea016cc68e0bf843 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:33:48 +0800 Subject: [PATCH 3/5] Add name 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 465d62c2c2c..cf9efb64609 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -113,6 +113,24 @@ func (lazy *lazyAPIVersion) String() string { return "unknown" } +// lazyName implements String() string and it will +// lazily get Group from request info. +type lazyName struct { + req *http.Request +} + +func (lazy *lazyName) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.Name + } + } + + 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 9fb24aaa807..41cb24170a5 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 @@ -109,6 +109,18 @@ func TestLazyApiVersion(t *testing.T) { assert.Equal(t, "v1", fmt.Sprintf("%v", scopeWithReq)) } +func TestLazyName(t *testing.T) { + assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyName{})) + + scopeWithEmptyReq := &lazyName{&http.Request{}} + assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) + + req := &http.Request{} + ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Name: "jaeger-76d45d6876-vqp8t"}) + scopeWithReq := &lazyName{req: req.WithContext(ctx)} + assert.Equal(t, "jaeger-76d45d6876-vqp8t", 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 2fddf7f4101..e1b06eaf9a3 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 @@ -29,6 +29,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("client", &lazyClientIP{req: req}), attribute.Stringer("api-group", &lazyAPIGroup{req: req}), attribute.Stringer("api-version", &lazyAPIVersion{req: req}), + attribute.Stringer("name", &lazyName{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}), From c83eb6dcaaacfe8b5706f7fdc969000fbef2d119 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:37:13 +0800 Subject: [PATCH 4/5] Add subresource 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 cf9efb64609..b7c4f9f6512 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -131,6 +131,24 @@ func (lazy *lazyName) String() string { return "unknown" } +// lazySubresource implements String() string and it will +// lazily get Group from request info. +type lazySubresource struct { + req *http.Request +} + +func (lazy *lazySubresource) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.Subresource + } + } + + 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 41cb24170a5..8392f8e7745 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 @@ -121,6 +121,18 @@ func TestLazyName(t *testing.T) { assert.Equal(t, "jaeger-76d45d6876-vqp8t", fmt.Sprintf("%v", scopeWithReq)) } +func TestLazySubresource(t *testing.T) { + assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazySubresource{})) + + scopeWithEmptyReq := &lazySubresource{&http.Request{}} + assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) + + req := &http.Request{} + ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Subresource: "binding"}) + scopeWithReq := &lazySubresource{req: req.WithContext(ctx)} + assert.Equal(t, "binding", 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 e1b06eaf9a3..9fe032d812a 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 @@ -30,6 +30,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("api-group", &lazyAPIGroup{req: req}), attribute.Stringer("api-version", &lazyAPIVersion{req: req}), attribute.Stringer("name", &lazyName{req: req}), + attribute.Stringer("subresource", &lazySubresource{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}), From 45b9b0df41fb67cf2c71263b3577f3b3daff4899 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:50:47 +0800 Subject: [PATCH 5/5] Add namespace 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 b7c4f9f6512..2c2d3e4824b 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -149,6 +149,24 @@ func (lazy *lazySubresource) String() string { return "unknown" } +// lazyNamespace implements String() string and it will +// lazily get Group from request info. +type lazyNamespace struct { + req *http.Request +} + +func (lazy *lazyNamespace) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.Namespace + } + } + + 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 8392f8e7745..81faefa7a07 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 @@ -133,6 +133,18 @@ func TestLazySubresource(t *testing.T) { assert.Equal(t, "binding", fmt.Sprintf("%v", scopeWithReq)) } +func TestLazyNamespace(t *testing.T) { + assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyNamespace{})) + + scopeWithEmptyReq := &lazyNamespace{&http.Request{}} + assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq)) + + req := &http.Request{} + ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Namespace: "jaeger"}) + scopeWithReq := &lazyNamespace{req: req.WithContext(ctx)} + assert.Equal(t, "jaeger", 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 9fe032d812a..760c9bf40b8 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 @@ -31,6 +31,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("api-version", &lazyAPIVersion{req: req}), attribute.Stringer("name", &lazyName{req: req}), attribute.Stringer("subresource", &lazySubresource{req: req}), + attribute.Stringer("namespace", &lazyNamespace{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}),