2019-08-23 18:34:23 -07:00
|
|
|
package policy
|
|
|
|
|
2022-05-09 12:43:11 +05:30
|
|
|
import (
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-05-09 12:43:11 +05:30
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
2020-06-25 09:52:27 -07:00
|
|
|
|
2022-05-09 12:43:11 +05:30
|
|
|
// check if all slice elements are same
|
2022-05-17 13:12:43 +02:00
|
|
|
func isMatchResourcesAllValid(rule kyvernov1.Rule) bool {
|
2022-05-09 12:43:11 +05:30
|
|
|
var kindlist []string
|
|
|
|
for _, all := range rule.MatchResources.All {
|
|
|
|
kindlist = append(kindlist, all.Kinds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(kindlist) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(kindlist); i++ {
|
|
|
|
if kindlist[i] != kindlist[0] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func fetchUniqueKinds(rule kyvernov1.Rule) []string {
|
2022-05-09 12:43:11 +05:30
|
|
|
var kindlist []string
|
|
|
|
|
|
|
|
kindlist = append(kindlist, rule.MatchResources.Kinds...)
|
|
|
|
|
|
|
|
for _, all := range rule.MatchResources.Any {
|
|
|
|
kindlist = append(kindlist, all.Kinds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMatchResourcesAllValid(rule) {
|
|
|
|
for _, all := range rule.MatchResources.All {
|
|
|
|
kindlist = append(kindlist, all.Kinds...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inResult := make(map[string]bool)
|
|
|
|
var result []string
|
|
|
|
for _, kind := range kindlist {
|
|
|
|
if _, ok := inResult[kind]; !ok {
|
|
|
|
inResult[kind] = true
|
|
|
|
result = append(result, kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertlist(ulists []unstructured.Unstructured) []*unstructured.Unstructured {
|
|
|
|
var result []*unstructured.Unstructured
|
|
|
|
for _, list := range ulists {
|
|
|
|
result = append(result, list.DeepCopy())
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|