diff --git a/test/pkg/integration/common/port_forward.go b/test/pkg/integration/common/port_forward.go index bab4555fa61005..188551441a51ca 100644 --- a/test/pkg/integration/common/port_forward.go +++ b/test/pkg/integration/common/port_forward.go @@ -5,14 +5,19 @@ package common import ( + "bytes" "context" + "errors" "fmt" + "io" "net" "os/exec" "strings" "time" +) - "golang.org/x/xerrors" +const ( + errorDialingBackend = "error: error upgrading connection: error dialing backend: EOF" ) // ForwardPortOfPod establishes a TCP port forwarding to a Kubernetes pod @@ -43,14 +48,37 @@ func forwardPort(ctx context.Context, kubeconfig string, namespace, resourceType } command := exec.CommandContext(ctx, "kubectl", args...) + var serr, sout bytes.Buffer + command.Stdout = &sout + command.Stderr = &serr err := command.Start() if err != nil { - errchan <- xerrors.Errorf("unexpected error starting port-forward: %w, args: %v", err, args) + if strings.TrimSuffix(serr.String(), "\n") == errorDialingBackend { + errchan <- io.EOF + if command.Process != nil { + _ = command.Process.Kill() + } + } else { + errchan <- fmt.Errorf("unexpected error string port-forward: %w", errors.New(serr.String())) + if command.Process != nil { + _ = command.Process.Kill() + } + } } err = command.Wait() if err != nil { - errchan <- xerrors.Errorf("unexpected error running port-forward: %w, args: %v", err, args) + if strings.TrimSuffix(serr.String(), "\n") == errorDialingBackend { + errchan <- io.EOF + if command.Process != nil { + _ = command.Process.Kill() + } + } else { + errchan <- fmt.Errorf("unexpected error running port-forward: %w", errors.New(serr.String())) + if command.Process != nil { + _ = command.Process.Kill() + } + } } }() @@ -58,12 +86,12 @@ func forwardPort(ctx context.Context, kubeconfig string, namespace, resourceType go func() { localPort := strings.Split(port, ":")[0] for { - conn, _ := net.DialTimeout("tcp", net.JoinHostPort("", localPort), time.Second) + conn, _ := net.DialTimeout("tcp", net.JoinHostPort("localhost", localPort), time.Second) if conn != nil { conn.Close() break } - time.Sleep(500 * time.Millisecond) + time.Sleep(5 * time.Second) } readychan <- struct{}{}