From e78d65c0905d2b81fa0d8088532d828eccea8ac0 Mon Sep 17 00:00:00 2001 From: Francisco Augusto Date: Tue, 3 Oct 2023 15:09:26 +0200 Subject: [PATCH] Improve execute command retries (#298) * Improve execute command retries * Fix kubeconfig condition * Fix ecr login when ecr is in another region * Remove comment * [BUILD] 0.17.0-0.3.0: New pre-release * Prepare for next version: 0.17.0-0.3.0-SNAPSHOT * Prepare for next version: 0.17.0-0.3.1-SNAPSHOT * Improve retry logic --------- Co-authored-by: stratiocommit --- pkg/commons/utils.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/commons/utils.go b/pkg/commons/utils.go index be08bb99f3..04cb585d96 100644 --- a/pkg/commons/utils.go +++ b/pkg/commons/utils.go @@ -19,6 +19,7 @@ package commons import ( "bytes" "context" + "time" "unicode" "os" @@ -27,9 +28,6 @@ import ( "golang.org/x/exp/slices" "gopkg.in/yaml.v3" - "sigs.k8s.io/kind/pkg/cluster/nodes" - "sigs.k8s.io/kind/pkg/errors" - "github.com/Azure/azure-sdk-for-go/sdk/azidentity" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" @@ -37,6 +35,8 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" vault "github.com/sosedoff/ansible-vault-go" + "sigs.k8s.io/kind/pkg/cluster/nodes" + "sigs.k8s.io/kind/pkg/errors" ) const secretName = "secrets.yml" @@ -230,16 +230,25 @@ func removeKey(nodes []*yaml.Node, key string) []*yaml.Node { } func ExecuteCommand(n nodes.Node, command string, envVars ...[]string) (string, error) { - raw := bytes.Buffer{} + var err error + var raw bytes.Buffer cmd := n.Command("sh", "-c", command) if len(envVars) > 0 { cmd.SetEnv(envVars[0]...) } - if err := cmd.SetStdout(&raw).SetStderr(&raw).Run(); err != nil { - return "", err + for i := 0; i < 3; i++ { + raw = bytes.Buffer{} + err = cmd.SetStdout(&raw).SetStderr(&raw).Run() + if err == nil || !strings.Contains(command, "--kubeconfig") || !strings.Contains(raw.String(), "timeout") { + break + } + time.Sleep(5 * time.Second) } if strings.Contains(raw.String(), "Error:") { - return "", errors.New(raw.String()) + return "", errors.Wrap(err, "Command Output: "+raw.String()) + } + if err != nil { + return "", err } return raw.String(), nil }