Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add expression method to get value #1841

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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)

Check warning on line 69 in pkg/apis/v1alpha1/types.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/v1alpha1/types.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

// 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
Loading