Added simple tests and changed lazyVerb String method to the same as httplog uses

Kubernetes-commit: b31339231314ae8a72481cfda45539e35097450a
This commit is contained in:
Paweł Banaszewski 2022-11-07 17:13:26 +00:00 committed by Kubernetes Publisher
parent 490f0b7444
commit e21a3f224f
4 changed files with 46 additions and 21 deletions

View File

@ -21,6 +21,7 @@ import (
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apiserver/pkg/audit"
"k8s.io/apiserver/pkg/endpoints/metrics"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
)
@ -91,25 +92,20 @@ func (lazy *lazyAuditID) String() string {
}
// lazyVerb implements String() string and it will
// lazily get Verb from request info based on request context
// lazily get normalized Verb
type lazyVerb struct {
req *http.Request
}
func (lazy *lazyVerb) String() string {
if lazy.req != nil {
ctx := lazy.req.Context()
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
if ok {
return requestInfo.Verb
}
if lazy.req == nil {
return "unknown"
}
return "unknown"
return metrics.NormalizedVerb(lazy.req)
}
// lazyVerb implements String() string and it will
// lazily get Resource from request info based on request context
// lazyResource implements String() string and it will
// lazily get Resource from request info
type lazyResource struct {
req *http.Request
}

View File

@ -17,11 +17,14 @@ limitations under the License.
package handlers
import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apiserver/pkg/endpoints/request"
)
func TestLazyTruncatedUserAgent(t *testing.T) {
@ -72,3 +75,24 @@ func TestLazyAccept(t *testing.T) {
acceptWithoutReq := &lazyAccept{}
assert.Equal(t, "unknown", fmt.Sprintf("%v", acceptWithoutReq))
}
func TestLazyVerb(t *testing.T) {
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyVerb{}))
u, _ := url.Parse("?watch=true")
req := &http.Request{Method: "GET", URL: u}
verbWithReq := &lazyVerb{req: req}
assert.Equal(t, "WATCH", fmt.Sprintf("%v", verbWithReq))
}
func TestLazyResource(t *testing.T) {
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{}))
resourceWithEmptyReq := &lazyResource{&http.Request{}}
assert.Equal(t, "unknown", fmt.Sprintf("%v", resourceWithEmptyReq))
req := &http.Request{}
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Resource: "resource"})
resourceWithReq := &lazyResource{req: req.WithContext(ctx)}
assert.Equal(t, "resource", fmt.Sprintf("%v", resourceWithReq))
}

View File

@ -565,6 +565,20 @@ func InstrumentHandlerFunc(verb, group, version, resource, subresource, scope, c
}
}
// NormalizedVerb returns normalized verb
func NormalizedVerb(req *http.Request) string {
verb := req.Method
if requestInfo, ok := request.RequestInfoFrom(req.Context()); ok {
// If we can find a requestInfo, we can get a scope, and then
// we can convert GETs to LISTs when needed.
scope := CleanScope(requestInfo)
verb = CanonicalVerb(strings.ToUpper(verb), scope)
}
// mark APPLY requests and WATCH requests correctly.
return CleanVerb(verb, req)
}
// CleanScope returns the scope of the request.
func CleanScope(requestInfo *request.RequestInfo) string {
if requestInfo.Name != "" || requestInfo.Verb == "create" {

View File

@ -243,16 +243,7 @@ func SetStacktracePredicate(ctx context.Context, pred StacktracePred) {
func (rl *respLogger) Log() {
latency := time.Since(rl.startTime)
auditID := audit.GetAuditIDTruncated(rl.req.Context())
verb := rl.req.Method
if requestInfo, ok := request.RequestInfoFrom(rl.req.Context()); ok {
// If we can find a requestInfo, we can get a scope, and then
// we can convert GETs to LISTs when needed.
scope := metrics.CleanScope(requestInfo)
verb = metrics.CanonicalVerb(strings.ToUpper(verb), scope)
}
// mark APPLY requests and WATCH requests correctly.
verb = metrics.CleanVerb(verb, rl.req)
verb := metrics.NormalizedVerb(rl.req)
keysAndValues := []interface{}{
"verb", verb,