Skip to content

Commit

Permalink
fix: add options support in context binding (#484)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Sep 18, 2024
1 parent fdd3587 commit 5e1c8d5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
17 changes: 0 additions & 17 deletions pkg/binding/binding.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/engine/assert/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

func NewContextBinding(path *field.Path, bindings binding.Bindings, value any, entry any) binding.Binding {
func NewContextBinding(path *field.Path, bindings binding.Bindings, value any, entry any, opts ...template.Option) binding.Binding {
return template.NewLazyBinding(
func() (any, error) {
expression := parseExpression(context.TODO(), entry)
Expand All @@ -19,7 +19,7 @@ func NewContextBinding(path *field.Path, bindings binding.Bindings, value any, e
if expression.binding != "" {
return nil, field.Invalid(path.Child("variable"), entry, "binding is not supported in context")
}
projected, err := template.Execute(context.Background(), expression.statement, value, bindings)
projected, err := template.Execute(context.TODO(), expression.statement, value, bindings, opts...)
if err != nil {
return nil, field.InternalError(path.Child("variable"), err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/json-engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

jpbinding "github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/binding"
"github.com/kyverno/kyverno-json/pkg/engine"
"github.com/kyverno/kyverno-json/pkg/engine/assert"
"github.com/kyverno/kyverno-json/pkg/engine/builder"
"github.com/kyverno/kyverno-json/pkg/engine/template"
"github.com/kyverno/kyverno-json/pkg/matching"
"k8s.io/apimachinery/pkg/util/validation/field"
)

type Request struct {
Expand Down Expand Up @@ -69,7 +70,12 @@ func New() engine.Engine[Request, Response] {
ruleEngine := builder.
Function(func(ctx context.Context, r ruleRequest) []RuleResponse {
bindings := r.bindings.Register("$rule", jpbinding.NewBinding(r.rule))
bindings = binding.NewContextBindings(bindings, r.resource, r.rule.Context...)
// TODO: this doesn't seem to be the right path
var path *field.Path
path = path.Child("context")
for i, entry := range r.rule.Context {
bindings = bindings.Register("$"+entry.Name, assert.NewContextBinding(path.Index(i), bindings, r.resource, entry.Variable.Value()))
}
identifier := ""
if r.rule.Identifier != "" {
result, err := template.Execute(context.Background(), r.rule.Identifier, r.resource, bindings)
Expand Down

0 comments on commit 5e1c8d5

Please sign in to comment.