-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler.go
89 lines (80 loc) · 2.07 KB
/
handler.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
package k8ssshpod
import (
"context"
"fmt"
"io"
"github.com/gliderlabs/ssh"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func ssHandler(client kubernetes.Interface, config *rest.Config, audit bool) func(s ssh.Session) {
return func(s ssh.Session) {
defer s.Exit(0)
io.WriteString(s, "Wellcome to use K8S-SSH-POD\n")
t := term.NewTerminal(s, "")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
// select namespace
select_namespace:
lsns := func() ([]string, error) { return listNamespaces(ctx, client, nil) }
ns, exit, err := promptAndSelectNames(s, t, lsns)
if err != nil {
log.Println(err)
_, _ = fmt.Fprintf(s, "[ERROR]: %v", err)
return
}
switch exit {
case label_back:
goto select_namespace
case label_exit:
return
}
// select pod
select_pod:
var getPod = func() ([]string, error) { return listPodWithNamespace(ctx, client, ns, nil) }
podName, exit, err := promptAndSelectNames(s, t, getPod)
if err != nil {
log.Errorln(err)
_, _ = fmt.Fprintf(s, "[ERROR]: %v", err)
return
}
switch exit {
case label_back:
goto select_namespace
case label_exit:
return
}
// select container
var getContainer = func() ([]string, error) { return listContainersForPod(ctx, client, ns, podName) }
containerName, exit, err := promptAndSelectNames(s, t, getContainer)
if err != nil {
log.Errorln(err)
_, _ = fmt.Fprintf(s, "[ERROR]: %v", err)
return
}
switch exit {
case label_back:
goto select_pod
case label_exit:
return
}
_, window, isTty := s.Pty()
if isTty {
podReader, deferHandler := auditLog(s, audit)
// defer deferHandler()
// exec
if err := exec(ctx, client, config, podReader, s, s, ns, podName, containerName, TerminalSizeQueue{window}, isTty, deferHandler); err != nil {
log.Errorln(err)
return
}
} else {
io.WriteString(s, "No PTY requested.\n")
s.Exit(1)
}
log.Debugln("exit ssh handler")
}
}
}