From 1935061fc8bb347a828b296a17ee8b1e0c9b2985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ortiz=20Garc=C3=ADa?= Date: Wed, 6 Oct 2021 18:49:54 -0700 Subject: [PATCH] Handle kyaml Filter errors type Result as non-breaking errors and store in ResourceList This is a breaking change: - Result can only count as error when passed as pointer, this makes easy use of "errors.As" - Existing Filter() implementations that return Result as an error will no longer break the pipeline --- kyaml/fn/framework/framework.go | 14 +++++++++++++- kyaml/fn/framework/framework_test.go | 13 ++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/kyaml/fn/framework/framework.go b/kyaml/fn/framework/framework.go index 7e8555e059b..2fc460ccdc3 100644 --- a/kyaml/fn/framework/framework.go +++ b/kyaml/fn/framework/framework.go @@ -4,6 +4,7 @@ package framework import ( + goerrors "errors" "os" "sigs.k8s.io/kustomize/kyaml/errors" @@ -140,8 +141,19 @@ func Execute(p ResourceListProcessor, rlSource *kio.ByteReadWriter) error { // Filter executes the given kio.Filter and replaces the ResourceList's items with the result. // This can be used to help implement ResourceListProcessors. See SimpleProcessor for example. +// +// Filters that return a Result as error will store the result in the ResourceList instead of +// and continue processing instead of erroring out. func (rl *ResourceList) Filter(api kio.Filter) error { var err error rl.Items, err = api.Filter(rl.Items) - return errors.Wrap(err) + if err != nil { + var r Results + if goerrors.As(err, &r) { + rl.Results = append(rl.Results, r...) + return nil + } + return errors.Wrap(err) + } + return nil } diff --git a/kyaml/fn/framework/framework_test.go b/kyaml/fn/framework/framework_test.go index 400f1ba7fc7..39dc678f0bd 100644 --- a/kyaml/fn/framework/framework_test.go +++ b/kyaml/fn/framework/framework_test.go @@ -15,8 +15,8 @@ import ( ) func TestExecute_Result(t *testing.T) { - p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error { - err := &framework.Results{ + p := framework.SimpleProcessor{Filter: kio.FilterFunc(func(in []*yaml.RNode) ([]*yaml.RNode, error) { + return in, framework.Results{ { Message: "bad value for replicas", Severity: framework.Error, @@ -40,9 +40,7 @@ func TestExecute_Result(t *testing.T) { Tags: map[string]string{"foo": "bar"}, }, } - rl.Results = *err - return err - }) + })} out := new(bytes.Buffer) source := &kio.ByteReadWriter{Reader: bytes.NewBufferString(` kind: ResourceList @@ -57,10 +55,7 @@ items: replicas: 0 `), Writer: out} err := framework.Execute(p, source) - assert.EqualError(t, err, `[error] v1/Deployment/default/tester .spec.Replicas: bad value for replicas - -[error] : some error`) - assert.Equal(t, 1, err.(*framework.Results).ExitCode()) + assert.NoError(t, err) assert.Equal(t, `apiVersion: config.kubernetes.io/v1 kind: ResourceList items: