2020-08-29 01:22:22 +00:00
|
|
|
package validate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2020-10-07 18:12:31 +00:00
|
|
|
|
|
|
|
commonAnchors "github.com/kyverno/kyverno/pkg/engine/anchor/common"
|
2020-08-29 01:22:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Checks if pattern has anchors
|
|
|
|
func hasNestedAnchors(pattern interface{}) bool {
|
|
|
|
switch typed := pattern.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
if anchors := getAnchorsFromMap(typed); len(anchors) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, value := range typed {
|
|
|
|
if hasNestedAnchors(value) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case []interface{}:
|
|
|
|
for _, value := range typed {
|
|
|
|
if hasNestedAnchors(value) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSortedNestedAnchorResource - sorts anchors key
|
|
|
|
func getSortedNestedAnchorResource(resources map[string]interface{}) *list.List {
|
|
|
|
sortedResourceKeys := list.New()
|
|
|
|
for k, v := range resources {
|
2021-09-21 20:21:57 +00:00
|
|
|
if commonAnchors.IsGlobalAnchor(k) {
|
2021-09-21 14:45:09 +00:00
|
|
|
sortedResourceKeys.PushFront(k)
|
|
|
|
continue
|
|
|
|
}
|
2020-08-29 01:22:22 +00:00
|
|
|
if hasNestedAnchors(v) {
|
|
|
|
sortedResourceKeys.PushFront(k)
|
2020-12-09 06:52:37 +00:00
|
|
|
} else {
|
|
|
|
sortedResourceKeys.PushBack(k)
|
2020-08-29 01:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortedResourceKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAnchorsFromMap gets the anchor map
|
|
|
|
func getAnchorsFromMap(anchorsMap map[string]interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for key, value := range anchorsMap {
|
2021-09-21 14:45:09 +00:00
|
|
|
if commonAnchors.IsConditionAnchor(key) || commonAnchors.IsExistenceAnchor(key) || commonAnchors.IsEqualityAnchor(key) || commonAnchors.IsNegationAnchor(key) || commonAnchors.IsGlobalAnchor(key) {
|
2020-08-29 01:22:22 +00:00
|
|
|
result[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|