From 85851c3dd9720d54e227cf5a443823eb124583fc Mon Sep 17 00:00:00 2001 From: "Anhj An anhongyang125@163.com" Date: Mon, 14 Nov 2022 10:28:41 +0800 Subject: [PATCH] This change adds support for timeout of each resource Update pkg\kube\wait.go, pkg\kube\client.go Signed-off-by: Anhj An --- pkg/kube/client.go | 16 ++++++++++++++++ pkg/kube/wait.go | 14 ++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 5e75c34e4..3da0d056b 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -147,6 +147,22 @@ func (c *Client) Wait(resources ResourceList, timeout time.Duration) error { return w.waitForResources(resources) } +// Wait waits up to the given timeout for the specified resources to be ready. perResourceTimeout means the timeout of each resource +func (c *Client) WaitWithPerResource(resources ResourceList, timeout, perResourceTimeout time.Duration) error { + cs, err := c.getKubeClient() + if err != nil { + return err + } + checker := NewReadyChecker(cs, c.Log, PausedAsReady(true)) + w := waiter{ + c: checker, + log: c.Log, + timeout: timeout, + perResourceTimeout: perResourceTimeout, + } + return w.waitForResources(resources) +} + // WaitWithJobs wait up to the given timeout for the specified resources to be ready, including jobs. func (c *Client) WaitWithJobs(resources ResourceList, timeout time.Duration) error { cs, err := c.getKubeClient() diff --git a/pkg/kube/wait.go b/pkg/kube/wait.go index fd01e9bc7..fdfb97c79 100644 --- a/pkg/kube/wait.go +++ b/pkg/kube/wait.go @@ -39,9 +39,10 @@ import ( ) type waiter struct { - c ReadyChecker - timeout time.Duration - log func(string, ...interface{}) + c ReadyChecker + timeout time.Duration + perResourceTimeout time.Duration + log func(string, ...interface{}) } // isServiceUnavailable helps figure out if the error is caused by etcd not being available @@ -68,7 +69,12 @@ func (w *waiter) waitForResources(created ResourceList) error { ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() - return wait.PollImmediateUntil(2*time.Second, func() (bool, error) { + timeout := 2 * time.Second + if w.perResourceTimeout > timeout { + timeout = w.perResourceTimeout + } + + return wait.PollImmediateUntil(timeout, func() (bool, error) { for _, v := range created { ready, err := w.c.IsReady(ctx, v) if !ready || err != nil {