Skip to content

Commit

Permalink
Return the context's cancel cause in the Watcher
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed May 8, 2024
1 parent e7b449c commit d244054
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/kubernetes/watch/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ func (w *Watcher[T]) WithErrorCallback(callback ErrorCallback) *Watcher[T] {
return w
}

// Until runs a watch until condition returns true. It will error out in case
// the context gets canceled or the condition returns an error.
// Until runs a watch until condition returns true. It will return the error
// returned by condition, nil if condition returns true, or the context's cause
// in case the context gets cancelled, whichever happens first.
func (w *Watcher[T]) Until(ctx context.Context, condition Condition[T]) error {
return retry(ctx, w.errorCallback, func(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -304,7 +305,7 @@ func (w *Watcher[T]) watch(ctx context.Context, resourceVersion string, conditio
for startWatch != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
return nil, context.Cause(ctx)

case <-watchTimeout.C:
return nil, apierrors.NewTimeoutError("server unexpectedly didn't close the watch", 1)
Expand Down Expand Up @@ -422,7 +423,7 @@ func retry(ctx context.Context, errorCallback ErrorCallback, runWatch func(conte
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
return context.Cause(ctx)
case <-timer.C:
continue
}
Expand Down

0 comments on commit d244054

Please sign in to comment.