-
Notifications
You must be signed in to change notification settings - Fork 197
/
conn_k8s_container.go
329 lines (294 loc) · 7.3 KB
/
conn_k8s_container.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package srvconn
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"sync"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"github.com/jumpserver/koko/pkg/logger"
)
var (
_ ServerConnection = (*ContainerConnection)(nil)
ErrNotFoundCommand = errors.New("not found command")
ErrNotFoundShell = errors.New("not found any shell")
)
func NewContainerConnection(opts ...ContainerOption) (*ContainerConnection, error) {
var opt ContainerOptions
for _, setter := range opts {
setter(&opt)
}
if opt.win == nil {
opt.win = &remotecommand.TerminalSize{
Width: 80,
Height: 40,
}
}
k8sCfg := opt.K8sCfg()
client, err := kubernetes.NewForConfig(k8sCfg)
if err != nil {
return nil, err
}
shell, err := FindAvailableShell(&opt)
if err != nil {
return nil, err
}
winSizeChan := make(chan *remotecommand.TerminalSize, 10)
done := make(chan struct{})
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()
slaver := slaveStream{
r: stdinReader,
w: stdoutWriter,
winSizeChan: winSizeChan,
done: done,
}
con := ContainerConnection{
opt: opt,
shell: shell,
slaver: &slaver,
winSizeChan: winSizeChan,
done: done,
stdoutReader: stdoutReader,
stdinWriter: stdinWriter,
}
con.winSizeChan <- opt.win
go func() {
if err2 := execContainerShell(client, k8sCfg, &con); err2 != nil {
logger.Error(err2)
}
_ = con.Close()
logger.Infof("K8s %s exec shell exit", con.opt.String())
}()
return &con, nil
}
type ContainerConnection struct {
opt ContainerOptions
shell string
slaver *slaveStream
winSizeChan chan *remotecommand.TerminalSize
stdinWriter io.WriteCloser
stdoutReader io.ReadCloser
done chan struct{}
once sync.Once
}
func (c *ContainerConnection) Read(p []byte) (int, error) {
return c.stdoutReader.Read(p)
}
func (c *ContainerConnection) Write(p []byte) (int, error) {
return c.stdinWriter.Write(p)
}
func (c *ContainerConnection) Close() error {
c.once.Do(func() {
_, _ = c.stdinWriter.Write([]byte("\r\nexit\r\n"))
_ = c.stdinWriter.Close()
_ = c.stdoutReader.Close()
close(c.done)
logger.Infof("K8s %s connection close", c.opt.String())
})
return nil
}
func (c *ContainerConnection) KeepAlive() error {
return nil
}
func (c *ContainerConnection) SetWinSize(w, h int) error {
size := &remotecommand.TerminalSize{
Width: uint16(w),
Height: uint16(h),
}
select {
case c.winSizeChan <- size:
case <-c.done:
return nil
}
return nil
}
type ContainerOption func(*ContainerOptions)
type ContainerOptions struct {
Host string
Token string
PodName string
Namespace string
ContainerName string
IsSkipTls bool
win *remotecommand.TerminalSize
}
func (o ContainerOptions) K8sCfg() *rest.Config {
kubeConf := &rest.Config{
Host: o.Host,
BearerToken: o.Token,
}
if o.IsSkipTls {
kubeConf.Insecure = true
}
return kubeConf
}
func (o ContainerOptions) String() string {
return fmt.Sprintf("(%s)-(%s)-(%s)", o.Namespace,
o.PodName, o.ContainerName)
}
func ContainerHost(host string) ContainerOption {
return func(opt *ContainerOptions) {
opt.Host = host
}
}
func ContainerToken(token string) ContainerOption {
return func(opt *ContainerOptions) {
opt.Token = token
}
}
func ContainerPodName(name string) ContainerOption {
return func(opt *ContainerOptions) {
opt.PodName = name
}
}
func ContainerNamespace(namespace string) ContainerOption {
return func(opt *ContainerOptions) {
opt.Namespace = namespace
}
}
func ContainerName(container string) ContainerOption {
return func(opt *ContainerOptions) {
opt.ContainerName = container
}
}
func ContainerSkipTls(isSkipTls bool) ContainerOption {
return func(args *ContainerOptions) {
args.IsSkipTls = isSkipTls
}
}
func ContainerPtyWin(win Windows) ContainerOption {
return func(args *ContainerOptions) {
args.win = &remotecommand.TerminalSize{
Width: uint16(win.Width),
Height: uint16(win.Height),
}
}
}
func FindAvailableShell(opt *ContainerOptions) (shell string, err error) {
shells := []string{"bash", "sh", "powershell", "cmd"}
for i := range shells {
if err = HasShellInContainer(opt, shells[i]); err == nil {
return shells[i], nil
} else {
logger.Debug(err)
}
}
return "", ErrNotFoundShell
}
func HasShellInContainer(opt *ContainerOptions, shell string) error {
container := opt.ContainerName
podName := opt.PodName
namespace := opt.Namespace
kubeConf := opt.K8sCfg()
client, err := kubernetes.NewForConfig(kubeConf)
if err != nil {
return err
}
testScript := fmt.Sprintf(scriptTmpl, shell)
command := []string{"sh", "-c", testScript}
validateChecker := func(result string) error {
if !strings.HasSuffix(result, shell) {
return fmt.Errorf("%w: %s %s", ErrNotFoundCommand, result, shell)
}
return nil
}
switch shell {
case "cmd":
command = []string{"where", "cmd"}
validateChecker = func(result string) error {
if !strings.HasSuffix(result, "cmd.exe") {
return fmt.Errorf("%w: %s cmd.exe", ErrNotFoundCommand, result)
}
return nil
}
case "powershell":
command = []string{"Get-Command", "powershell"}
validateChecker = func(result string) error {
if !strings.Contains(result, "powershell.exe") {
return fmt.Errorf("%w: %s powershell.exe", ErrNotFoundCommand, result)
}
return nil
}
}
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: container,
Command: command,
Stdout: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(kubeConf, "POST", req.URL())
if err != nil {
return err
}
var buf bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdout: &buf,
Tty: false,
})
if err != nil {
return err
}
result := strings.TrimSpace(buf.String())
buf.Reset()
return validateChecker(result)
}
type slaveStream struct {
r io.ReadCloser
w io.WriteCloser
winSizeChan chan *remotecommand.TerminalSize
done chan struct{}
}
func (s *slaveStream) Read(p []byte) (int, error) {
return s.r.Read(p)
}
func (s *slaveStream) Write(p []byte) (int, error) {
return s.w.Write(p)
}
func (s *slaveStream) Next() *remotecommand.TerminalSize {
select {
case size := <-s.winSizeChan:
return size
case <-s.done:
return nil
}
}
func execContainerShell(k8sClient *kubernetes.Clientset, k8sCfg *rest.Config, c *ContainerConnection) error {
req := k8sClient.CoreV1().RESTClient().Post().
Resource("pods").
Name(c.opt.PodName).
Namespace(c.opt.Namespace).
SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: c.opt.ContainerName,
Command: []string{c.shell},
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(k8sCfg, "POST", req.URL())
if err != nil {
return err
}
streamOption := remotecommand.StreamOptions{
Stdin: c.slaver,
Stdout: c.slaver,
Stderr: c.slaver,
TerminalSizeQueue: c.slaver,
Tty: true,
}
// 这个 stream 是阻塞的方法
err = exec.Stream(streamOption)
return err
}
var scriptTmpl = `#!/bin/sh
command -v %s`