mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* updates for foreach and mutate Signed-off-by: Jim Bugwadia <jim@nirmata.com> * allow tests to pass on Windows Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter check Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add elementIndex variable Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix jsonResult usage Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add mutate validation and fix error in validate.foreach Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not skip validation for all array entries when one is skipped Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add foreach tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix fmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix format errors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * remove unused declarations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * revert namespaceWithLabelYaml Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate of element list Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update CRDs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * Update api/kyverno/v1/policy_types.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/forceMutate.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/forceMutate.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/forceMutate.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/mutation.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/mutation.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/mutation.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/validate/validate.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update pkg/engine/validate/validate.go Co-authored-by: Steven E. Harris <seh@panix.com> * Update test/cli/test/custom-functions/policy.yaml Co-authored-by: Steven E. Harris <seh@panix.com> * Update test/cli/test/foreach/policies.yaml Co-authored-by: Steven E. Harris <seh@panix.com> * accept review comments and format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add comments to strategicMergePatch buffer Signed-off-by: Jim Bugwadia <jim@nirmata.com> * load context and evaluate preconditions foreach element Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add test for foreach mutate context and precondition * precondition testcase * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Steven E. Harris <seh@panix.com> Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
109 lines
2.5 KiB
Go
109 lines
2.5 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
|
)
|
|
|
|
// CopyMap creates a full copy of the target map
|
|
func CopyMap(m map[string]interface{}) map[string]interface{} {
|
|
mapCopy := make(map[string]interface{})
|
|
for k, v := range m {
|
|
mapCopy[k] = v
|
|
}
|
|
|
|
return mapCopy
|
|
}
|
|
|
|
// CopySlice creates a full copy of the target slice
|
|
func CopySlice(s []interface{}) []interface{} {
|
|
sliceCopy := make([]interface{}, len(s))
|
|
copy(sliceCopy, s)
|
|
|
|
return sliceCopy
|
|
}
|
|
|
|
// CopySliceOfMaps creates a full copy of the target slice
|
|
func CopySliceOfMaps(s []map[string]interface{}) []interface{} {
|
|
sliceCopy := make([]interface{}, len(s))
|
|
for i, v := range s {
|
|
sliceCopy[i] = CopyMap(v)
|
|
}
|
|
|
|
return sliceCopy
|
|
}
|
|
|
|
func ToMap(data interface{}) (map[string]interface{}, error) {
|
|
if m, ok := data.(map[string]interface{}); ok {
|
|
return m, nil
|
|
}
|
|
|
|
b, err := json.Marshal(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mapData := make(map[string]interface{})
|
|
err = json.Unmarshal(b, &mapData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mapData, nil
|
|
}
|
|
|
|
func GetRawKeyIfWrappedWithAttributes(str string) string {
|
|
if len(str) < 2 {
|
|
return str
|
|
}
|
|
|
|
if str[0] == '(' && str[len(str)-1] == ')' {
|
|
return str[1 : len(str)-1]
|
|
} else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') {
|
|
return str[2 : len(str)-1]
|
|
} else {
|
|
return str
|
|
}
|
|
}
|
|
|
|
func TransformConditions(original apiextensions.JSON) (interface{}, error) {
|
|
// conditions are currently in the form of []interface{}
|
|
oldConditions, err := utils.ApiextensionsJsonToKyvernoConditions(original)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch typedValue := oldConditions.(type) {
|
|
case kyverno.AnyAllConditions:
|
|
return copyAnyAllConditions(typedValue), nil
|
|
case []kyverno.Condition: // backwards compatibility
|
|
return copyOldConditions(typedValue), nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid preconditions")
|
|
}
|
|
|
|
func copyAnyAllConditions(original kyverno.AnyAllConditions) kyverno.AnyAllConditions {
|
|
if reflect.DeepEqual(original, kyverno.AnyAllConditions{}) {
|
|
return kyverno.AnyAllConditions{}
|
|
}
|
|
return *original.DeepCopy()
|
|
}
|
|
|
|
// backwards compatibility
|
|
func copyOldConditions(original []kyverno.Condition) []kyverno.Condition {
|
|
if len(original) == 0 {
|
|
return []kyverno.Condition{}
|
|
}
|
|
|
|
var copies []kyverno.Condition
|
|
for _, condition := range original {
|
|
copies = append(copies, *condition.DeepCopy())
|
|
}
|
|
|
|
return copies
|
|
}
|