From 4c97d1276ca765bc9ba181a6a280b25b75a713dd Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Mon, 6 Jan 2025 12:31:43 +0000 Subject: [PATCH] helm waiter Signed-off-by: Austin Abro --- pkg/kube/client.go | 4 ++-- pkg/kube/statuswait.go | 4 ++++ pkg/kube/statuswait_test.go | 2 +- pkg/kube/wait.go | 14 +++++++------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index ce22f265a..fe830747d 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -126,7 +126,7 @@ func NewWaiter(strategy WaitStrategy, factory Factory, log func(string, ...inter if err != nil { return nil, err } - return &waiter{kubeClient: kc, log: log}, nil + return &HelmWaiter{kubeClient: kc, log: log}, nil case StatusWaiter: sw, err := getStatusWatcher(factory) if err != nil { @@ -333,7 +333,7 @@ func getResource(info *resource.Info) (runtime.Object, error) { // WaitForDelete wait up to the given timeout for the specified resources to be deleted. func (c *Client) WaitForDelete(resources ResourceList, timeout time.Duration) error { - w := waiter{ + w := HelmWaiter{ log: c.Log, timeout: timeout, } diff --git a/pkg/kube/statuswait.go b/pkg/kube/statuswait.go index d58e34cdc..bbc92292d 100644 --- a/pkg/kube/statuswait.go +++ b/pkg/kube/statuswait.go @@ -51,6 +51,8 @@ func (w *statusWaiter) WaitWithJobs(resourceList ResourceList, timeout time.Dura } func (w *statusWaiter) waitForDelete(ctx context.Context, resourceList ResourceList) error { + deadline, _ := ctx.Deadline() + w.log("beginning wait for %d resources to be deleted with timeout of %v", len(resourceList), time.Until(deadline)) cancelCtx, cancel := context.WithCancel(ctx) defer cancel() runtimeObjs := []runtime.Object{} @@ -110,6 +112,8 @@ func (w *statusWaiter) waitForDelete(ctx context.Context, resourceList ResourceL } func (w *statusWaiter) wait(ctx context.Context, resourceList ResourceList, waitForJobs bool) error { + deadline, _ := ctx.Deadline() + w.log("beginning wait for %d resources with timeout of %v", len(resourceList), deadline) cancelCtx, cancel := context.WithCancel(ctx) defer cancel() runtimeObjs := []runtime.Object{} diff --git a/pkg/kube/statuswait_test.go b/pkg/kube/statuswait_test.go index 31211d226..b018691cd 100644 --- a/pkg/kube/statuswait_test.go +++ b/pkg/kube/statuswait_test.go @@ -143,7 +143,7 @@ func TestStatusWaitForDelete(t *testing.T) { statusWatcher := watcher.NewDefaultStatusWatcher(fakeClient, fakeMapper) kwaiter := statusWaiter{ sw: statusWatcher, - log: log.Printf, + log: t.Logf, } ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() diff --git a/pkg/kube/wait.go b/pkg/kube/wait.go index 0ee4504cb..044bbbe1d 100644 --- a/pkg/kube/wait.go +++ b/pkg/kube/wait.go @@ -39,20 +39,20 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) -type waiter struct { +type HelmWaiter struct { c ReadyChecker timeout time.Duration log func(string, ...interface{}) kubeClient *kubernetes.Clientset } -func (w *waiter) Wait(resources ResourceList, timeout time.Duration) error { +func (w *HelmWaiter) Wait(resources ResourceList, timeout time.Duration) error { w.c = NewReadyChecker(w.kubeClient, w.log, PausedAsReady(true)) w.timeout = timeout return w.waitForResources(resources) } -func (w *waiter) WaitWithJobs(resources ResourceList, timeout time.Duration) error { +func (w *HelmWaiter) WaitWithJobs(resources ResourceList, timeout time.Duration) error { w.c = NewReadyChecker(w.kubeClient, w.log, PausedAsReady(true), CheckJobs(true)) w.timeout = timeout return w.waitForResources(resources) @@ -60,7 +60,7 @@ func (w *waiter) WaitWithJobs(resources ResourceList, timeout time.Duration) err // waitForResources polls to get the current status of all pods, PVCs, Services and // Jobs(optional) until all are ready or a timeout is reached -func (w *waiter) waitForResources(created ResourceList) error { +func (w *HelmWaiter) waitForResources(created ResourceList) error { w.log("beginning wait for %d resources with timeout of %v", len(created), w.timeout) ctx, cancel := context.WithTimeout(context.Background(), w.timeout) @@ -94,7 +94,7 @@ func (w *waiter) waitForResources(created ResourceList) error { }) } -func (w *waiter) isRetryableError(err error, resource *resource.Info) bool { +func (w *HelmWaiter) isRetryableError(err error, resource *resource.Info) bool { if err == nil { return false } @@ -109,12 +109,12 @@ func (w *waiter) isRetryableError(err error, resource *resource.Info) bool { return true } -func (w *waiter) isRetryableHTTPStatusCode(httpStatusCode int32) bool { +func (w *HelmWaiter) isRetryableHTTPStatusCode(httpStatusCode int32) bool { return httpStatusCode == 0 || httpStatusCode == http.StatusTooManyRequests || (httpStatusCode >= 500 && httpStatusCode != http.StatusNotImplemented) } // waitForDeletedResources polls to check if all the resources are deleted or a timeout is reached -func (w *waiter) waitForDeletedResources(deleted ResourceList) error { +func (w *HelmWaiter) waitForDeletedResources(deleted ResourceList) error { w.log("beginning wait for %d resources to be deleted with timeout of %v", len(deleted), w.timeout) ctx, cancel := context.WithTimeout(context.Background(), w.timeout)