Skip to content

Commit

Permalink
Improve execute command retries (kubernetes-sigs#298)
Browse files Browse the repository at this point in the history
* 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 <stratiocommit@stratio.com>
  • Loading branch information
kahun and stratiocommit authored Oct 3, 2023
1 parent 857d4fa commit e78d65c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/commons/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package commons
import (
"bytes"
"context"
"time"
"unicode"

"os"
Expand All @@ -27,16 +28,15 @@ 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"
"github.com/aws/aws-sdk-go-v2/credentials"
"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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e78d65c

Please sign in to comment.