Skip to content

Commit

Permalink
refactor: feedback api (#511)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Sep 23, 2024
1 parent 30bb58b commit d75fd96
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .crds/json.kyverno.io_validatingpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ spec:
value:
description: Value is the feedback entry value (a JMESPath
expression).
type: string
x-kubernetes-preserve-unknown-fields: true
required:
- name
- value
type: object
type: array
identifier:
Expand Down
5 changes: 2 additions & 3 deletions .schemas/json/_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -17801,8 +17801,7 @@
"description": "Feedback contains a feedback entry.",
"type": "object",
"required": [
"name",
"value"
"name"
],
"properties": {
"compiler": {
Expand All @@ -17819,7 +17818,7 @@
},
"value": {
"description": "Value is the feedback entry value (a JMESPath expression).",
"type": "string"
"x-kubernetes-preserve-unknown-fields": true
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions .schemas/json/validatingpolicy-json-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,7 @@
"null"
],
"required": [
"name",
"value"
"name"
],
"properties": {
"compiler": {
Expand All @@ -534,7 +533,7 @@
},
"value": {
"description": "Value is the feedback entry value (a JMESPath expression).",
"type": "string"
"x-kubernetes-preserve-unknown-fields": true
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions .schemas/json/validatingpolicylist-json-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,7 @@
"null"
],
"required": [
"name",
"value"
"name"
],
"properties": {
"compiler": {
Expand All @@ -557,7 +556,7 @@
},
"value": {
"description": "Value is the feedback entry value (a JMESPath expression).",
"type": "string"
"x-kubernetes-preserve-unknown-fields": true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion .schemas/openapi/v2/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .schemas/openapi/v3/apis/json.kyverno.io/v1alpha1.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions charts/kyverno-json/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ spec:
value:
description: Value is the feedback entry value (a JMESPath
expression).
type: string
x-kubernetes-preserve-unknown-fields: true
required:
- name
- value
type: object
type: array
identifier:
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/policy/v1alpha1/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ type Feedback struct {
Name string `json:"name"`

// Value is the feedback entry value (a JMESPath expression).
Value string `json:"value"`
// +optional
Value *Any `json:"value,omitempty"`
}
4 changes: 4 additions & 0 deletions pkg/apis/policy/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/data/crds/json.kyverno.io_validatingpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ spec:
value:
description: Value is the feedback entry value (a JMESPath
expression).
type: string
x-kubernetes-preserve-unknown-fields: true
required:
- name
- value
type: object
type: array
identifier:
Expand Down
30 changes: 15 additions & 15 deletions pkg/json-engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ func New() engine.Engine[Request, Response] {
}
var feedback map[string]Feedback
for _, f := range r.rule.Feedback {
// TODO
// defaultCompiler := defaultCompiler
// if f.Engine != nil {
// defaultCompiler = string(*f.Engine)
// }
result, err := compilers.Execute(f.Value, r.resource, bindings, compiler.Jp)
if feedback == nil {
feedback = map[string]Feedback{}
}
if err != nil {
feedback[f.Name] = Feedback{
Error: err,
entry := Feedback{}
if f.Value != nil {
defaultCompiler := defaultCompiler
if f.Compiler != nil {
defaultCompiler = string(*f.Compiler)
}
} else {
feedback[f.Name] = Feedback{
Value: result,
if handler, err := f.Value.Compile(compiler.CompileProjection, defaultCompiler); err != nil {
entry.Error = err
} else if projected, err := handler(r.resource, bindings); err != nil {
entry.Error = err
} else {
entry.Value = projected
}
}
if feedback == nil {
feedback = map[string]Feedback{}
}
feedback[f.Name] = entry
}
violations, err := matching.Assert(nil, r.rule.Assert, r.resource, bindings, compiler, defaultCompiler)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions test/commands/scan/cel/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ kind: ValidatingPolicy
metadata:
name: test
spec:
compiler: cel
rules:
- name: foo-bar-4
context:
- name: celFoo
compiler: cel
variable: (4)
- name: jpFoo
compiler: jp
variable: ($celFoo)
- name: celFoo
compiler: cel
variable: (bindings.resolve('jpFoo'))
assert:
all:
- compiler: cel
check:
- check:
(object.foo.bar): (bindings.resolve('celFoo'))
3 changes: 2 additions & 1 deletion website/docs/apis/kyverno-json.v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ auto_generated: true
**Appears in:**

- [ContextEntry](#json-kyverno-io-v1alpha1-ContextEntry)
- [Feedback](#json-kyverno-io-v1alpha1-Feedback)

<p>Any can be any type.</p>

Expand Down Expand Up @@ -141,7 +142,7 @@ auto_generated: true
|---|---|---|---|---|
| `compiler` | [`Compiler`](#json-kyverno-io-v1alpha1-Compiler) | | | <p>Compiler defines the default compiler to use when evaluating expressions.</p> |
| `name` | `string` | :white_check_mark: | | <p>Name is the feedback entry name.</p> |
| `value` | `string` | :white_check_mark: | | <p>Value is the feedback entry value (a JMESPath expression).</p> |
| `value` | [`Any`](#json-kyverno-io-v1alpha1-Any) | | | <p>Value is the feedback entry value (a JMESPath expression).</p> |

## `Match` {#json-kyverno-io-v1alpha1-Match}

Expand Down

0 comments on commit d75fd96

Please sign in to comment.