mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 15:37:19 +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>
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package context
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
jmespath "github.com/kyverno/kyverno/pkg/engine/jmespath"
|
|
)
|
|
|
|
//Query the JSON context with JMESPATH search path
|
|
func (ctx *Context) Query(query string) (interface{}, error) {
|
|
query = strings.TrimSpace(query)
|
|
if query == "" {
|
|
return nil, fmt.Errorf("invalid query (nil)")
|
|
}
|
|
|
|
var emptyResult interface{}
|
|
|
|
// compile the query
|
|
queryPath, err := jmespath.New(query)
|
|
if err != nil {
|
|
ctx.log.Error(err, "incorrect query", "query", query)
|
|
return emptyResult, fmt.Errorf("incorrect query %s: %v", query, err)
|
|
}
|
|
|
|
// search
|
|
ctx.mutex.RLock()
|
|
defer ctx.mutex.RUnlock()
|
|
|
|
var data interface{}
|
|
if err := json.Unmarshal(ctx.jsonRaw, &data); err != nil {
|
|
return emptyResult, errors.Wrap(err, "failed to unmarshal context")
|
|
}
|
|
|
|
result, err := queryPath.Search(data)
|
|
if err != nil {
|
|
return emptyResult, errors.Wrap(err, "JMESPath query failed")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (ctx *Context) HasChanged(jmespath string) (bool, error) {
|
|
objData, err := ctx.Query("request.object." + jmespath)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "failed to query request.object")
|
|
}
|
|
|
|
if objData == nil {
|
|
return false, fmt.Errorf("request.object.%s not found", jmespath)
|
|
}
|
|
|
|
oldObjData, err := ctx.Query("request.oldObject." + jmespath)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "failed to query request.object")
|
|
}
|
|
|
|
if oldObjData == nil {
|
|
return false, fmt.Errorf("request.oldObject.%s not found", jmespath)
|
|
}
|
|
|
|
if reflect.DeepEqual(objData, oldObjData) {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|