|
| 1 | +package stream |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + apitypes "github.com/alibaba/pouch/apis/types" |
| 13 | + "github.com/alibaba/pouch/cri/stream/remotecommand" |
| 14 | + "github.com/alibaba/pouch/daemon/mgr" |
| 15 | + |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | +) |
| 18 | + |
| 19 | +// Runtime is the interface to execute the commands and provide the streams. |
| 20 | +type Runtime interface { |
| 21 | + // Exec executes the command in pod. |
| 22 | + Exec(ctx context.Context, containerID string, cmd []string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) |
| 23 | + |
| 24 | + // Attach attaches to pod. |
| 25 | + Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error |
| 26 | + |
| 27 | + // PortForward forward port to pod. |
| 28 | + PortForward(ctx context.Context, name string, port int32, stream io.ReadWriteCloser) error |
| 29 | +} |
| 30 | + |
| 31 | +type streamRuntime struct { |
| 32 | + containerMgr mgr.ContainerMgr |
| 33 | +} |
| 34 | + |
| 35 | +// NewStreamRuntime creates a brand new stream runtime. |
| 36 | +func NewStreamRuntime(ctrMgr mgr.ContainerMgr) Runtime { |
| 37 | + return &streamRuntime{containerMgr: ctrMgr} |
| 38 | +} |
| 39 | + |
| 40 | +// Exec executes a command inside the container. |
| 41 | +func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) { |
| 42 | + createConfig := &apitypes.ExecCreateConfig{ |
| 43 | + Cmd: cmd, |
| 44 | + AttachStdin: streamOpts.Stdin, |
| 45 | + AttachStdout: streamOpts.Stdout, |
| 46 | + AttachStderr: streamOpts.Stderr, |
| 47 | + Tty: streamOpts.TTY, |
| 48 | + } |
| 49 | + |
| 50 | + execid, err := s.containerMgr.CreateExec(ctx, containerID, createConfig) |
| 51 | + if err != nil { |
| 52 | + return 0, fmt.Errorf("failed to create exec for container %q: %v", containerID, err) |
| 53 | + } |
| 54 | + |
| 55 | + attachConfig := &mgr.AttachConfig{ |
| 56 | + Streams: streams, |
| 57 | + MuxDisabled: true, |
| 58 | + } |
| 59 | + |
| 60 | + err = s.containerMgr.StartExec(ctx, execid, attachConfig) |
| 61 | + if err != nil { |
| 62 | + return 0, fmt.Errorf("failed to start exec for container %q: %v", containerID, err) |
| 63 | + } |
| 64 | + |
| 65 | + // TODO Find a better way instead of the dead loop |
| 66 | + var ei *apitypes.ContainerExecInspect |
| 67 | + for { |
| 68 | + ei, err = s.containerMgr.InspectExec(ctx, execid) |
| 69 | + if err != nil { |
| 70 | + return 0, fmt.Errorf("failed to inspect exec for container %q: %v", containerID, err) |
| 71 | + } |
| 72 | + // Loop until exec finished. |
| 73 | + if !ei.Running { |
| 74 | + break |
| 75 | + } |
| 76 | + time.Sleep(100 * time.Millisecond) |
| 77 | + } |
| 78 | + |
| 79 | + return uint32(ei.ExitCode), nil |
| 80 | +} |
| 81 | + |
| 82 | +// Attach attaches to a running container. |
| 83 | +func (s *streamRuntime) Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error { |
| 84 | + attachConfig := &mgr.AttachConfig{ |
| 85 | + Stdin: streamOpts.Stdin, |
| 86 | + Stdout: streamOpts.Stdout, |
| 87 | + Stderr: streamOpts.Stderr, |
| 88 | + Streams: streams, |
| 89 | + } |
| 90 | + |
| 91 | + err := s.containerMgr.Attach(ctx, containerID, attachConfig) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to attach to container %q: %v", containerID, err) |
| 94 | + } |
| 95 | + |
| 96 | + <-streams.StreamCh |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// PortForward forwards ports from a PodSandbox. |
| 102 | +func (s *streamRuntime) PortForward(ctx context.Context, id string, port int32, stream io.ReadWriteCloser) error { |
| 103 | + sandbox, err := s.containerMgr.Get(ctx, id) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to get metadata of sandbox %q: %v", id, err) |
| 106 | + } |
| 107 | + pid := sandbox.State.Pid |
| 108 | + |
| 109 | + socat, err := exec.LookPath("socat") |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to find socat: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + // Check following links for meaning of the options: |
| 115 | + // * socat: https://linux.die.net/man/1/socat |
| 116 | + // * nsenter: http://man7.org/linux/man-pages/man1/nsenter.1.html |
| 117 | + args := []string{"-t", fmt.Sprintf("%d", pid), "-n", socat, |
| 118 | + "-", fmt.Sprintf("TCP4:localhost:%d", port)} |
| 119 | + |
| 120 | + nsenter, err := exec.LookPath("nsenter") |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("failed to find nsenter: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + logrus.Infof("Executing port forwarding command: %s %s", nsenter, strings.Join(args, " ")) |
| 126 | + |
| 127 | + cmd := exec.Command(nsenter, args...) |
| 128 | + cmd.Stdout = stream |
| 129 | + |
| 130 | + stderr := new(bytes.Buffer) |
| 131 | + cmd.Stderr = stderr |
| 132 | + |
| 133 | + // If we use Stdin, command.Run() won't return until the goroutine that's copying |
| 134 | + // from stream finishes. Unfortunately, if you have a client like telnet connected |
| 135 | + // via port forwarding, as long as the user's telnet client is connected to the user's |
| 136 | + // local listener that port forwarding sets up, the telnet session never exits. This |
| 137 | + // means that even if socat has finished running, command.Run() won't ever return |
| 138 | + // (because the client still has the connection and stream open). |
| 139 | + // |
| 140 | + // The work around is to use StdinPipe(), as Wait() (called by Run()) closes the pipe |
| 141 | + // when the command (socat) exits. |
| 142 | + in, err := cmd.StdinPipe() |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("failed to create stdin pipe: %v", err) |
| 145 | + } |
| 146 | + go func() { |
| 147 | + if _, err := io.Copy(in, stream); err != nil { |
| 148 | + logrus.Errorf("failed to copy port forward input for %q port %d: %v", id, port, err) |
| 149 | + } |
| 150 | + in.Close() |
| 151 | + logrus.Infof("finish copy port forward input for %q port %d: %v", id, port, err) |
| 152 | + }() |
| 153 | + |
| 154 | + err = cmd.Run() |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("nsenter command returns error: %v, stderr: %q", err, stderr.String()) |
| 157 | + } |
| 158 | + |
| 159 | + logrus.Infof("finish port forwarding for %q port %d", id, port) |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
0 commit comments