Skip to content

Commit

Permalink
feat: add expression method to get value (#1841)
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 committed Aug 10, 2024
1 parent 84260de commit 4131f0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package v1alpha1

import (
"context"
"fmt"
"regexp"

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/expressions"
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -62,6 +65,10 @@ type Expectation struct {
// Expression defines an expression to be used in string fields.
type Expression string

func (e Expression) Value(ctx context.Context, bindings binding.Bindings) (string, error) {
return expressions.String(ctx, string(e), bindings)
}

// Format determines the output format (json or yaml).
// +kubebuilder:validation:Pattern=`^(?:json|yaml|\(.+\))$`
type Format string
Expand Down
5 changes: 2 additions & 3 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
opscript "github.com/kyverno/chainsaw/pkg/engine/operations/script"
opsleep "github.com/kyverno/chainsaw/pkg/engine/operations/sleep"
opupdate "github.com/kyverno/chainsaw/pkg/engine/operations/update"
"github.com/kyverno/chainsaw/pkg/expressions"
"github.com/kyverno/chainsaw/pkg/loaders/resource"
"github.com/kyverno/chainsaw/pkg/report"
"github.com/kyverno/chainsaw/pkg/runner/failer"
Expand Down Expand Up @@ -1087,7 +1086,7 @@ func (p *stepProcessor) fileRefOrCheck(ctx context.Context, ref v1alpha1.ActionC
}
}
if ref.File != "" {
ref, err := expressions.String(ctx, string(ref.File), bindings)
ref, err := ref.File.Value(ctx, bindings)
if err != nil {
return nil, err
}
Expand All @@ -1106,7 +1105,7 @@ func (p *stepProcessor) fileRefOrResource(ctx context.Context, ref v1alpha1.Acti
return []unstructured.Unstructured{*ref.Resource}, nil
}
if ref.File != "" {
ref, err := expressions.String(ctx, string(ref.File), bindings)
ref, err := ref.File.Value(ctx, bindings)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4131f0c

Please sign in to comment.