Skip to content

Commit

Permalink
fix: tiny refactor on port forward (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk authored Jun 19, 2024
1 parent 62f326c commit a00e9d5
Showing 1 changed file with 9 additions and 10 deletions.
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)
)

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):
return ErrPortForwardingTimeout
}

Expand Down

0 comments on commit a00e9d5

Please sign in to comment.