Getting rid of socat

Signed-off-by: abhi <abhi@docker.com>
This commit is contained in:
abhi 2018-03-23 22:36:39 -07:00
parent aeef99a76e
commit 02b952ec17

View File

@ -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()