mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
fix: compile regex globally (#6510)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
2f64505469
commit
caf10ba531
2 changed files with 28 additions and 16 deletions
|
@ -7,28 +7,32 @@ import (
|
|||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// GetKindFromGVK - get kind and APIVersion from GVK
|
||||
func GetKindFromGVK(str string) (groupVersion string, kind string) {
|
||||
parts := strings.Split(str, "/")
|
||||
count := len(parts)
|
||||
versionRegex := regexp.MustCompile(`v\d((alpha|beta)\d)?`)
|
||||
var versionRegex = regexp.MustCompile(`^v\d((alpha|beta)\d)?|\*$`)
|
||||
|
||||
if count == 2 {
|
||||
if versionRegex.MatchString(parts[0]) || parts[0] == "*" {
|
||||
return parts[0], formatSubresource(parts[1])
|
||||
} else {
|
||||
// GetKindFromGVK - get kind and APIVersion from GVK
|
||||
func GetKindFromGVK(str string) (string, string) {
|
||||
parts := strings.Split(str, "/")
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
return "", formatSubresource(str)
|
||||
case 2:
|
||||
if parts[0] == "*" && parts[1] == "*" {
|
||||
return "", parts[0] + "/" + parts[1]
|
||||
}
|
||||
} else if count == 3 {
|
||||
if versionRegex.MatchString(parts[0]) || parts[0] == "*" {
|
||||
return parts[0], parts[1] + "/" + parts[2]
|
||||
} else {
|
||||
return parts[0] + "/" + parts[1], formatSubresource(parts[2])
|
||||
if versionRegex.MatchString(parts[0]) {
|
||||
return parts[0], formatSubresource(parts[1])
|
||||
}
|
||||
} else if count == 4 {
|
||||
return "", parts[0] + "/" + parts[1]
|
||||
case 3:
|
||||
if versionRegex.MatchString(parts[0]) {
|
||||
return parts[0], parts[1] + "/" + parts[2]
|
||||
}
|
||||
return parts[0] + "/" + parts[1], formatSubresource(parts[2])
|
||||
case 4:
|
||||
return parts[0] + "/" + parts[1], parts[2] + "/" + parts[3]
|
||||
default:
|
||||
return "", ""
|
||||
}
|
||||
return "", formatSubresource(str)
|
||||
}
|
||||
|
||||
func formatSubresource(s string) string {
|
||||
|
|
|
@ -12,6 +12,14 @@ func Test_GetKindFromGVK(t *testing.T) {
|
|||
assert.Equal(t, "", apiVersion)
|
||||
assert.Equal(t, "*", kind)
|
||||
|
||||
apiVersion, kind = GetKindFromGVK("*.*")
|
||||
assert.Equal(t, "", apiVersion)
|
||||
assert.Equal(t, "*/*", kind)
|
||||
|
||||
apiVersion, kind = GetKindFromGVK("*/*")
|
||||
assert.Equal(t, "", apiVersion)
|
||||
assert.Equal(t, "*/*", kind)
|
||||
|
||||
apiVersion, kind = GetKindFromGVK("Pod")
|
||||
assert.Equal(t, "", apiVersion)
|
||||
assert.Equal(t, "Pod", kind)
|
||||
|
|
Loading…
Add table
Reference in a new issue