1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-12 02:46:56 +00:00
kyverno/pkg/policy/utils.go

40 lines
918 B
Go
Raw Normal View History

2019-08-23 18:34:23 -07:00
package policy
import (
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func resourceMatches(match kyvernov1.ResourceDescription, res unstructured.Unstructured, isNamespacedPolicy bool) bool {
if match.Name != "" && res.GetName() != match.Name {
return false
}
if len(match.Names) > 0 && !contains(match.Names, res.GetName()) {
return false
}
if !isNamespacedPolicy && len(match.Namespaces) > 0 && !contains(match.Namespaces, res.GetNamespace()) {
return false
}
return true
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
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
}