mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #106674 from wojtek-t/fix_watch_metrics
Fix logging resource-scoped watch requests as GET requests.
This commit is contained in:
commit
efa9f59f21
@ -562,7 +562,7 @@ func CanonicalVerb(verb string, scope string) string {
|
|||||||
// LIST and APPLY from PATCH.
|
// LIST and APPLY from PATCH.
|
||||||
func CleanVerb(verb string, request *http.Request) string {
|
func CleanVerb(verb string, request *http.Request) string {
|
||||||
reportedVerb := verb
|
reportedVerb := verb
|
||||||
if verb == "LIST" && checkIfWatch(request) {
|
if suggestedVerb := getVerbIfWatch(request); suggestedVerb == "WATCH" {
|
||||||
reportedVerb = "WATCH"
|
reportedVerb = "WATCH"
|
||||||
}
|
}
|
||||||
// normalize the legacy WATCHLIST to WATCH to ensure users aren't surprised by metrics
|
// normalize the legacy WATCHLIST to WATCH to ensure users aren't surprised by metrics
|
||||||
@ -593,25 +593,17 @@ func cleanVerb(verb, suggestedVerb string, request *http.Request) string {
|
|||||||
return OtherRequestMethod
|
return OtherRequestMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
//getVerbIfWatch additionally ensures that GET or List would be transformed to WATCH
|
// getVerbIfWatch additionally ensures that GET or List would be transformed to WATCH
|
||||||
func getVerbIfWatch(req *http.Request) string {
|
func getVerbIfWatch(req *http.Request) string {
|
||||||
if strings.ToUpper(req.Method) == "GET" || strings.ToUpper(req.Method) == "LIST" {
|
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
|
// see apimachinery/pkg/runtime/conversion.go Convert_Slice_string_To_bool
|
||||||
if values := req.URL.Query()["watch"]; len(values) > 0 {
|
if values := req.URL.Query()["watch"]; len(values) > 0 {
|
||||||
if value := strings.ToLower(values[0]); value != "0" && value != "false" {
|
if value := strings.ToLower(values[0]); value != "0" && value != "false" {
|
||||||
return true
|
return "WATCH"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanDryRun(u *url.URL) string {
|
func cleanDryRun(u *url.URL) string {
|
||||||
|
@ -49,6 +49,7 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
desc: "LIST should be transformed to WATCH if we have the right query param on the request",
|
desc: "LIST should be transformed to WATCH if we have the right query param on the request",
|
||||||
initialVerb: "LIST",
|
initialVerb: "LIST",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Method: "GET",
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
RawQuery: "watch=true",
|
RawQuery: "watch=true",
|
||||||
},
|
},
|
||||||
@ -59,6 +60,7 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
desc: "LIST isn't transformed to WATCH if we have query params that do not include watch",
|
desc: "LIST isn't transformed to WATCH if we have query params that do not include watch",
|
||||||
initialVerb: "LIST",
|
initialVerb: "LIST",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Method: "GET",
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
RawQuery: "blah=asdf&something=else",
|
RawQuery: "blah=asdf&something=else",
|
||||||
},
|
},
|
||||||
@ -66,20 +68,25 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
expectedVerb: "LIST",
|
expectedVerb: "LIST",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "GET isn't be transformed to WATCH if we have the right query param on the request",
|
// The above may seem counter-intuitive, but it actually is needed for cases like
|
||||||
|
// watching a single item, e.g.:
|
||||||
|
// /api/v1/namespaces/foo/pods/bar?fieldSelector=metadata.name=baz&watch=true
|
||||||
|
desc: "GET is transformed to WATCH if we have the right query param on the request",
|
||||||
initialVerb: "GET",
|
initialVerb: "GET",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Method: "GET",
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
RawQuery: "watch=true",
|
RawQuery: "watch=true",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedVerb: "GET",
|
expectedVerb: "WATCH",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "LIST is transformed to WATCH for the old pattern watch",
|
desc: "LIST is transformed to WATCH for the old pattern watch",
|
||||||
initialVerb: "LIST",
|
initialVerb: "LIST",
|
||||||
suggestedVerb: "WATCH",
|
suggestedVerb: "WATCH",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Method: "GET",
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
RawQuery: "/api/v1/watch/pods",
|
RawQuery: "/api/v1/watch/pods",
|
||||||
},
|
},
|
||||||
@ -91,6 +98,7 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
initialVerb: "LIST",
|
initialVerb: "LIST",
|
||||||
suggestedVerb: "WATCHLIST",
|
suggestedVerb: "WATCHLIST",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Method: "GET",
|
||||||
URL: &url.URL{
|
URL: &url.URL{
|
||||||
RawQuery: "/api/v1/watch/pods",
|
RawQuery: "/api/v1/watch/pods",
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user