Skip to content

Commit

Permalink
refactor: policy compilation
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 committed Sep 24, 2024
1 parent 9d7931b commit c51d502
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 213 deletions.
10 changes: 3 additions & 7 deletions pkg/apis/policy/v1alpha1/any.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"github.com/kyverno/kyverno-json/pkg/core/projection"
hashutils "github.com/kyverno/kyverno-json/pkg/utils/hash"
"k8s.io/apimachinery/pkg/util/json"
)

Expand All @@ -12,18 +12,16 @@ import (
// +kubebuilder:validation:Type:=""
type Any struct {
_value any
_hash string
}

func NewAny(value any) Any {
return Any{
_value: value,
_hash: hashutils.Hash(value),
}
}

func (t *Any) Compile(compiler func(string, any, string) (projection.ScalarHandler, error), defaultCompiler string) (projection.ScalarHandler, error) {
return compiler(t._hash, t._value, defaultCompiler)
func (t *Any) Compile(compilers compilers.Compilers) (projection.ScalarHandler, error) {
return projection.ParseScalar(t._value, compilers)

Check warning on line 24 in pkg/apis/policy/v1alpha1/any.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/policy/v1alpha1/any.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}

func (a *Any) MarshalJSON() ([]byte, error) {
Expand All @@ -37,13 +35,11 @@ func (a *Any) UnmarshalJSON(data []byte) error {
return err
}
a._value = v
a._hash = hashutils.Hash(a._value)
return nil
}

func (in *Any) DeepCopyInto(out *Any) {
out._value = deepCopy(in._value)
out._hash = in._hash
}

func (in *Any) DeepCopy() *Any {
Expand Down
10 changes: 3 additions & 7 deletions pkg/apis/policy/v1alpha1/assertion_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/core/assertion"
hashutils "github.com/kyverno/kyverno-json/pkg/utils/hash"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/util/json"
)

Expand All @@ -12,18 +12,16 @@ import (
// AssertionTree represents an assertion tree.
type AssertionTree struct {
_tree any
_hash string
}

func NewAssertionTree(value any) AssertionTree {
return AssertionTree{
_tree: value,
_hash: hashutils.Hash(value),
}
}

func (t *AssertionTree) Compile(compiler func(string, any, string) (assertion.Assertion, error), defaultCompiler string) (assertion.Assertion, error) {
return compiler(t._hash, t._tree, defaultCompiler)
func (t *AssertionTree) Compile(compilers compilers.Compilers) (assertion.Assertion, error) {
return assertion.Parse(t._tree, compilers)

Check warning on line 24 in pkg/apis/policy/v1alpha1/assertion_tree.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/policy/v1alpha1/assertion_tree.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}

func (a *AssertionTree) MarshalJSON() ([]byte, error) {
Expand All @@ -37,11 +35,9 @@ func (a *AssertionTree) UnmarshalJSON(data []byte) error {
return err
}
a._tree = v
a._hash = hashutils.Hash(a._tree)
return nil
}

func (in *AssertionTree) DeepCopyInto(out *AssertionTree) {
out._tree = deepCopy(in._tree)
out._hash = in._hash
}
2 changes: 2 additions & 0 deletions pkg/apis/policy/v1alpha1/context_entry.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package v1alpha1

type Context []ContextEntry

// ContextEntry adds variables and data sources to a rule context.
type ContextEntry struct {
// Compiler defines the default compiler to use when evaluating expressions.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/policy/v1alpha1/validating_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ValidatingRule struct {

// Context defines variables and data sources that can be used during rule execution.
// +optional
Context []ContextEntry `json:"context,omitempty"`
Context Context `json:"context,omitempty"`

// Match defines when this policy rule should be applied.
// +optional
Expand Down
24 changes: 23 additions & 1 deletion 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.

Loading

0 comments on commit c51d502

Please sign in to comment.