github.com/argoproj/argo-cd/v3@v3.2.1/util/metrics/kubectl/util.go (about) 1 package kubectl 2 3 import ( 4 "net/url" 5 "regexp" 6 ) 7 8 // The functions here are adapted from: https://github.com/argoproj/pkg/blob/f5a0a066030558f089fa645dc6546ddc5917bad5/kubeclientmetrics/metric.go 9 10 const findPathRegex = `/v\d\w*?(/[a-zA-Z0-9-]*)(/[a-zA-Z0-9-]*)?(/[a-zA-Z0-9-]*)?(/[a-zA-Z0-9-]*)?` 11 12 var processPath = regexp.MustCompile(findPathRegex) 13 14 // discernGetRequest uses a path from a request to determine if the request is a GET, LIST, or WATCH. 15 // The function tries to find an API version within the path and then calculates how many remaining 16 // segments are after the API version. A LIST/WATCH request has segments for the kind with a 17 // namespace and the specific namespace if the kind is a namespaced resource. Meanwhile a GET 18 // request has an additional segment for resource name. As a result, a LIST/WATCH has an odd number 19 // of segments while a GET request has an even number of segments. Watch is determined if the query 20 // parameter watch=true is present in the request. 21 func discernGetRequest(u url.URL) string { 22 segments := processPath.FindStringSubmatch(u.Path) 23 unusedGroup := 0 24 for _, str := range segments { 25 if str == "" { 26 unusedGroup++ 27 } 28 } 29 if unusedGroup%2 == 1 { 30 if watchQueryParamValues, ok := u.Query()["watch"]; ok { 31 if len(watchQueryParamValues) > 0 && watchQueryParamValues[0] == "true" { 32 return "Watch" 33 } 34 } 35 return "List" 36 } 37 return "Get" 38 } 39 40 func resolveK8sRequestVerb(u url.URL, method string) string { 41 if method == "POST" { 42 return "Create" 43 } 44 if method == "DELETE" { 45 return "Delete" 46 } 47 if method == "PATCH" { 48 return "Patch" 49 } 50 if method == "PUT" { 51 return "Update" 52 } 53 if method == "GET" { 54 return discernGetRequest(u) 55 } 56 return "Unknown" 57 }