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}),