diff --git a/pkg/controllers/report/utils/scanner.go b/pkg/controllers/report/utils/scanner.go index 0ca9c45a5c..fd143dd8a3 100644 --- a/pkg/controllers/report/utils/scanner.go +++ b/pkg/controllers/report/utils/scanner.go @@ -47,17 +47,18 @@ func (s *scanner) ScanResource(resource unstructured.Unstructured, nsLabels map[ func (s *scanner) scan(resource unstructured.Unstructured, nsLabels map[string]string, policy kyvernov1.PolicyInterface) (*response.EngineResponse, error) { ctx := context.NewContext() - err := ctx.AddResource(resource.Object) - if err != nil { + if err := ctx.AddResource(resource.Object); err != nil { return nil, err } - err = ctx.AddNamespace(resource.GetNamespace()) - if err != nil { + if err := ctx.AddNamespace(resource.GetNamespace()); err != nil { return nil, err } if err := ctx.AddImageInfos(&resource); err != nil { return nil, err } + if err := ctx.AddOperation("CREATE"); err != nil { + return nil, err + } policyCtx := &engine.PolicyContext{ Policy: policy, NewResource: resource, diff --git a/pkg/engine/context/context.go b/pkg/engine/context/context.go index 2291dc369b..90d2bac72f 100644 --- a/pkg/engine/context/context.go +++ b/pkg/engine/context/context.go @@ -52,6 +52,9 @@ type Interface interface { // AddTargetResource merges resource json under target AddTargetResource(data map[string]interface{}) error + // AddOperation merges operation under request.operation + AddOperation(data string) error + // AddUserInfo merges userInfo json under kyverno.userInfo AddUserInfo(userInfo kyvernov1beta1.RequestInfo) error @@ -173,6 +176,11 @@ func (ctx *context) AddTargetResource(data map[string]interface{}) error { return addToContext(ctx, data, "target") } +// AddOperation data at path: request.operation +func (ctx *context) AddOperation(data string) error { + return addToContext(ctx, data, "request", "operation") +} + // AddUserInfo adds userInfo at path request.userInfo func (ctx *context) AddUserInfo(userRequestInfo kyvernov1beta1.RequestInfo) error { return addToContext(ctx, userRequestInfo, "request") diff --git a/pkg/policy/apply.go b/pkg/policy/apply.go index a11793b7bd..bc16c80439 100644 --- a/pkg/policy/apply.go +++ b/pkg/policy/apply.go @@ -52,6 +52,10 @@ func applyPolicy(policy kyvernov1.PolicyInterface, resource unstructured.Unstruc logger.Error(err, "unable to add image info to variables context") } + if err := ctx.AddOperation("CREATE"); err != nil { + logger.Error(err, "unable to set operation in context") + } + engineResponseMutation, err = mutation(policy, resource, logger, ctx, namespaceLabels) if err != nil { logger.Error(err, "failed to process mutation rule")