Merge pull request #82120 from soltysh/bug1745532

FlattenListVisitor now continues traversal on errors and returns an aggregate error
This commit is contained in:
Kubernetes Prow Robot 2019-08-30 09:40:08 -07:00 committed by GitHub
commit 4cc80bac91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View File

@ -372,10 +372,9 @@ func (v ContinueOnErrorVisitor) Visit(fn VisitorFunc) error {
// FlattenListVisitor flattens any objects that runtime.ExtractList recognizes as a list // FlattenListVisitor flattens any objects that runtime.ExtractList recognizes as a list
// - has an "Items" public field that is a slice of runtime.Objects or objects satisfying // - has an "Items" public field that is a slice of runtime.Objects or objects satisfying
// that interface - into multiple Infos. An error on any sub item (for instance, if a List // that interface - into multiple Infos. Returns nil in the case of no errors.
// contains an object that does not have a registered client or resource) will terminate // When an error is hit on sub items (for instance, if a List contains an object that does
// the visit. // not have a registered client or resource), returns an aggregate error.
// TODO: allow errors to be aggregated?
type FlattenListVisitor struct { type FlattenListVisitor struct {
visitor Visitor visitor Visitor
typer runtime.ObjectTyper typer runtime.ObjectTyper
@ -425,20 +424,22 @@ func (v FlattenListVisitor) Visit(fn VisitorFunc) error {
if info.Mapping != nil && !info.Mapping.GroupVersionKind.Empty() { if info.Mapping != nil && !info.Mapping.GroupVersionKind.Empty() {
preferredGVKs = append(preferredGVKs, info.Mapping.GroupVersionKind) preferredGVKs = append(preferredGVKs, info.Mapping.GroupVersionKind)
} }
errs := []error{}
for i := range items { for i := range items {
item, err := v.mapper.infoForObject(items[i], v.typer, preferredGVKs) item, err := v.mapper.infoForObject(items[i], v.typer, preferredGVKs)
if err != nil { if err != nil {
return err errs = append(errs, err)
continue
} }
if len(info.ResourceVersion) != 0 { if len(info.ResourceVersion) != 0 {
item.ResourceVersion = info.ResourceVersion item.ResourceVersion = info.ResourceVersion
} }
if err := fn(item, nil); err != nil { if err := fn(item, nil); err != nil {
return err errs = append(errs, err)
} }
} }
return nil return utilerrors.NewAggregate(errs)
}) })
} }

View File

@ -18,9 +18,11 @@ package resource
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
"time" "time"
@ -180,3 +182,18 @@ func TestFlattenListVisitor(t *testing.T) {
t.Fatal(spew.Sdump(test.Infos)) t.Fatal(spew.Sdump(test.Infos))
} }
} }
func TestFlattenListVisitorWithVisitorError(t *testing.T) {
b := newDefaultBuilder().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../artifacts/deeply-nested.yaml"}}).
Flatten()
test := &testVisitor{InjectErr: errors.New("visitor error")}
err := b.Do().Visit(test.Handle)
if err == nil || !strings.Contains(err.Error(), "visitor error") {
t.Fatal(err)
}
if len(test.Infos) != 6 {
t.Fatal(spew.Sdump(test.Infos))
}
}