From 05af5c9ef891c7b8c6009eab335991ff35164916 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Fri, 13 Oct 2023 23:57:14 +0000 Subject: [PATCH] flesh out errorList Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- pkg/watch/errorlist.go | 44 +++++++++++++++++++++++++++++++++--------- pkg/watch/manager.go | 8 ++++---- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pkg/watch/errorlist.go b/pkg/watch/errorlist.go index 4065b3c2a0e..a691efc63f4 100644 --- a/pkg/watch/errorlist.go +++ b/pkg/watch/errorlist.go @@ -16,16 +16,17 @@ limitations under the License. package watch import ( + "errors" "fmt" "strings" "k8s.io/apimachinery/pkg/runtime/schema" ) -// errorList is an error that aggregates multiple gvkErrs. +// errorList is an error that aggregates multiple errors. type errorList struct { - errs []gvkErr - isUniversal bool + errs []error + containsUniversalErr bool } type WatchesError interface { @@ -50,11 +51,11 @@ func (w gvkErr) Error() string { return fmt.Sprintf("error for gvk: %s: %s", w.gvk, w.err.Error()) } -func (e errorList) String() string { +func (e *errorList) String() string { return e.Error() } -func (e errorList) Error() string { +func (e *errorList) Error() string { var builder strings.Builder for i, err := range e.errs { if i > 0 { @@ -65,15 +66,40 @@ func (e errorList) Error() string { return builder.String() } -func (e errorList) FailingGVKs() []schema.GroupVersionKind { +// returns a new errorList type. +func newErrList() *errorList { + return &errorList{ + errs: []error{}, + } +} + +func (e *errorList) FailingGVKs() []schema.GroupVersionKind { gvks := make([]schema.GroupVersionKind, len(e.errs)) for _, err := range e.errs { - gvks = append(gvks, err.gvk) + var gvkErr gvkErr + if errors.As(err, &gvkErr) { + gvks = append(gvks, gvkErr.gvk) + } } return gvks } -func (e errorList) IsUniversal() bool { - return e.isUniversal +func (e *errorList) IsUniversal() bool { + return e.containsUniversalErr +} + +// adds a non gvk specific error to the list. +func (e *errorList) Add(err error) { + e.errs = append(e.errs, err) + e.containsUniversalErr = true +} + +// adds a gvk specific error to the list. +func (e *errorList) AddGVKErr(gvk schema.GroupVersionKind, err error) { + e.errs = append(e.errs, gvkErr{gvk: gvk, err: err}) +} + +func (e *errorList) Size() int { + return len(e.errs) } diff --git a/pkg/watch/manager.go b/pkg/watch/manager.go index a54137a10e0..a6aefb95c61 100644 --- a/pkg/watch/manager.go +++ b/pkg/watch/manager.go @@ -254,7 +254,7 @@ func (wm *Manager) replaceWatches(ctx context.Context, r *Registrar) error { wm.watchedMux.Lock() defer wm.watchedMux.Unlock() - errlist := errorList{errs: []gvkErr{}} + errlist := newErrList() desired := wm.managedKinds.Get() for gvk := range wm.watchedKinds { @@ -263,7 +263,7 @@ func (wm *Manager) replaceWatches(ctx context.Context, r *Registrar) error { continue } if err := wm.doRemoveWatch(ctx, r, gvk); err != nil { - errlist.errs = append(errlist.errs, gvkErr{gvk: gvk, err: fmt.Errorf("removing watch for %+v %w", gvk, err)}) + errlist.AddGVKErr(gvk, fmt.Errorf("removing watch for %+v %w", gvk, err)) } } @@ -273,7 +273,7 @@ func (wm *Manager) replaceWatches(ctx context.Context, r *Registrar) error { continue } if err := wm.doAddWatch(ctx, r, gvk); err != nil { - errlist.errs = append(errlist.errs, gvkErr{gvk: gvk, err: fmt.Errorf("adding watch for %+v %w", gvk, err)}) + errlist.AddGVKErr(gvk, fmt.Errorf("adding watch for %+v %w", gvk, err)) } } @@ -281,7 +281,7 @@ func (wm *Manager) replaceWatches(ctx context.Context, r *Registrar) error { log.Error(err, "while trying to report gvk count metric") } - if len(errlist.errs) > 0 { + if errlist.Size() > 0 { return errlist } return nil