mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
46f02a8ba7
* optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package context
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
datautils "github.com/kyverno/kyverno/pkg/utils/data"
|
|
)
|
|
|
|
// Query the JSON context with JMESPATH search path
|
|
func (ctx *context) Query(query string) (interface{}, error) {
|
|
if err := ctx.loadDeferred(query); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
query = strings.TrimSpace(query)
|
|
if query == "" {
|
|
return nil, fmt.Errorf("invalid query (nil)")
|
|
}
|
|
// compile the query
|
|
queryPath, err := ctx.jp.Query(query)
|
|
if err != nil {
|
|
logger.Error(err, "incorrect query", "query", query)
|
|
return nil, fmt.Errorf("incorrect query %s: %v", query, err)
|
|
}
|
|
// search
|
|
result, err := queryPath.Search(ctx.jsonRaw)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("JMESPath query failed: %w", err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (ctx *context) loadDeferred(query string) error {
|
|
level := len(ctx.jsonRawCheckpoints)
|
|
return ctx.deferred.LoadMatching(query, level)
|
|
}
|
|
|
|
func (ctx *context) HasChanged(jmespath string) (bool, error) {
|
|
objData, err := ctx.Query("request.object." + jmespath)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to query request.object: %w", err)
|
|
}
|
|
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, fmt.Errorf("failed to query request.object: %w", err)
|
|
}
|
|
if oldObjData == nil {
|
|
return false, fmt.Errorf("request.oldObject.%s not found", jmespath)
|
|
}
|
|
return !datautils.DeepEqual(objData, oldObjData), nil
|
|
}
|