Merge pull request #105648 from kkkkun/kkkkun/fix-metric
GET should be transformed to watch in kube-Apiserver Kubernetes-commit: 4c659c5342797c9a1f2859f42b2077859c4ba621
This commit is contained in:
commit
17346dba5e
4
go.mod
4
go.mod
|
|
@ -47,7 +47,7 @@ require (
|
|||
k8s.io/api v0.0.0-20211104003544-e423bf5bf942
|
||||
k8s.io/apimachinery v0.0.0-20211104003341-94020522c95c
|
||||
k8s.io/client-go v0.0.0-20211104003836-6d49694c4453
|
||||
k8s.io/component-base v0.0.0-20211104004413-91797e1d1ca8
|
||||
k8s.io/component-base v0.0.0-20211104125346-7402a6acafc9
|
||||
k8s.io/klog/v2 v2.30.0
|
||||
k8s.io/kube-openapi v0.0.0-20210817084001-7fbd8d59e5b8
|
||||
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b
|
||||
|
|
@ -60,5 +60,5 @@ replace (
|
|||
k8s.io/api => k8s.io/api v0.0.0-20211104003544-e423bf5bf942
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20211104003341-94020522c95c
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20211104003836-6d49694c4453
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20211104004413-91797e1d1ca8
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20211104125346-7402a6acafc9
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -950,8 +950,8 @@ k8s.io/apimachinery v0.0.0-20211104003341-94020522c95c h1:18i+Svc8AQt2tsHgUJjMgi
|
|||
k8s.io/apimachinery v0.0.0-20211104003341-94020522c95c/go.mod h1:NdmIf2dMPBkkSfPmCDRbThro3RAWWypv6x+CttBbMto=
|
||||
k8s.io/client-go v0.0.0-20211104003836-6d49694c4453 h1:5555+BsEXvnsDKnNtMm4pL5Jf9x4HBN96ZZ9cQ5Wt9c=
|
||||
k8s.io/client-go v0.0.0-20211104003836-6d49694c4453/go.mod h1:XlENrFN0KamgreLfijzwed9BylToe+dU63vC+0pl4/M=
|
||||
k8s.io/component-base v0.0.0-20211104004413-91797e1d1ca8 h1:L6x6ib3sOlNdnNBMWOfb+5sIExv6Fxo2gn3NIhoTePE=
|
||||
k8s.io/component-base v0.0.0-20211104004413-91797e1d1ca8/go.mod h1:i3cLNE42Rergf0WtAjvBXeMFMCiM70q8HHLQQjzt13Q=
|
||||
k8s.io/component-base v0.0.0-20211104125346-7402a6acafc9 h1:lXlkeLjLQk6A8ojjW5EITpIBOedNgi47pcmljE7nWQE=
|
||||
k8s.io/component-base v0.0.0-20211104125346-7402a6acafc9/go.mod h1:i3cLNE42Rergf0WtAjvBXeMFMCiM70q8HHLQQjzt13Q=
|
||||
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
|
||||
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
|
||||
k8s.io/klog/v2 v2.30.0 h1:bUO6drIvCIsvZ/XFgfxoGFQU/a4Qkh0iAlvUR7vlHJw=
|
||||
|
|
|
|||
|
|
@ -380,7 +380,7 @@ func RecordRequestAbort(req *http.Request, requestInfo *request.RequestInfo) {
|
|||
}
|
||||
|
||||
scope := CleanScope(requestInfo)
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), "", req)
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), getVerbIfWatch(req), req)
|
||||
resource := requestInfo.Resource
|
||||
subresource := requestInfo.Subresource
|
||||
group := requestInfo.APIGroup
|
||||
|
|
@ -403,7 +403,7 @@ func RecordRequestTermination(req *http.Request, requestInfo *request.RequestInf
|
|||
// InstrumentRouteFunc which is registered in installer.go with predefined
|
||||
// list of verbs (different than those translated to RequestInfo).
|
||||
// However, we need to tweak it e.g. to differentiate GET from LIST.
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), "", req)
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), getVerbIfWatch(req), req)
|
||||
|
||||
if requestInfo.IsResourceRequest {
|
||||
requestTerminationsTotal.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
|
||||
|
|
@ -425,7 +425,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
|
|||
// InstrumentRouteFunc which is registered in installer.go with predefined
|
||||
// list of verbs (different than those translated to RequestInfo).
|
||||
// However, we need to tweak it e.g. to differentiate GET from LIST.
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), "", req)
|
||||
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), getVerbIfWatch(req), req)
|
||||
|
||||
if requestInfo.IsResourceRequest {
|
||||
e = longRunningRequestsGauge.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component)
|
||||
|
|
@ -544,13 +544,8 @@ func CanonicalVerb(verb string, scope string) string {
|
|||
// LIST and APPLY from PATCH.
|
||||
func CleanVerb(verb string, request *http.Request) string {
|
||||
reportedVerb := verb
|
||||
if verb == "LIST" {
|
||||
// see apimachinery/pkg/runtime/conversion.go Convert_Slice_string_To_bool
|
||||
if values := request.URL.Query()["watch"]; len(values) > 0 {
|
||||
if value := strings.ToLower(values[0]); value != "0" && value != "false" {
|
||||
reportedVerb = "WATCH"
|
||||
}
|
||||
}
|
||||
if verb == "LIST" && checkIfWatch(request) {
|
||||
reportedVerb = "WATCH"
|
||||
}
|
||||
// normalize the legacy WATCHLIST to WATCH to ensure users aren't surprised by metrics
|
||||
if verb == "WATCHLIST" {
|
||||
|
|
@ -564,13 +559,15 @@ func CleanVerb(verb string, request *http.Request) string {
|
|||
|
||||
// cleanVerb additionally ensures that unknown verbs don't clog up the metrics.
|
||||
func cleanVerb(verb, suggestedVerb string, request *http.Request) string {
|
||||
reportedVerb := CleanVerb(verb, request)
|
||||
// CanonicalVerb (being an input for this function) doesn't handle correctly the
|
||||
// deprecated path pattern for watch of:
|
||||
// GET /api/{version}/watch/{resource}
|
||||
// We correct it manually based on the pass verb from the installer.
|
||||
var reportedVerb string
|
||||
if suggestedVerb == "WATCH" || suggestedVerb == "WATCHLIST" {
|
||||
reportedVerb = "WATCH"
|
||||
} else {
|
||||
reportedVerb = CleanVerb(verb, request)
|
||||
}
|
||||
if validRequestMethods.Has(reportedVerb) {
|
||||
return reportedVerb
|
||||
|
|
@ -578,6 +575,27 @@ func cleanVerb(verb, suggestedVerb string, request *http.Request) string {
|
|||
return OtherRequestMethod
|
||||
}
|
||||
|
||||
//getVerbIfWatch additionally ensures that GET or List would be transformed to WATCH
|
||||
func getVerbIfWatch(req *http.Request) string {
|
||||
if strings.ToUpper(req.Method) == "GET" || strings.ToUpper(req.Method) == "LIST" {
|
||||
if checkIfWatch(req) {
|
||||
return "WATCH"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
//checkIfWatch check request is watch
|
||||
func checkIfWatch(req *http.Request) bool {
|
||||
// see apimachinery/pkg/runtime/conversion.go Convert_Slice_string_To_bool
|
||||
if values := req.URL.Query()["watch"]; len(values) > 0 {
|
||||
if value := strings.ToLower(values[0]); value != "0" && value != "false" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cleanDryRun(u *url.URL) string {
|
||||
// avoid allocating when we don't see dryRun in the query
|
||||
if !strings.Contains(u.RawQuery, "dryRun") {
|
||||
|
|
|
|||
|
|
@ -65,6 +65,16 @@ func TestCleanVerb(t *testing.T) {
|
|||
},
|
||||
expectedVerb: "LIST",
|
||||
},
|
||||
{
|
||||
desc: "GET isn't be transformed to WATCH if we have the right query param on the request",
|
||||
initialVerb: "GET",
|
||||
request: &http.Request{
|
||||
URL: &url.URL{
|
||||
RawQuery: "watch=true",
|
||||
},
|
||||
},
|
||||
expectedVerb: "GET",
|
||||
},
|
||||
{
|
||||
desc: "LIST is transformed to WATCH for the old pattern watch",
|
||||
initialVerb: "LIST",
|
||||
|
|
|
|||
Loading…
Reference in New Issue