-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
9d7931b
commit 0494ca0
Showing
8 changed files
with
187 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package apis | ||
|
||
import ( | ||
"github.com/kyverno/kyverno-json/pkg/core/assertion" | ||
"github.com/kyverno/kyverno-json/pkg/core/projection" | ||
) | ||
|
||
type Compiler interface { | ||
CompileAssertion(any) (assertion.Assertion, error) | ||
CompileProjection(any) (projection.ScalarHandler, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core | ||
|
||
import "github.com/kyverno/kyverno-json/pkg/core/expression" | ||
|
||
const ( | ||
CompilerJP = expression.CompilerJP | ||
CompilerCEL = expression.CompilerCEL | ||
) | ||
|
||
type Engine interface{} | ||
|
||
type engine struct { | ||
defaultCompiler string | ||
} | ||
|
||
func NewDefaultEngine() engine { | ||
return engine{ | ||
defaultCompiler: CompilerJP, | ||
} | ||
} | ||
|
||
func NewEngine(defaultCompiler string) engine { | ||
return NewDefaultEngine().WithDefaultCompiler(defaultCompiler) | ||
} | ||
|
||
func (e engine) WithDefaultCompiler(defaultCompiler string) engine { | ||
e.defaultCompiler = defaultCompiler | ||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package jsonengine | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
jpbinding "github.com/jmespath-community/go-jmespath/pkg/binding" | ||
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1" | ||
"github.com/kyverno/kyverno-json/pkg/core/compilers" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
type compiler struct{} | ||
|
||
func (c *compiler) compileContextEntry( | ||
path *field.Path, | ||
compilers compilers.Compilers, | ||
entry v1alpha1.ContextEntry, | ||
) (func(any, jpbinding.Bindings) jpbinding.Bindings, error) { | ||
if entry.Compiler != nil { | ||
compilers = compilers.WithDefaultCompiler(string(*entry.Compiler)) | ||
} | ||
handler, err := entry.Variable.Compile(compilers) | ||
if err != nil { | ||
return nil, field.InternalError(path.Child("variable"), err) | ||
} | ||
return func(resource any, bindings jpbinding.Bindings) jpbinding.Bindings { | ||
return bindings.Register( | ||
"$"+entry.Name, | ||
binding.NewDelegate( | ||
sync.OnceValues( | ||
func() (any, error) { | ||
projected, err := handler(resource, bindings) | ||
if err != nil { | ||
return nil, field.InternalError(path.Child("variable"), err) | ||
} | ||
return projected, nil | ||
}, | ||
), | ||
), | ||
) | ||
}, nil | ||
} | ||
|
||
func (c *compiler) compileContext( | ||
path *field.Path, | ||
compilers compilers.Compilers, | ||
entries v1alpha1.Context, | ||
) (func(any, jpbinding.Bindings) jpbinding.Bindings, error) { | ||
var out []func(any, jpbinding.Bindings) jpbinding.Bindings | ||
for _, entry := range entries { | ||
entry, err := c.compileContextEntry(path, compilers, entry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out = append(out, entry) | ||
} | ||
return func(resource any, bindings jpbinding.Bindings) jpbinding.Bindings { | ||
for _, entry := range out { | ||
bindings = entry(resource, bindings) | ||
} | ||
return bindings | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters