Skip to content

Commit

Permalink
refactor: more engine package (#1824)
Browse files Browse the repository at this point in the history
* chore: add unit tests (#1815)

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

* refactor: more engine package

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 Aug 2, 2024
1 parent f2cc745 commit 4b8269d
Show file tree
Hide file tree
Showing 38 changed files with 46 additions and 51 deletions.
4 changes: 3 additions & 1 deletion pkg/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

const PollInterval = 50 * time.Millisecond

func Key(obj metav1.Object) ObjectKey {
return ObjectKey{
Name: obj.GetName(),
Expand Down Expand Up @@ -60,7 +62,7 @@ func PatchObject(actual, expected runtime.Object) (runtime.Object, error) {

func WaitForDeletion(ctx context.Context, client Client, object Object) error {
key := Key(object)
return wait.PollUntilContextCancel(ctx, 50*time.Millisecond, true, func(ctx context.Context) (bool, error) {
return wait.PollUntilContextCancel(ctx, PollInterval, true, func(ctx context.Context) (bool, error) {
if err := client.Get(ctx, key, object); err != nil {
if kerrors.IsNotFound(err) {
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/assert/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
engineclient "github.com/kyverno/chainsaw/pkg/engine/client"
nspacer "github.com/kyverno/chainsaw/pkg/engine/namespacer"
opassert "github.com/kyverno/chainsaw/pkg/engine/operations/assert"
"github.com/kyverno/chainsaw/pkg/loaders/config"
"github.com/kyverno/chainsaw/pkg/loaders/resource"
opassert "github.com/kyverno/chainsaw/pkg/runner/operations/assert"
restutils "github.com/kyverno/chainsaw/pkg/utils/rest"
"github.com/kyverno/pkg/ext/output/color"
"github.com/spf13/cobra"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (o *operation) Exec(ctx context.Context, tc binding.Bindings) (_ outputs.Ou
func (o *operation) execute(ctx context.Context, tc binding.Bindings, obj unstructured.Unstructured) (outputs.Outputs, error) {
var lastErr error
var outputs outputs.Outputs
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (bool, error) {
outputs, lastErr = o.tryApplyResource(ctx, tc, obj)
// TODO: determine if the error can be retried
return lastErr == nil, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
operrors "github.com/kyverno/chainsaw/pkg/engine/operations/errors"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
operrors "github.com/kyverno/chainsaw/pkg/runner/operations/errors"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
"go.uber.org/multierr"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (o *operation) Exec(ctx context.Context, bindings binding.Bindings) (_ outp

func (o *operation) execute(ctx context.Context, bindings binding.Bindings, obj unstructured.Unstructured) error {
var lastErrs []error
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (_ bool, err error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (_ bool, err error) {
var errs []error
defer func() {
// record last errors only if there was no real error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
apibindings "github.com/kyverno/chainsaw/pkg/engine/bindings"
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
environment "github.com/kyverno/chainsaw/pkg/utils/env"
restutils "github.com/kyverno/chainsaw/pkg/utils/rest"
"github.com/kyverno/pkg/ext/output/color"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -80,7 +80,7 @@ func (o *operation) Exec(ctx context.Context, bindings binding.Bindings) (_ outp
func (o *operation) execute(ctx context.Context, bindings binding.Bindings, obj unstructured.Unstructured) (outputs.Outputs, error) {
var lastErr error
var outputs outputs.Outputs
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (bool, error) {
outputs, lastErr = o.tryCreateResource(ctx, bindings, obj)
// TODO: determine if the error can be retried
return lastErr == nil, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
"go.uber.org/multierr"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -128,7 +128,7 @@ func (o *operation) deleteResource(ctx context.Context, resource unstructured.Un
func (o *operation) waitForDeletion(ctx context.Context, resource unstructured.Unstructured) error {
gvk := resource.GetObjectKind().GroupVersionKind()
key := client.Key(&resource)
return wait.PollUntilContextCancel(ctx, internal.PollInterval, true, func(ctx context.Context) (bool, error) {
return wait.PollUntilContextCancel(ctx, client.PollInterval, true, func(ctx context.Context) (bool, error) {
var actual unstructured.Unstructured
actual.SetGroupVersionKind(gvk)
if err := o.client.Get(ctx, key, &actual); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
"go.uber.org/multierr"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -66,7 +66,7 @@ func (o *operation) Exec(ctx context.Context, bindings binding.Bindings) (_ outp

func (o *operation) execute(ctx context.Context, bindings binding.Bindings, obj unstructured.Unstructured) error {
var lastErrs []error
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (_ bool, err error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (_ bool, err error) {
var errs []error
defer func() {
// record last errors only if there was no real error
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
Expand Down Expand Up @@ -76,7 +76,7 @@ func (o *operation) Exec(ctx context.Context, bindings binding.Bindings) (_ outp
func (o *operation) execute(ctx context.Context, bindings binding.Bindings, obj unstructured.Unstructured) (outputs.Outputs, error) {
var lastErr error
var outputs outputs.Outputs
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (bool, error) {
outputs, lastErr = o.tryPatchResource(ctx, bindings, obj)
// TODO: determine if the error can be retried
return lastErr == nil, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
apibindings "github.com/kyverno/chainsaw/pkg/engine/bindings"
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
restutils "github.com/kyverno/chainsaw/pkg/utils/rest"
"github.com/kyverno/pkg/ext/output/color"
"k8s.io/client-go/rest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
)

type operation struct {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/checks"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/operations/internal"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -78,7 +78,7 @@ func (o *operation) Exec(ctx context.Context, bindings binding.Bindings) (_ outp
func (o *operation) execute(ctx context.Context, bindings binding.Bindings, obj unstructured.Unstructured) (outputs.Outputs, error) {
var lastErr error
var outputs outputs.Outputs
err := wait.PollUntilContextCancel(ctx, internal.PollInterval, false, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextCancel(ctx, client.PollInterval, false, func(ctx context.Context) (bool, error) {
outputs, lastErr = o.tryUpdateResource(ctx, bindings, obj)
// TODO: determine if the error can be retried
return lastErr == nil, nil
Expand Down
7 changes: 0 additions & 7 deletions pkg/runner/operations/internal/interval.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/runner/processors/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

"github.com/kyverno/chainsaw/pkg/engine"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/operations"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/report"
"github.com/kyverno/chainsaw/pkg/runner/failer"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/pkg/ext/output/color"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/processors/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/engine"
enginecontext "github.com/kyverno/chainsaw/pkg/engine/context"
"github.com/kyverno/chainsaw/pkg/engine/operations"
mock "github.com/kyverno/chainsaw/pkg/engine/operations/testing"
"github.com/kyverno/chainsaw/pkg/engine/outputs"
"github.com/kyverno/chainsaw/pkg/report"
"github.com/kyverno/chainsaw/pkg/runner/operations"
mock "github.com/kyverno/chainsaw/pkg/runner/operations/testing"
"github.com/kyverno/chainsaw/pkg/testing"
"github.com/stretchr/testify/assert"
)
Expand Down
22 changes: 11 additions & 11 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/kubectl"
"github.com/kyverno/chainsaw/pkg/engine/logging"
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
"github.com/kyverno/chainsaw/pkg/engine/operations"
opapply "github.com/kyverno/chainsaw/pkg/engine/operations/apply"
opassert "github.com/kyverno/chainsaw/pkg/engine/operations/assert"
opcommand "github.com/kyverno/chainsaw/pkg/engine/operations/command"
opcreate "github.com/kyverno/chainsaw/pkg/engine/operations/create"
opdelete "github.com/kyverno/chainsaw/pkg/engine/operations/delete"
operror "github.com/kyverno/chainsaw/pkg/engine/operations/error"
oppatch "github.com/kyverno/chainsaw/pkg/engine/operations/patch"
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/loaders/resource"
"github.com/kyverno/chainsaw/pkg/report"
"github.com/kyverno/chainsaw/pkg/runner/failer"
"github.com/kyverno/chainsaw/pkg/runner/operations"
opapply "github.com/kyverno/chainsaw/pkg/runner/operations/apply"
opassert "github.com/kyverno/chainsaw/pkg/runner/operations/assert"
opcommand "github.com/kyverno/chainsaw/pkg/runner/operations/command"
opcreate "github.com/kyverno/chainsaw/pkg/runner/operations/create"
opdelete "github.com/kyverno/chainsaw/pkg/runner/operations/delete"
operror "github.com/kyverno/chainsaw/pkg/runner/operations/error"
oppatch "github.com/kyverno/chainsaw/pkg/runner/operations/patch"
opscript "github.com/kyverno/chainsaw/pkg/runner/operations/script"
opsleep "github.com/kyverno/chainsaw/pkg/runner/operations/sleep"
opupdate "github.com/kyverno/chainsaw/pkg/runner/operations/update"
"github.com/kyverno/chainsaw/pkg/runner/timeout"
"github.com/kyverno/chainsaw/pkg/testing"
"github.com/kyverno/pkg/ext/output/color"
Expand Down

0 comments on commit 4b8269d

Please sign in to comment.