Skip to content

Commit

Permalink
feat: migrate to new expressions (#1843)
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 Aug 10, 2024
1 parent 92ec50d commit 7fb94ad
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 82 deletions.
16 changes: 8 additions & 8 deletions pkg/apis/v1alpha1/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type ActionObjectSelector struct {

// Selector defines labels selector.
// +optional
Selector string `json:"selector,omitempty"`
Selector Expression `json:"selector,omitempty"`
}

// ActionOutputs contains outputs options for an action.
Expand Down Expand Up @@ -274,7 +274,7 @@ type PodLogs struct {

// Container in pod to get logs from else --all-containers is used.
// +optional
Container string `json:"container,omitempty"`
Container Expression `json:"container,omitempty"`

// Tail is the number of last lines to collect from pods. If omitted or zero,
// then the default is 10 if you use a selector, or -1 (all) if you use a pod name.
Expand All @@ -293,11 +293,11 @@ type Proxy struct {

// TargetPort defines the target port to proxy the request.
// +optional
TargetPort string `json:"port,omitempty"`
TargetPort Expression `json:"port,omitempty"`

// TargetPath defines the target path to proxy the request.
// +optional
TargetPath string `json:"path,omitempty"`
TargetPath Expression `json:"path,omitempty"`
}

// Script describes a script to run as a part of a test step.
Expand Down Expand Up @@ -361,11 +361,11 @@ type WaitFor struct {
// WaitForCondition represents parameters for waiting on a specific condition of a resource.
type WaitForCondition struct {
// Name defines the specific condition to wait for, e.g., "Available", "Ready".
Name string `json:"name"`
Name Expression `json:"name"`

// Value defines the specific condition status to wait for, e.g., "True", "False".
// +optional
Value *string `json:"value,omitempty"`
Value *Expression `json:"value,omitempty"`
}

// WaitForDeletion represents parameters for waiting on a resource's deletion.
Expand All @@ -374,8 +374,8 @@ type WaitForDeletion struct{}
// WaitForJsonPath represents parameters for waiting on a json path of a resource.
type WaitForJsonPath struct {
// Path defines the json path to wait for, e.g. '{.status.phase}'.
Path string `json:"path"`
Path Expression `json:"path"`

// Value defines the expected value to wait for, e.g., "Running".
Value string `json:"value"`
Value Expression `json:"value"`
}
20 changes: 11 additions & 9 deletions pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type Any = v1alpha1.Any
// Binding represents a key/value set as a binding in an executing test.
type Binding struct {
// Name the name of the binding.
// +kubebuilder:validation:Pattern=`^(?:\w+|\(.+\))$`
Name string `json:"name"`
// +kubebuilder:validation:Type:=string
// +kubebuilder:validation:Pattern:=`^(?:\w+|\(.+\))$`
Name Expression `json:"name"`

// Value value of the binding.
// +kubebuilder:validation:Schemaless
Expand All @@ -29,7 +30,7 @@ type Binding struct {
}

func (b Binding) CheckName() error {
if !identifier.MatchString(b.Name) {
if !identifier.MatchString(string(b.Name)) {
return fmt.Errorf("invalid name %s", b.Name)
}
return nil
Expand Down Expand Up @@ -70,8 +71,9 @@ func (e Expression) Value(ctx context.Context, bindings binding.Bindings) (strin
}

// Format determines the output format (json or yaml).
// +kubebuilder:validation:Pattern=`^(?:json|yaml|\(.+\))$`
type Format string
// +kubebuilder:validation:Type:=string
// +kubebuilder:validation:Pattern:=`^(?:json|yaml|\(.+\))$`
type Format Expression

// Match represents a match condition against an evaluated object.
type Match = Any
Expand All @@ -81,12 +83,12 @@ type ObjectName struct {
// Namespace of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
// +optional
Namespace string `json:"namespace,omitempty"`
Namespace Expression `json:"namespace,omitempty"`

// Name of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
// +optional
Name string `json:"name,omitempty"`
Name Expression `json:"name,omitempty"`
}

// ObjectReference represents one or more objects with a specific apiVersion and kind.
Expand All @@ -104,11 +106,11 @@ type ObjectReference struct {
// ObjectType represents a specific apiVersion and kind.
type ObjectType struct {
// API version of the referent.
APIVersion string `json:"apiVersion"`
APIVersion Expression `json:"apiVersion"`

// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
Kind string `json:"kind"`
Kind Expression `json:"kind"`
}

// Output represents an output binding with a match to determine if the binding must be considered or not.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestBinding_CheckName(t *testing.T) {
tests := []struct {
name string
bindingName string
bindingName Expression
bindingValue Any
wantErr bool
}{{
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions pkg/commands/migrate/kuttl/tests/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ func testStep(to *v1alpha1.TestStepSpec, in unstructured.Unstructured) error {
Delete: &v1alpha1.Delete{
Ref: &v1alpha1.ObjectReference{
ObjectType: v1alpha1.ObjectType{
APIVersion: operation.APIVersion,
Kind: operation.Kind,
APIVersion: v1alpha1.Expression(operation.APIVersion),
Kind: v1alpha1.Expression(operation.Kind),
},
ObjectName: v1alpha1.ObjectName{
Namespace: operation.Namespace,
Name: operation.Name,
Namespace: v1alpha1.Expression(operation.Namespace),
Name: v1alpha1.Expression(operation.Name),
},
Labels: operation.Labels,
},
Expand Down Expand Up @@ -506,12 +506,12 @@ func testAssert(to *v1alpha1.TestStepSpec, in unstructured.Unstructured) error {
op := &v1alpha1.PodLogs{
ActionObjectSelector: v1alpha1.ActionObjectSelector{
ObjectName: v1alpha1.ObjectName{
Name: collector.Pod,
Namespace: collector.Namespace,
Name: v1alpha1.Expression(collector.Pod),
Namespace: v1alpha1.Expression(collector.Namespace),
},
Selector: collector.Selector,
Selector: v1alpha1.Expression(collector.Selector),
},
Container: collector.Container,
Container: v1alpha1.Expression(collector.Container),
}
if collector.Tail != 0 {
op.Tail = ptr.To(collector.Tail)
Expand All @@ -531,10 +531,10 @@ func testAssert(to *v1alpha1.TestStepSpec, in unstructured.Unstructured) error {
Events: &v1alpha1.Events{
ActionObjectSelector: v1alpha1.ActionObjectSelector{
ObjectName: v1alpha1.ObjectName{
Name: collector.Pod,
Namespace: collector.Namespace,
Name: v1alpha1.Expression(collector.Pod),
Namespace: v1alpha1.Expression(collector.Namespace),
},
Selector: collector.Selector,
Selector: v1alpha1.Expression(collector.Selector),
},
},
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/engine/bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/expressions"
)

var identifier = regexp.MustCompile(`^\w+$`)
Expand All @@ -25,7 +24,7 @@ func RegisterBinding(ctx context.Context, bindings binding.Bindings, name string
}

func ResolveBinding(ctx context.Context, bindings binding.Bindings, input any, variable v1alpha1.Binding) (string, any, error) {
name, err := expressions.String(ctx, variable.Name, bindings)
name, err := variable.Name.Value(ctx, bindings)
if err != nil {
return "", nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/engine/kubectl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/expressions"
)

func Describe(ctx context.Context, client client.Client, tc binding.Bindings, collector *v1alpha1.Describe) (string, []string, error) {
if collector == nil {
return "", nil, errors.New("collector is null")
}
name, err := expressions.String(ctx, collector.Name, tc)
name, err := collector.Name.Value(ctx, tc)
if err != nil {
return "", nil, err
}
namespace, err := expressions.String(ctx, collector.Namespace, tc)
namespace, err := collector.Namespace.Value(ctx, tc)
if err != nil {
return "", nil, err
}
selector, err := expressions.String(ctx, collector.Selector, tc)
selector, err := collector.Selector.Value(ctx, tc)
if err != nil {
return "", nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/engine/kubectl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/expressions"
)

func Get(ctx context.Context, client client.Client, tc binding.Bindings, collector *v1alpha1.Get) (string, []string, error) {
if collector == nil {
return "", nil, errors.New("collector is null")
}
name, err := expressions.String(ctx, collector.Name, tc)
name, err := collector.Name.Value(ctx, tc)
if err != nil {
return "", nil, err
}
namespace, err := expressions.String(ctx, collector.Namespace, tc)
namespace, err := collector.Namespace.Value(ctx, tc)
if err != nil {
return "", nil, err
}
selector, err := expressions.String(ctx, collector.Selector, tc)
selector, err := collector.Selector.Value(ctx, tc)
if err != nil {
return "", nil, err
}
format, err := expressions.String(ctx, string(collector.Format), tc)
format, err := v1alpha1.Expression(collector.Format).Value(ctx, tc)
if err != nil {
return "", nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/engine/kubectl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import (

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/expressions"
)

func Logs(ctx context.Context, tc binding.Bindings, collector *v1alpha1.PodLogs) (string, []string, error) {
if collector == nil {
return "", nil, errors.New("collector is null")
}
name, err := expressions.String(ctx, collector.Name, tc)
name, err := collector.Name.Value(ctx, tc)
if err != nil {
return "", nil, err
}
namespace, err := expressions.String(ctx, collector.Namespace, tc)
namespace, err := collector.Namespace.Value(ctx, tc)
if err != nil {
return "", nil, err
}
selector, err := expressions.String(ctx, collector.Selector, tc)
selector, err := collector.Selector.Value(ctx, tc)
if err != nil {
return "", nil, err
}
container, err := expressions.String(ctx, collector.Container, tc)
container, err := collector.Container.Value(ctx, tc)
if err != nil {
return "", nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/engine/kubectl/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/expressions"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func mapResource(ctx context.Context, client client.Client, tc binding.Bindings, resource v1alpha1.ObjectType) (string, bool, error) {
if resource.APIVersion != "" && resource.Kind != "" {
if apiVersion, err := expressions.String(ctx, resource.APIVersion, tc); err != nil {
if apiVersion, err := resource.APIVersion.Value(ctx, tc); err != nil {
return "", false, err
} else if kind, err := expressions.String(ctx, resource.Kind, tc); err != nil {
} else if kind, err := resource.Kind.Value(ctx, tc); err != nil {
return "", false, err
} else {
return mapResourceFromApiVersionAndKind(client, apiVersion, kind)
Expand Down
9 changes: 4 additions & 5 deletions pkg/engine/kubectl/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/expressions"
)

func Proxy(ctx context.Context, client client.Client, tc binding.Bindings, collector *v1alpha1.Proxy) (string, []string, error) {
if collector == nil {
return "", nil, errors.New("collector is null")
}
name, err := expressions.String(ctx, collector.Name, tc)
name, err := collector.Name.Value(ctx, tc)
if err != nil {
return "", nil, err
}
namespace, err := expressions.String(ctx, collector.Namespace, tc)
namespace, err := collector.Namespace.Value(ctx, tc)
if err != nil {
return "", nil, err
}
targetPath, err := expressions.String(ctx, collector.TargetPath, tc)
targetPath, err := collector.TargetPath.Value(ctx, tc)
if err != nil {
return "", nil, err
}
targetPort, err := expressions.String(ctx, collector.TargetPort, tc)
targetPort, err := collector.TargetPort.Value(ctx, tc)
if err != nil {
return "", nil, err
}
Expand Down
Loading

0 comments on commit 7fb94ad

Please sign in to comment.