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
Prateek Pandey 8b6d3d1f6a
feat: trigger generate on existing matched resource (#3819)
* feat: trigger generate on existing matched resource

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* refactor the triggers and fix review comments

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* add trigger for other matching kinds

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* implement match exclude using dynamic client

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* refactor generate trigger

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* increase sleep timeout

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* optimize unstructured list

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* fix review comments

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

* log refactor and clean debug comments

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>
2022-05-09 07:13:11 +00:00

69 lines
1.5 KiB
Go

package policy
import (
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func isRunningPod(obj unstructured.Unstructured) bool {
objMap := obj.UnstructuredContent()
phase, ok, err := unstructured.NestedString(objMap, "status", "phase")
if !ok || err != nil {
return false
}
return phase == "Running"
}
// check if all slice elements are same
func isMatchResourcesAllValid(rule kyverno.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 kyverno.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
}