forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
191 lines (160 loc) · 5.9 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package stream
import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strings"
"time"
apitypes "github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cri/stream/remotecommand"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/sirupsen/logrus"
)
// Runtime is the interface to execute the commands and provide the streams.
type Runtime interface {
// Exec executes the command in pod.
Exec(ctx context.Context, containerID string, cmd []string, resizeChan <-chan apitypes.ResizeOptions, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error)
// Attach attaches to pod.
Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error
// PortForward forward port to pod.
PortForward(ctx context.Context, name string, port int32, stream io.ReadWriteCloser) error
}
type streamRuntime struct {
containerMgr mgr.ContainerMgr
}
// NewStreamRuntime creates a brand new stream runtime.
func NewStreamRuntime(ctrMgr mgr.ContainerMgr) Runtime {
return &streamRuntime{containerMgr: ctrMgr}
}
// Exec executes a command inside the container.
func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []string, resizeChan <-chan apitypes.ResizeOptions, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) {
createConfig := &apitypes.ExecCreateConfig{
Cmd: cmd,
AttachStdin: streamOpts.Stdin,
AttachStdout: streamOpts.Stdout,
AttachStderr: streamOpts.Stderr,
Tty: streamOpts.TTY,
}
execid, err := s.containerMgr.CreateExec(ctx, containerID, createConfig)
if err != nil {
return 0, fmt.Errorf("failed to create exec for container %q: %v", containerID, err)
}
attachConfig := &mgr.AttachConfig{
Stdin: streamOpts.Stdin,
Streams: streams,
MuxDisabled: true,
}
err = s.containerMgr.StartExec(ctx, execid, attachConfig)
if err != nil {
return 0, fmt.Errorf("failed to start exec for container %q: %v", containerID, err)
}
handleResizing(containerID, execid, resizeChan, func(size apitypes.ResizeOptions) {
err := s.containerMgr.ResizeExec(ctx, execid, size)
if err != nil {
logrus.Errorf("failed to resize process %q console for container %q: %v", execid, containerID, err)
}
})
// TODO Find a better way instead of the dead loop
var ei *apitypes.ContainerExecInspect
for {
ei, err = s.containerMgr.InspectExec(ctx, execid)
if err != nil {
return 0, fmt.Errorf("failed to inspect exec for container %q: %v", containerID, err)
}
// Loop until exec finished.
if !ei.Running {
break
}
time.Sleep(100 * time.Millisecond)
}
return uint32(ei.ExitCode), nil
}
// handleResizing spawns a goroutine that processes the resize channel, calling resizeFunc for each
// remotecommand.TerminalSize received from the channel. The resize channel must be closed elsewhere to stop the
// goroutine.
func handleResizing(containerID, execID string, resizeChan <-chan apitypes.ResizeOptions, resizeFunc func(size apitypes.ResizeOptions)) {
if resizeChan == nil {
return
}
go func() {
for {
size, ok := <-resizeChan
if !ok {
return
}
if size.Height <= 0 || size.Width <= 0 {
continue
}
resizeFunc(size)
}
}()
}
// Attach attaches to a running container.
func (s *streamRuntime) Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error {
attachConfig := &mgr.AttachConfig{
Stdin: streamOpts.Stdin,
Stdout: streamOpts.Stdout,
Stderr: streamOpts.Stderr,
Streams: streams,
}
err := s.containerMgr.Attach(ctx, containerID, attachConfig)
if err != nil {
return fmt.Errorf("failed to attach to container %q: %v", containerID, err)
}
<-streams.StreamCh
return nil
}
// PortForward forwards ports from a PodSandbox.
func (s *streamRuntime) PortForward(ctx context.Context, id string, port int32, stream io.ReadWriteCloser) error {
sandbox, err := s.containerMgr.Get(ctx, id)
if err != nil {
return fmt.Errorf("failed to get metadata of sandbox %q: %v", id, err)
}
pid := sandbox.State.Pid
socat, err := exec.LookPath("socat")
if err != nil {
return fmt.Errorf("failed to find socat: %v", err)
}
// Check following links for meaning of the options:
// * socat: https://linux.die.net/man/1/socat
// * nsenter: http://man7.org/linux/man-pages/man1/nsenter.1.html
args := []string{"-t", fmt.Sprintf("%d", pid), "-n", socat,
"-", fmt.Sprintf("TCP4:localhost:%d", port)}
nsenter, err := exec.LookPath("nsenter")
if err != nil {
return fmt.Errorf("failed to find nsenter: %v", err)
}
logrus.Infof("Executing port forwarding command: %s %s", nsenter, strings.Join(args, " "))
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 fmt.Errorf("failed to create stdin pipe: %v", err)
}
go func() {
if _, err := io.Copy(in, stream); err != nil {
logrus.Errorf("failed to copy port forward input for %q port %d: %v", id, port, err)
}
in.Close()
logrus.Infof("finish copy port forward input for %q port %d: %v", id, port, err)
}()
err = cmd.Run()
if err != nil {
return fmt.Errorf("nsenter command returns error: %v, stderr: %q", err, stderr.String())
}
logrus.Infof("finish port forwarding for %q port %d", id, port)
return nil
}