From 2d5896f20f4638443d35ec5e48295d794240d5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Ramon=20Ma=C3=B1es?= <32740567+jrmanes@users.noreply.github.com> Date: Wed, 8 May 2024 10:52:19 +0200 Subject: [PATCH] Revert "fix: port forward function channels (#308)" (#326) This reverts commit 6ad89fb4f4c5c4a4b126751f7dbea9a8099250f8. --- pkg/k8s/k8s_pod.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/k8s/k8s_pod.go b/pkg/k8s/k8s_pod.go index bd83d39..39af08c 100644 --- a/pkg/k8s/k8s_pod.go +++ b/pkg/k8s/k8s_pod.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io" "net/http" "path/filepath" "strings" @@ -265,29 +266,27 @@ func (c *Client) PortForwardPod( stopChan := make(chan struct{}, 1) readyChan := make(chan struct{}) - defer func() { - close(stopChan) - close(readyChan) - }() - var stdout, stderr bytes.Buffer + var stdout, stderr io.Writer // Create a new PortForwarder - pf, err := portforward.New(dialer, ports, stopChan, readyChan, &stdout, &stderr) + pf, err := portforward.New(dialer, ports, stopChan, readyChan, stdout, stderr) if err != nil { return ErrCreatingPortForwarder.Wrap(err) } - if stderr.Len() > 0 { - return ErrPortForwarding.WithParams(stderr.String()) + if stderr != nil { + return ErrPortForwarding.WithParams(stderr) } logrus.Debugf("Port forwarding from %d to %d", localPort, remotePort) logrus.Debugf("Port forwarding stdout: %v", stdout) - // Start the port forwarding errChan := make(chan error) - defer close(errChan) + + // Start the port forwarding go func() { if err := pf.ForwardPorts(); err != nil { errChan <- err + } else { + close(errChan) // if there's no error, close the channel } }()