forked from Mirantis/cri-dockerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.go
169 lines (146 loc) · 3.86 KB
/
streaming.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
/*
Copyright 2021 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package streaming
import (
"context"
"fmt"
"io"
"math"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/remotecommand"
dockertypes "github.com/docker/docker/api/types"
"k8s.io/kubernetes/pkg/kubelet/cri/streaming"
"github.com/Mirantis/cri-dockerd/libdocker"
)
type StreamingRuntime struct {
Client libdocker.DockerClientInterface
ExecHandler ExecHandler
}
// ExecHandler knows how to execute a command in a running Docker container.
type ExecHandler interface {
ExecInContainer(
ctx context.Context,
client libdocker.DockerClientInterface,
container *dockertypes.ContainerJSON,
cmd []string,
stdin io.Reader,
stdout, stderr io.WriteCloser,
tty bool,
resize <-chan remotecommand.TerminalSize,
timeout time.Duration,
) error
}
var _ streaming.Runtime = &StreamingRuntime{}
func (r *StreamingRuntime) Exec(
containerID string,
cmd []string,
in io.Reader,
out, err io.WriteCloser,
tty bool,
resize <-chan remotecommand.TerminalSize,
) error {
return r.ExecWithContext(context.TODO(), containerID, cmd, in, out, err, tty, resize, 0)
}
// ExecWithContext adds a context.
func (r *StreamingRuntime) ExecWithContext(
ctx context.Context,
containerID string,
cmd []string,
in io.Reader,
out, errw io.WriteCloser,
tty bool,
resize <-chan remotecommand.TerminalSize,
timeout time.Duration,
) error {
container, err := libdocker.CheckContainerStatus(r.Client, containerID)
if err != nil {
return err
}
return r.ExecHandler.ExecInContainer(
ctx,
r.Client,
container,
cmd,
in,
out,
errw,
tty,
resize,
timeout,
)
}
func (r *StreamingRuntime) Attach(
containerID string,
in io.Reader,
out, errw io.WriteCloser,
tty bool,
resize <-chan remotecommand.TerminalSize,
) error {
_, err := libdocker.CheckContainerStatus(r.Client, containerID)
if err != nil {
return err
}
return attachContainer(r.Client, containerID, in, out, errw, tty, resize)
}
func (r *StreamingRuntime) PortForward(
podSandboxID string,
port int32,
stream io.ReadWriteCloser,
) error {
if port < 0 || port > math.MaxUint16 {
return fmt.Errorf("invalid port %d", port)
}
return r.portForward(podSandboxID, port, stream)
}
func attachContainer(
client libdocker.DockerClientInterface,
containerID string,
stdin io.Reader,
stdout, stderr io.WriteCloser,
tty bool,
resize <-chan remotecommand.TerminalSize,
) error {
// Have to start this before the call to client.AttachToContainer because client.AttachToContainer is a blocking
// call :-( Otherwise, resize events don't get processed and the terminal never resizes.
handleResizing(resize, func(size remotecommand.TerminalSize) {
client.ResizeContainerTTY(containerID, uint(size.Height), uint(size.Width))
})
opts := dockertypes.ContainerAttachOptions{
Stream: true,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: stderr != nil,
}
sopts := libdocker.StreamOptions{
InputStream: stdin,
OutputStream: stdout,
ErrorStream: stderr,
RawTerminal: tty,
}
return client.AttachToContainer(containerID, opts, sopts)
}
func handleResizing(resize <-chan remotecommand.TerminalSize, resizeFunc func(size remotecommand.TerminalSize)) {
if resize == nil {
return
}
go func() {
defer runtime.HandleCrash()
for size := range resize {
if size.Height < 1 || size.Width < 1 {
continue
}
resizeFunc(size)
}
}()
}