Skip to content

Commit 180f8c2

Browse files
feat: add expression method to get value (#1841) (#1842)
Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 84260de commit 180f8c2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pkg/apis/v1alpha1/types.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package v1alpha1
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
67

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

68+
func (e Expression) Value(ctx context.Context, bindings binding.Bindings) (string, error) {
69+
return expressions.String(ctx, string(e), bindings)
70+
}
71+
6572
// Format determines the output format (json or yaml).
6673
// +kubebuilder:validation:Pattern=`^(?:json|yaml|\(.+\))$`
6774
type Format string

pkg/runner/processors/step.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
opscript "github.com/kyverno/chainsaw/pkg/engine/operations/script"
2626
opsleep "github.com/kyverno/chainsaw/pkg/engine/operations/sleep"
2727
opupdate "github.com/kyverno/chainsaw/pkg/engine/operations/update"
28-
"github.com/kyverno/chainsaw/pkg/expressions"
2928
"github.com/kyverno/chainsaw/pkg/loaders/resource"
3029
"github.com/kyverno/chainsaw/pkg/report"
3130
"github.com/kyverno/chainsaw/pkg/runner/failer"
@@ -1087,7 +1086,7 @@ func (p *stepProcessor) fileRefOrCheck(ctx context.Context, ref v1alpha1.ActionC
10871086
}
10881087
}
10891088
if ref.File != "" {
1090-
ref, err := expressions.String(ctx, string(ref.File), bindings)
1089+
ref, err := ref.File.Value(ctx, bindings)
10911090
if err != nil {
10921091
return nil, err
10931092
}
@@ -1106,7 +1105,7 @@ func (p *stepProcessor) fileRefOrResource(ctx context.Context, ref v1alpha1.Acti
11061105
return []unstructured.Unstructured{*ref.Resource}, nil
11071106
}
11081107
if ref.File != "" {
1109-
ref, err := expressions.String(ctx, string(ref.File), bindings)
1108+
ref, err := ref.File.Value(ctx, bindings)
11101109
if err != nil {
11111110
return nil, err
11121111
}

0 commit comments

Comments
 (0)