From d24405435a6e40b5c20c849bbc7343d729406848 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Wed, 8 May 2024 16:23:15 +0200 Subject: [PATCH] Return the context's cancel cause in the Watcher Signed-off-by: Tom Wieczorek --- pkg/kubernetes/watch/watcher.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/kubernetes/watch/watcher.go b/pkg/kubernetes/watch/watcher.go index ccf1091f5ed2..b39864197328 100644 --- a/pkg/kubernetes/watch/watcher.go +++ b/pkg/kubernetes/watch/watcher.go @@ -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) @@ -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) @@ -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 }