-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
173 lines (163 loc) · 4.28 KB
/
server.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
package k8s_exec_pod
import (
"context"
"fmt"
"github.com/Shanghai-Lunara/pkg/zaplogger"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"io"
"k8s.io/client-go/kubernetes"
"net/http"
"strconv"
"time"
)
const (
CodeSuccess = iota
CodeError
)
type Server struct {
server *http.Server
ctx context.Context
k8sClient kubernetes.Interface
sessionHub SessionHub
}
func InitServer(ctx context.Context, addr, kubeconfig, masterUrl string) *Server {
cfg, k8sClient := NewResource(masterUrl, kubeconfig)
h := &Server{
k8sClient: k8sClient,
sessionHub: NewSessionHub(k8sClient, cfg),
}
router := gin.New()
router.Use(cors.Default())
router.GET(RouterPodShellToken, h.PodToken)
router.GET(RouterSSH, h.SSH)
router.GET(RouterPodLogStream, h.LogStream)
router.GET(RouterPodLogDownload, h.LogDownload)
h.server = &http.Server{
Addr: addr,
Handler: router,
}
go func() {
if err := h.server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
zaplogger.Sugar().Info("Server closed under request")
} else {
zaplogger.Sugar().Info("Server closed unexpected err:", err)
}
}
}()
return h
}
func (s *Server) ShutDown() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.server.Shutdown(ctx); err != nil {
zaplogger.Sugar().Errorf("http.Server shutdown err:%v", err)
}
}
func (s *Server) PodToken(c *gin.Context) {
option := &ExecOptions{
Namespace: c.Param("namespace"),
PodName: c.Param("pod"),
ContainerName: c.Param("container"),
Follow: true,
Command: []string{c.Param("command")},
}
var res HttpResponse
session, err := s.sessionHub.New(option)
if err != nil {
res.Code = CodeError
res.Message = fmt.Sprintf("Failed to init session err:%s", err.Error())
} else {
res.Code = CodeSuccess
res.Token = session.Id()
}
zaplogger.Sugar().Infof("Namespace:%s PodName:%s ContainerName:%s Command:%v", option.Namespace, option.PodName, option.ContainerName, option.Command)
c.JSON(http.StatusOK, res)
}
func (s *Server) SSH(c *gin.Context) {
token := c.Param("token")
zaplogger.Sugar().Info("SSH token:", token)
proxy, err := NewProxy(context.Background(), c.Writer, c.Request)
if err != nil {
zaplogger.Sugar().Error(err)
return
}
session, err := s.sessionHub.Get(token)
if err != nil {
zaplogger.Sugar().Error(err)
return
}
session.HandleSSH(proxy)
}
func (s *Server) LogStream(c *gin.Context) {
token := c.Param("token")
zaplogger.Sugar().Info("Log token:", token)
proxy, err := NewProxy(context.Background(), c.Writer, c.Request)
if err != nil {
zaplogger.Sugar().Error(err)
return
}
session, err := s.sessionHub.Get(token)
if err != nil {
zaplogger.Sugar().Error(err)
return
}
if err = setOptionWithSince(c, session.Option()); err != nil {
c.Abort()
return
}
go session.HandleLog(proxy)
}
func setOptionWithSince(c *gin.Context, opt *ExecOptions) error {
// check `sinceSeconds` and `sinceTime`
sinceSec, err := strconv.Atoi(c.Param("SinceSeconds"))
if err != nil {
zaplogger.Sugar().Errorw("Convert sinceSeconds failed", "SinceSeconds", c.Param("SinceSeconds"), "err", err)
return err
}
if sinceSec > 0 {
a := int64(sinceSec)
opt.SinceSeconds = &a
opt.SinceTime = nil
} else {
}
return nil
}
func (s *Server) LogDownload(c *gin.Context) {
pre, err := strconv.ParseBool(c.Param("previous"))
if err != nil {
zaplogger.Sugar().Error(err)
c.Abort()
return
}
option := &ExecOptions{
Namespace: c.Param("namespace"),
PodName: c.Param("pod"),
ContainerName: c.Param("container"),
Follow: false,
UsePreviousLogs: pre,
}
if err = setOptionWithSince(c, option); err != nil {
c.Abort()
return
}
reader, err := LogDownload(s.k8sClient, option)
if err != nil {
zaplogger.Sugar().Error(err)
c.Abort()
return
}
defer func() {
zaplogger.Sugar().Info("LogTransmit readCloser close")
if err := reader.Close(); err != nil {
zaplogger.Sugar().Error(err)
}
}()
fileContentDisposition := fmt.Sprintf("attachment;filename=%s_%s_%s.log", c.Param("namespace"), c.Param("pod"), c.Param("container"))
c.Header("Content-Type", "text/plain")
c.Header("Content-Disposition", fileContentDisposition)
if _, err = io.Copy(c.Writer, reader); err != nil {
zaplogger.Sugar().Error(err)
}
}