1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/policy/utils.go
shuting d3db3bc342
refactor: generate reconciliation on policy updates (#7531)
* generate rule type validation

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* generate rule type validation

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* add the kuttl test

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* linter fixes

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* rever validation checks

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* refactor

Signed-off-by: ShutingZhao <shuting@nirmata.com>

---------

Signed-off-by: ShutingZhao <shuting@nirmata.com>
2023-06-14 13:52:27 +00:00

70 lines
1.5 KiB
Go

package policy
import (
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// check if all slice elements are same
func isMatchResourcesAllValid(rule kyvernov1.Rule) bool {
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
}
func fetchUniqueKinds(rule kyvernov1.Rule) []string {
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
}
func castPolicy(p interface{}) kyvernov1.PolicyInterface {
var policy kyvernov1.PolicyInterface
switch obj := p.(type) {
case *kyvernov1.ClusterPolicy:
policy = obj
case *kyvernov1.Policy:
policy = obj
}
return policy
}