Skip to content

Commit

Permalink
flesh out errorList
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com>
  • Loading branch information
acpana committed Oct 13, 2023
1 parent 9937605 commit 05af5c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
44 changes: 35 additions & 9 deletions pkg/watch/errorlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions pkg/watch/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}

Expand All @@ -273,15 +273,15 @@ 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))
}
}

if err := wm.metrics.reportGvkCount(int64(len(wm.watchedKinds))); err != nil {
log.Error(err, "while trying to report gvk count metric")
}

if len(errlist.errs) > 0 {
if errlist.Size() > 0 {
return errlist
}
return nil
Expand Down

0 comments on commit 05af5c9

Please sign in to comment.