1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 00:17:13 +00:00
kyverno/pkg/utils/kube/kind.go
Vyankatesh Kudtarkar 23269b7f5c
Fix issue for wildcard versions (#4670)
* Fix wildcard issue

Co-Authored-By: vyankd <51167361+vyankd@users.noreply.github.com>

* Delete res.yaml

Co-Authored-By: vyankd <51167361+vyankd@users.noreply.github.com>

Co-authored-by: vyankd <51167361+vyankd@users.noreply.github.com>
2022-09-23 22:04:39 +08:00

67 lines
1.5 KiB
Go

package kube
import "strings"
// GetKindFromGVK - get kind and APIVersion from GVK
func GetKindFromGVK(str string) (apiVersion string, kind string) {
parts := strings.Split(str, "/")
count := len(parts)
if count == 2 {
return parts[0], formatSubresource(parts[1])
}
if count == 3 {
if parts[1] == "*" {
return "", formatSubresource(parts[2])
}
return parts[0] + "/" + parts[1], formatSubresource(parts[2])
}
if count == 4 {
return parts[0] + "/" + parts[1], parts[2] + "/" + parts[3]
}
return "", formatSubresource(str)
}
func formatSubresource(s string) string {
return strings.Replace(s, ".", "/", 1)
}
// GetGroupFromGVK - get group GVK
func GetGroupFromGVK(str string) (group string) {
parts := strings.Split(str, "/")
count := len(parts)
if count == 3 {
if parts[1] == "*" {
return parts[0]
}
}
return ""
}
func SplitSubresource(s string) (kind string, subresource string) {
normalized := strings.Replace(s, ".", "/", 1)
parts := strings.Split(normalized, "/")
if len(parts) == 2 {
return parts[0], parts[1]
}
return s, ""
}
func ContainsKind(list []string, kind string) bool {
for _, e := range list {
if _, k := GetKindFromGVK(e); k == kind {
return true
}
}
return false
}
// SkipSubResources skip list of resources which don't have an API group.
func SkipSubResources(kind string) bool {
s := []string{"PodExecOptions", "PodAttachOptions", "PodProxyOptions", "ServiceProxyOptions", "NodeProxyOptions"}
return ContainsKind(s, kind)
}