From 64937f1f68a600d2fd745a460c7b536134ba57ae Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Wed, 28 Jul 2021 09:17:05 +0530 Subject: [PATCH] e2e: add retryKubectlArgs helper for kubectl retry added helper function retryKubectlArgs to perform action if its a known error. Signed-off-by: Madhu Rajanna (cherry picked from commit 9f0af30735f34b4977e56a29e4035ce3edd8fc0c) --- e2e/utils.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/e2e/utils.go b/e2e/utils.go index eed83ccb9..e51874af7 100644 --- a/e2e/utils.go +++ b/e2e/utils.go @@ -1282,3 +1282,33 @@ func retryKubectlFile(namespace string, action kubectlAction, filename string, t return true, nil }) } + +// retryKubectlArgs takes a namespace and action telling kubectl what to do +// with the passed arguments. This function retries until no error occurred, or +// the timeout passed. +func retryKubectlArgs(namespace string, action kubectlAction, t int, args ...string) error { + timeout := time.Duration(t) * time.Minute + args = append([]string{string(action)}, args...) + e2elog.Logf("waiting for kubectl (%s args) to finish", args) + start := time.Now() + + return wait.PollImmediate(poll, timeout, func() (bool, error) { + _, err := framework.RunKubectl(namespace, args...) + if err != nil { + if isRetryableAPIError(err) { + return false, nil + } + if isAlreadyExistsCLIError(err) { + return true, nil + } + e2elog.Logf( + "will run kubectl (%s) again (%d seconds elapsed)", + args, + int(time.Since(start).Seconds())) + + return false, fmt.Errorf("failed to run kubectl: %w", err) + } + + return true, nil + }) +}