Skip to content

Commit

Permalink
Stop logging noisy errors when finishing/canceling scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Aug 23, 2023
1 parent cc455fa commit 5d2e5a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions webhosting-operator/pkg/experiment/generator/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *Every) AddToManager(mgr manager.Manager) error {
RateLimiter: &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(r.Rate, int(r.Rate))},
}).
WatchesRawSource(EmitN(reconcileWorkers), &handler.EnqueueRequestForObject{}).
Complete(r)
Complete(StopOnContextCanceled(r))
}

func (r *Every) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (r *ForEach[T]) AddToManager(mgr manager.Manager) error {
GenericFunc: func(event.GenericEvent) bool { return false },
}),
).
Complete(r)
Complete(StopOnContextCanceled(r))
}

func (r *ForEach[T]) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
Expand Down
15 changes: 15 additions & 0 deletions webhosting-operator/pkg/experiment/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package generator

import (
"context"
"errors"
"fmt"
"sync"
"time"
Expand All @@ -32,6 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

Expand Down Expand Up @@ -62,6 +64,19 @@ func EmitN(n int) source.Source {
})
}

// StopOnContextCanceled wraps the given reconciler so that "context canceled" errors are ignored. This is helpful when
// a reconciler is expected to be canceled (e.g., when the scenario finishes). We neither need to retry nor log on such
// errors. We can just stop silently.
func StopOnContextCanceled(r reconcile.Reconciler) reconcile.Reconciler {
return reconcile.Func(func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
result, err := r.Reconcile(ctx, request)
if errors.Is(err, context.Canceled) {
err = nil
}
return result, err
})
}

// NTimesConcurrently runs the given action n times. It distributes the work across the given number of concurrent
// workers.
func NTimesConcurrently(n, workers int, do func() error) error {
Expand Down

0 comments on commit 5d2e5a2

Please sign in to comment.