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
Jim Bugwadia bc07943c81
handle subresources (#3841)
* handle subresources

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* make fmt

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix logger name

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix webhook and logs

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* make fmt

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
2022-05-09 18:50:50 -07:00

55 lines
1.3 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)
}
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)
}