Handle kyaml Filter errors type Result as non-breaking errors and store in ResourceList

- Result can only count as error when passed as pointer, this makes easy use of "errors.As"
- Existing Filter() implementations that return Result from the `framework` package won't return an error anymore but modify the ResourceList
This commit is contained in:
Carlos Ortiz García
2021-10-06 18:49:54 -07:00
parent 326a57a9cc
commit 894ffec36a
2 changed files with 17 additions and 10 deletions

View File

@@ -4,6 +4,7 @@
package framework package framework
import ( import (
goerrors "errors"
"os" "os"
"sigs.k8s.io/kustomize/kyaml/errors" "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. // 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. // 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 { func (rl *ResourceList) Filter(api kio.Filter) error {
var err error var err error
rl.Items, err = api.Filter(rl.Items) 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
} }

View File

@@ -15,8 +15,8 @@ import (
) )
func TestExecute_Result(t *testing.T) { func TestExecute_Result(t *testing.T) {
p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error { p := framework.SimpleProcessor{Filter: kio.FilterFunc(func(in []*yaml.RNode) ([]*yaml.RNode, error) {
err := &framework.Results{ return in, framework.Results{
{ {
Message: "bad value for replicas", Message: "bad value for replicas",
Severity: framework.Error, Severity: framework.Error,
@@ -40,9 +40,7 @@ func TestExecute_Result(t *testing.T) {
Tags: map[string]string{"foo": "bar"}, Tags: map[string]string{"foo": "bar"},
}, },
} }
rl.Results = *err })}
return err
})
out := new(bytes.Buffer) out := new(bytes.Buffer)
source := &kio.ByteReadWriter{Reader: bytes.NewBufferString(` source := &kio.ByteReadWriter{Reader: bytes.NewBufferString(`
kind: ResourceList kind: ResourceList
@@ -57,10 +55,7 @@ items:
replicas: 0 replicas: 0
`), Writer: out} `), Writer: out}
err := framework.Execute(p, source) err := framework.Execute(p, source)
assert.EqualError(t, err, `[error] v1/Deployment/default/tester .spec.Replicas: bad value for replicas assert.NoError(t, err)
[error]: some error`)
assert.Equal(t, 1, err.(*framework.Results).ExitCode())
assert.Equal(t, `apiVersion: config.kubernetes.io/v1 assert.Equal(t, `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
items: items: