Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "Result" errors as non-fatal errors in kyaml FilterFuncs #4241

Merged
merged 2 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion kyaml/fn/framework/framework.go
Expand Up @@ -4,6 +4,7 @@
package framework

import (
goerrors "errors"
"os"

"sigs.k8s.io/kustomize/kyaml/errors"
Expand Down Expand Up @@ -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
// 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
}
13 changes: 4 additions & 9 deletions kyaml/fn/framework/framework_test.go
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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:
Expand Down