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..2c2d3e4824b 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,96 @@ 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" +} + +// 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" +} + +// 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" +} + +// 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" +} + +// 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 cdde785ea36..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 @@ -85,6 +85,66 @@ 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 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 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 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 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 7d273d62248..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 @@ -27,6 +27,11 @@ 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.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}),