Skip to content

Commit

Permalink
refactor: timeouts management (#1719)
Browse files Browse the repository at this point in the history
* refactor: timeouts management

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Jul 24, 2024
1 parent 6a9ee23 commit f00417c
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 100 deletions.
71 changes: 67 additions & 4 deletions pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"context"
"path/filepath"
"time"

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
Expand All @@ -12,8 +13,18 @@ import (
"k8s.io/client-go/rest"
)

type Timeouts struct {
Apply time.Duration
Assert time.Duration
Cleanup time.Duration
Delete time.Duration
Error time.Duration
Exec time.Duration
}

type TestContext struct {
Summary
timeouts Timeouts
bindings binding.Bindings
clusters clusters.Registry
cluster string
Expand Down Expand Up @@ -44,18 +55,32 @@ func (tc *TestContext) Cluster() (*rest.Config, client.Client, error) {
return tc.clusters.Resolve(false, tc.cluster)
}

func (tc *TestContext) Timeouts() Timeouts {
return tc.timeouts
}

func (tc TestContext) WithBinding(ctx context.Context, name string, value any) TestContext {
tc.bindings = apibindings.RegisterNamedBinding(ctx, tc.bindings, name, value)
return tc
}

func (tc TestContext) WithCluster(ctx context.Context, name string, cluster clusters.Cluster) TestContext {
tc.clusters = tc.clusters.Register(name, cluster)
return tc
}

func (tc TestContext) WithBinding(ctx context.Context, name string, value any) TestContext {
tc.bindings = apibindings.RegisterNamedBinding(ctx, tc.bindings, name, value)
func (tc TestContext) WithTimeouts(ctx context.Context, timeouts Timeouts) TestContext {
tc.timeouts = timeouts
return tc
}

func WithValues(ctx context.Context, tc TestContext, values any) TestContext {
return tc.WithBinding(ctx, "values", values)
func WithBindings(ctx context.Context, tc TestContext, variables ...v1alpha1.Binding) (TestContext, error) {
bindings, err := apibindings.RegisterBindings(ctx, tc.Bindings(), variables...)
if err != nil {
return tc, err
}
tc.bindings = bindings
return tc, nil
}

func WithClusters(ctx context.Context, tc TestContext, basePath string, c map[string]v1alpha1.Cluster) TestContext {
Expand All @@ -67,6 +92,44 @@ func WithClusters(ctx context.Context, tc TestContext, basePath string, c map[st
return tc
}

func WithDefaultTimeouts(ctx context.Context, tc TestContext, timeouts v1alpha1.DefaultTimeouts) TestContext {
return tc.WithTimeouts(ctx, Timeouts{
Apply: timeouts.Apply.Duration,
Assert: timeouts.Assert.Duration,
Cleanup: timeouts.Cleanup.Duration,
Delete: timeouts.Delete.Duration,
Error: timeouts.Error.Duration,
Exec: timeouts.Exec.Duration,
})
}

func WithTimeouts(ctx context.Context, tc TestContext, timeouts v1alpha1.Timeouts) TestContext {
old := tc.timeouts
if new := timeouts.Apply; new != nil {
old.Apply = new.Duration
}
if new := timeouts.Assert; new != nil {
old.Assert = new.Duration
}
if new := timeouts.Cleanup; new != nil {
old.Cleanup = new.Duration
}
if new := timeouts.Delete; new != nil {
old.Delete = new.Duration
}
if new := timeouts.Error; new != nil {
old.Error = new.Duration
}
if new := timeouts.Exec; new != nil {
old.Exec = new.Duration
}
return tc.WithTimeouts(ctx, old)
}

func WithValues(ctx context.Context, tc TestContext, values any) TestContext {
return tc.WithBinding(ctx, "values", values)
}

func UseCluster(ctx context.Context, tc TestContext, name string) (TestContext, error) {
clusterConfig, clusterClient, err := tc.clusters.Resolve(false)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/model/evaluate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model

import (
"context"
"fmt"

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/mutate"
"github.com/kyverno/chainsaw/pkg/runner/functions"
"github.com/kyverno/kyverno-json/pkg/engine/template"
)

func Evaluate[T any](ctx context.Context, in string, bindings binding.Bindings) (T, error) {
var def T
if converted, err := mutate.Mutate(ctx, nil, mutate.Parse(ctx, in), nil, bindings, template.WithFunctionCaller(functions.Caller)); err != nil {
return def, err
} else if converted, ok := converted.(T); !ok {
return converted, fmt.Errorf("expression didn't evaluate to the expected type (%s)", in)
} else {
return converted, nil
}
}

func String(ctx context.Context, in string, bindings binding.Bindings) (string, error) {
return Evaluate[string](ctx, in, bindings)
}
Loading

0 comments on commit f00417c

Please sign in to comment.