Skip to content

test: Convert port-forward stderr to be easier to deal with #12951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions test/pkg/integration/common/port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,27 +48,50 @@ 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()
}
}
}
}()

// wait until we can reach the local port before signaling we are ready
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{}{}
Expand Down