Skip to content
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

fix: tiny refactor on port forward #417

Merged
merged 2 commits into from
Jun 19, 2024
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
19 changes: 9 additions & 10 deletions pkg/k8s/k8s_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -263,26 +262,26 @@ func (c *Client) PortForwardPod(
}

dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", url)

ports := []string{fmt.Sprintf("%d:%d", localPort, remotePort)}

stopChan := make(chan struct{}, 1)
readyChan := make(chan struct{})
var (
stopChan = make(chan struct{}, 1)
readyChan = make(chan struct{})
errChan = make(chan error)
)
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved

var stdout, stderr io.Writer
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
// Create a new PortForwarder
pf, err := portforward.New(dialer, ports, stopChan, readyChan, stdout, stderr)
if err != nil {
return ErrCreatingPortForwarder.Wrap(err)
}
if stderr != nil {
return ErrPortForwarding.WithParams(stderr)
if stderr.Len() > 0 {
return ErrPortForwarding.WithParams(stderr.String())
}
logrus.Debugf("Port forwarding from %d to %d", localPort, remotePort)
logrus.Debugf("Port forwarding stdout: %v", stdout)

errChan := make(chan error)

// Start the port forwarding
go func() {
if err := pf.ForwardPorts(); err != nil {
Expand All @@ -300,7 +299,7 @@ func (c *Client) PortForwardPod(
case err := <-errChan:
// if there's an error, return it
return ErrForwardingPorts.Wrap(err)
case <-time.After(time.Second * 5):
case <-time.After(waitRetry * 2):
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
return ErrPortForwardingTimeout
}

Expand Down
Loading