Using netns to perform socat
This commit removes the usage of nsenter and uses netns to perform socat operation. Signed-off-by: abhi <abhi@docker.com>
This commit is contained in:
parent
c0f483adcf
commit
aeef99a76e
@ -23,12 +23,12 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
||||||
|
|
||||||
ctrdutil "github.com/containerd/cri/pkg/containerd/util"
|
|
||||||
sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
|
sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,69 +46,50 @@ func (c *criService) PortForward(ctx context.Context, r *runtime.PortForwardRequ
|
|||||||
return c.streamServer.GetPortForward(r)
|
return c.streamServer.GetPortForward(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// portForward requires `nsenter` and `socat` on the node, it uses `nsenter` to enter the
|
// portForward requires it uses netns to enter the sandbox namespace,
|
||||||
// sandbox namespace, and run `socat` inside the namespace to forward stream for a specific
|
// and forward stream for a specific port.
|
||||||
// port. The `socat` command keeps running until it exits or client disconnect.
|
|
||||||
func (c *criService) portForward(id string, port int32, stream io.ReadWriteCloser) error {
|
func (c *criService) portForward(id string, port int32, stream io.ReadWriteCloser) error {
|
||||||
s, err := c.sandboxStore.Get(id)
|
s, err := c.sandboxStore.Get(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to find sandbox %q in store", id)
|
return errors.Wrapf(err, "failed to find sandbox %q in store", id)
|
||||||
}
|
}
|
||||||
t, err := s.Container.Task(ctrdutil.NamespacedContext(), nil)
|
if s.NetNS == nil {
|
||||||
if err != nil {
|
return errors.Errorf("failed to find network namespace fo sandbox %q in store", id)
|
||||||
return errors.Wrap(err, "failed to get sandbox container task")
|
|
||||||
}
|
|
||||||
pid := t.Pid()
|
|
||||||
|
|
||||||
socat, err := exec.LookPath("socat")
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "failed to find socat")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check following links for meaning of the options:
|
err = s.NetNS.GetNs().Do(func(_ ns.NetNS) error {
|
||||||
// * socat: https://linux.die.net/man/1/socat
|
var wg sync.WaitGroup
|
||||||
// * nsenter: http://man7.org/linux/man-pages/man1/nsenter.1.html
|
client, err := net.Dial("tcp4", fmt.Sprintf("localhost:%d", port))
|
||||||
args := []string{"-t", fmt.Sprintf("%d", pid), "-n", socat,
|
|
||||||
"-", fmt.Sprintf("TCP4:localhost:%d", port)}
|
|
||||||
|
|
||||||
nsenter, err := exec.LookPath("nsenter")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to find nsenter")
|
return errors.Wrap(err, "failed to dial")
|
||||||
}
|
}
|
||||||
|
defer client.Close()
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
logrus.Infof("Executing port forwarding command: %s %s", nsenter, strings.Join(args, " "))
|
wg.Add(1)
|
||||||
|
|
||||||
cmd := exec.Command(nsenter, args...)
|
|
||||||
cmd.Stdout = stream
|
|
||||||
|
|
||||||
stderr := new(bytes.Buffer)
|
|
||||||
cmd.Stderr = stderr
|
|
||||||
|
|
||||||
// If we use Stdin, command.Run() won't return until the goroutine that's copying
|
|
||||||
// from stream finishes. Unfortunately, if you have a client like telnet connected
|
|
||||||
// via port forwarding, as long as the user's telnet client is connected to the user's
|
|
||||||
// local listener that port forwarding sets up, the telnet session never exits. This
|
|
||||||
// means that even if socat has finished running, command.Run() won't ever return
|
|
||||||
// (because the client still has the connection and stream open).
|
|
||||||
//
|
|
||||||
// The work around is to use StdinPipe(), as Wait() (called by Run()) closes the pipe
|
|
||||||
// when the command (socat) exits.
|
|
||||||
in, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "failed to create stdin pipe")
|
|
||||||
}
|
|
||||||
go func() {
|
go func() {
|
||||||
if _, err := io.Copy(in, stream); err != nil {
|
if _, err := io.Copy(client, stream); err != nil {
|
||||||
logrus.WithError(err).Errorf("Failed to copy port forward input for %q port %d", id, port)
|
logrus.WithError(err).Errorf("Failed to copy port forward input from %q port %d", id, port)
|
||||||
}
|
}
|
||||||
in.Close()
|
logrus.Infof("Finish copy port forward input for %q port %d: %v", id, port)
|
||||||
logrus.Debugf("Finish copy port forward input for %q port %d", id, port)
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
wg.Add(1)
|
||||||
if err := cmd.Run(); err != nil {
|
go func() {
|
||||||
return errors.Errorf("nsenter command returns error: %v, stderr: %q", err, stderr.String())
|
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)
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to execute portforward in network namespace %s", s.NetNS.GetPath())
|
||||||
|
}
|
||||||
logrus.Infof("Finish port forwarding for %q port %d", id, port)
|
logrus.Infof("Finish port forwarding for %q port %d", id, port)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -117,3 +117,10 @@ func (n *NetNS) GetPath() string {
|
|||||||
defer n.Unlock()
|
defer n.Unlock()
|
||||||
return n.ns.Path()
|
return n.ns.Path()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNs returns the network namespace handle
|
||||||
|
func (n *NetNS) GetNs() cnins.NetNS {
|
||||||
|
n.Lock()
|
||||||
|
defer n.Unlock()
|
||||||
|
return n.ns
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user