Skip to content

Commit

Permalink
Fix random port usage and add retry handling
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus committed Mar 1, 2019
1 parent 55837bb commit 1bfceb5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
[[constraint]]
name = "gopkg.in/yaml.v2"
version = "v2.2.1"

[[constraint]]
name = "github.com/hashicorp/go-retryablehttp"
version = "v0.5.2"
32 changes: 11 additions & 21 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ package exec
import (
"bufio"
"fmt"
"github.com/helm/chart-testing/pkg/util"
"github.com/pkg/errors"
"io"
"net"
"os"
"os/exec"
"strings"
"sync"

"github.com/helm/chart-testing/pkg/util"
"github.com/pkg/errors"
)

type ProcessExecutor struct {
Expand Down Expand Up @@ -109,34 +106,27 @@ func (p ProcessExecutor) CreateProcess(executable string, execArgs ...interface{
type fn func(port int) error

func (p ProcessExecutor) RunWithProxy(withProxy fn) error {
listener, err := net.Listen("tcp", ":0")
defer listener.Close()
randomPort, err := util.GetRandomPort()
if err != nil {
return errors.Wrap(err, "Could not find a free port to run 'kubectl proxy'")
return errors.Wrap(err, "Could not find a free port for running 'kubectl proxy'")
}

randomPort := listener.Addr().(*net.TCPAddr).Port
fmt.Printf("Running ' kubectl proxy on port %d\n", randomPort)
cmdProxy, err := p.CreateProcess("kubectl", "proxy", "--port", randomPort)
fmt.Printf("Running 'kubectl proxy' on port %d\n", randomPort)
cmdProxy, err := p.CreateProcess("kubectl", "proxy", fmt.Sprintf("--port=%d", randomPort))
if err != nil {
return errors.Wrap(err, "Error creating the 'kubectl proxy' process")
}

var wg sync.WaitGroup
wg.Add(1)
err = cmdProxy.Start()
if err != nil {
return errors.Wrap(err, "Error starting the 'kubectl proxy' process")
}

go func() {
err = cmdProxy.Start()
if err != nil {
fmt.Println("Error starting the 'kubectl proxy' process:", err)
return
}
wg.Wait()
cmdProxy.Wait()
}()

err = withProxy(randomPort)

wg.Done()
cmdProxy.Process.Signal(os.Kill)

if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/tool/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/helm/chart-testing/pkg/exec"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -90,15 +91,17 @@ func (k Kubectl) forceNamespaceDeletion(namespace string) error {
// Remove finalizer from the namespace
fun := func(port int) error {
k8sURL := fmt.Sprintf("http://127.0.0.1:%d/api/v1/namespaces/%s/finalize", port, namespace)
req, err := http.NewRequest("PUT", k8sURL, bytes.NewReader(namespaceUpdateBytes))
req, err := retryablehttp.NewRequest("PUT", k8sURL, bytes.NewReader(namespaceUpdateBytes))
if err != nil {
fmt.Println("Error creating the request to update the namespace:", err)
return err
}
req.Header.Set("Content-Type", "application/json")

errMsg := "Error removing finalizer from namespace"
if resp, err := http.DefaultClient.Do(req); err != nil {
client := retryablehttp.NewClient()
client.Logger = nil
if resp, err := client.Do(req); err != nil {
return errors.Wrap(err, errMsg)
} else if resp.StatusCode != http.StatusOK {
return errors.New(errMsg)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"net"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -203,3 +204,13 @@ func TruncateLeft(s string, maxLength int) string {
}
return s
}

func GetRandomPort() (int , error) {
listener, err := net.Listen("tcp", ":0")
defer listener.Close()
if err != nil {
return 0, err
}

return listener.Addr().(*net.TCPAddr).Port, nil
}

0 comments on commit 1bfceb5

Please sign in to comment.