Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 3d0706c

Browse files
authored
Merge pull request #691 from abhi/socat
Getting rid of nsenter and socat
2 parents 3040454 + c200cb4 commit 3d0706c

File tree

6 files changed

+46
-69
lines changed

6 files changed

+46
-69
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ install:
2121
- sudo apt-get install btrfs-tools
2222
- sudo apt-get install libseccomp2/trusty-backports
2323
- sudo apt-get install libseccomp-dev/trusty-backports
24-
- sudo apt-get install socat
25-
- docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
2624

2725
before_script:
2826
- export PATH=$HOME/gopath/bin:$PATH

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ specifications as appropriate.
6767
(Fedora, CentOS, RHEL). On releases of Ubuntu <=Trusty and Debian <=jessie a
6868
backport version of `libseccomp-dev` is required. See [travis.yml](.travis.yml) for an example on trusty.
6969
* **btrfs development library.** Required by containerd btrfs support. `btrfs-tools`(Ubuntu, Debian) / `btrfs-progs-devel`(Fedora, CentOS, RHEL)
70-
2. Install other dependencies:
71-
* **`nsenter`**: Required by portforward.
72-
* **`socat`**: Required by portforward.
73-
3. Install and setup a go 1.10 development environment.
74-
4. Make a local clone of this repository.
75-
5. Install binary dependencies by running the following command from your cloned `cri/` project directory:
70+
2. Install and setup a go 1.10 development environment.
71+
3. Make a local clone of this repository.
72+
4. Install binary dependencies by running the following command from your cloned `cri/` project directory:
7673
```bash
7774
# Note: install.deps installs the above mentioned runc, containerd, and CNI
7875
# binary dependencies. install.deps is only provided for general use and ease of

contrib/ansible/tasks/bootstrap_centos.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
- btrfs-progs
1010
- libseccomp
1111
- util-linux
12-
- socat
1312
- libselinux-python

contrib/ansible/tasks/bootstrap_ubuntu.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99
- apt-transport-https
1010
- btrfs-tools
1111
- libseccomp2
12-
- socat
1312
- util-linux
1413
# TODO: Limited support for trusty for nsenter. Need to handle/verify

pkg/server/sandbox_portforward.go

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@ limitations under the License.
1717
package server
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"io"
23-
"os/exec"
24-
"strings"
22+
"net"
23+
"sync"
2524

25+
"github.com/containernetworking/plugins/pkg/ns"
2626
"github.com/pkg/errors"
2727
"github.com/sirupsen/logrus"
2828
"golang.org/x/net/context"
2929
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
3030

31-
ctrdutil "github.com/containerd/cri/pkg/containerd/util"
3231
sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
3332
)
3433

3534
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox, and returns the address.
3635
func (c *criService) PortForward(ctx context.Context, r *runtime.PortForwardRequest) (retRes *runtime.PortForwardResponse, retErr error) {
37-
// TODO(random-liu): Run a socat container inside the sandbox to do portforward.
3836
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
3937
if err != nil {
4038
return nil, errors.Wrapf(err, "failed to find sandbox %q", r.GetPodSandboxId())
@@ -46,69 +44,48 @@ func (c *criService) PortForward(ctx context.Context, r *runtime.PortForwardRequ
4644
return c.streamServer.GetPortForward(r)
4745
}
4846

49-
// portForward requires `nsenter` and `socat` on the node, it uses `nsenter` to enter the
50-
// sandbox namespace, and run `socat` inside the namespace to forward stream for a specific
51-
// port. The `socat` command keeps running until it exits or client disconnect.
47+
// portForward requires it uses netns to enter the sandbox namespace,
48+
// and forward stream for a specific port.
5249
func (c *criService) portForward(id string, port int32, stream io.ReadWriteCloser) error {
5350
s, err := c.sandboxStore.Get(id)
5451
if err != nil {
5552
return errors.Wrapf(err, "failed to find sandbox %q in store", id)
5653
}
57-
t, err := s.Container.Task(ctrdutil.NamespacedContext(), nil)
58-
if err != nil {
59-
return errors.Wrap(err, "failed to get sandbox container task")
60-
}
61-
pid := t.Pid()
62-
63-
socat, err := exec.LookPath("socat")
64-
if err != nil {
65-
return errors.Wrap(err, "failed to find socat")
66-
}
67-
68-
// Check following links for meaning of the options:
69-
// * socat: https://linux.die.net/man/1/socat
70-
// * nsenter: http://man7.org/linux/man-pages/man1/nsenter.1.html
71-
args := []string{"-t", fmt.Sprintf("%d", pid), "-n", socat,
72-
"-", fmt.Sprintf("TCP4:localhost:%d", port)}
73-
74-
nsenter, err := exec.LookPath("nsenter")
75-
if err != nil {
76-
return errors.Wrap(err, "failed to find nsenter")
54+
if s.NetNS == nil || s.NetNS.Closed() {
55+
return errors.Errorf("network namespace for sandbox %q is closed", id)
7756
}
7857

79-
logrus.Infof("Executing port forwarding command: %s %s", nsenter, strings.Join(args, " "))
80-
81-
cmd := exec.Command(nsenter, args...)
82-
cmd.Stdout = stream
83-
84-
stderr := new(bytes.Buffer)
85-
cmd.Stderr = stderr
86-
87-
// If we use Stdin, command.Run() won't return until the goroutine that's copying
88-
// from stream finishes. Unfortunately, if you have a client like telnet connected
89-
// via port forwarding, as long as the user's telnet client is connected to the user's
90-
// local listener that port forwarding sets up, the telnet session never exits. This
91-
// means that even if socat has finished running, command.Run() won't ever return
92-
// (because the client still has the connection and stream open).
93-
//
94-
// The work around is to use StdinPipe(), as Wait() (called by Run()) closes the pipe
95-
// when the command (socat) exits.
96-
in, err := cmd.StdinPipe()
97-
if err != nil {
98-
return errors.Wrap(err, "failed to create stdin pipe")
99-
}
100-
go func() {
101-
if _, err := io.Copy(in, stream); err != nil {
102-
logrus.WithError(err).Errorf("Failed to copy port forward input for %q port %d", id, port)
58+
err = s.NetNS.GetNs().Do(func(_ ns.NetNS) error {
59+
var wg sync.WaitGroup
60+
client, err := net.Dial("tcp4", fmt.Sprintf("localhost:%d", port))
61+
if err != nil {
62+
return errors.Wrapf(err, "failed to dial %q", port)
10363
}
104-
in.Close()
105-
logrus.Debugf("Finish copy port forward input for %q port %d", id, port)
106-
}()
107-
108-
if err := cmd.Run(); err != nil {
109-
return errors.Errorf("nsenter command returns error: %v, stderr: %q", err, stderr.String())
64+
wg.Add(1)
65+
go func() {
66+
defer client.Close()
67+
if _, err := io.Copy(client, stream); err != nil {
68+
logrus.WithError(err).Errorf("Failed to copy port forward input for %q port %d", id, port)
69+
}
70+
logrus.Infof("Finish copy port forward input for %q port %d", id, port)
71+
wg.Done()
72+
}()
73+
wg.Add(1)
74+
go func() {
75+
defer stream.Close()
76+
if _, err := io.Copy(stream, client); err != nil {
77+
logrus.WithError(err).Errorf("Failed to copy port forward output for %q port %d", id, port)
78+
}
79+
logrus.Infof("Finish copy port forward output for %q port %d", id, port)
80+
wg.Done()
81+
}()
82+
wg.Wait()
83+
84+
return nil
85+
})
86+
if err != nil {
87+
return errors.Wrapf(err, "failed to execute portforward in network namespace %s", s.NetNS.GetPath())
11088
}
111-
11289
logrus.Infof("Finish port forwarding for %q port %d", id, port)
11390

11491
return nil

pkg/store/sandbox/netns.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,10 @@ func (n *NetNS) GetPath() string {
117117
defer n.Unlock()
118118
return n.ns.Path()
119119
}
120+
121+
// GetNs returns the network namespace handle
122+
func (n *NetNS) GetNs() cnins.NetNS {
123+
n.Lock()
124+
defer n.Unlock()
125+
return n.ns
126+
}

0 commit comments

Comments
 (0)