From 02b952ec1754f64b9d21e3f1f606b2ca7aed1303 Mon Sep 17 00:00:00 2001 From: abhi Date: Fri, 23 Mar 2018 22:36:39 -0700 Subject: [PATCH] Getting rid of socat Signed-off-by: abhi --- pkg/server/sandbox_portforward.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/pkg/server/sandbox_portforward.go b/pkg/server/sandbox_portforward.go index da11b73be..b8435b121 100644 --- a/pkg/server/sandbox_portforward.go +++ b/pkg/server/sandbox_portforward.go @@ -17,11 +17,10 @@ limitations under the License. package server import ( - "bytes" "fmt" "io" - "os/exec" - "strings" + "net" + "sync" "github.com/containernetworking/plugins/pkg/ns" "github.com/pkg/errors" @@ -34,7 +33,6 @@ import ( // PortForward prepares a streaming endpoint to forward ports from a PodSandbox, and returns the address. func (c *criService) PortForward(ctx context.Context, r *runtime.PortForwardRequest) (retRes *runtime.PortForwardResponse, retErr error) { - // TODO(random-liu): Run a socat container inside the sandbox to do portforward. sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId()) if err != nil { return nil, errors.Wrapf(err, "failed to find sandbox %q", r.GetPodSandboxId()) @@ -53,34 +51,32 @@ func (c *criService) portForward(id string, port int32, stream io.ReadWriteClose if err != nil { return errors.Wrapf(err, "failed to find sandbox %q in store", id) } - if s.NetNS == nil { - return errors.Errorf("failed to find network namespace fo sandbox %q in store", id) + if s.NetNS == nil || s.NetNS.Closed() { + return errors.Errorf("network namespace for sandbox %q is closed", id) } err = s.NetNS.GetNs().Do(func(_ ns.NetNS) error { var wg sync.WaitGroup client, err := net.Dial("tcp4", fmt.Sprintf("localhost:%d", port)) if err != nil { - return errors.Wrap(err, "failed to dial") + return errors.Wrapf(err, "failed to dial %q", port) } - defer client.Close() - defer stream.Close() - wg.Add(1) go func() { + defer client.Close() if _, err := io.Copy(client, stream); err != nil { - logrus.WithError(err).Errorf("Failed to copy port forward input from %q port %d", id, port) + logrus.WithError(err).Errorf("Failed to copy port forward input for %q port %d", id, port) } - logrus.Infof("Finish copy port forward input for %q port %d: %v", id, port) + logrus.Infof("Finish copy port forward input for %q port %d", id, port) wg.Done() }() wg.Add(1) go func() { + defer stream.Close() if _, err := io.Copy(stream, client); err != nil { logrus.WithError(err).Errorf("Failed to copy port forward output for %q port %d", id, port) } - logrus.Infof("Finish copy port forward output for %q port %d: %v", id, port) - + logrus.Infof("Finish copy port forward output for %q port %d", id, port) wg.Done() }() wg.Wait()