-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathrouter.go
292 lines (254 loc) · 11.9 KB
/
router.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
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"strings"
"sync/atomic"
"time"
serverTypes "github.com/alibaba/pouch/apis/server/types"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/httputils"
"github.com/alibaba/pouch/pkg/log"
"github.com/alibaba/pouch/pkg/randomid"
"github.com/alibaba/pouch/pkg/utils"
"github.com/gorilla/mux"
)
// versionMatcher defines to parse version url path.
const versionMatcher = "/v{version:[0-9.]+}"
func initRoute(s *Server) *mux.Router {
r := mux.NewRouter()
handlers := []*serverTypes.HandlerSpec{
// system
{Method: http.MethodGet, Path: "/_ping", HandlerFunc: s.ping},
{Method: http.MethodGet, Path: "/info", HandlerFunc: s.info},
{Method: http.MethodGet, Path: "/version", HandlerFunc: s.version},
{Method: http.MethodPost, Path: "/auth", HandlerFunc: s.auth},
{Method: http.MethodGet, Path: "/events", HandlerFunc: withCancelHandler(s.events)},
// daemon, we still list this API into system manager.
{Method: http.MethodPost, Path: "/daemon/update", HandlerFunc: s.updateDaemon},
// container
{Method: http.MethodPost, Path: "/containers/{name:.*}/checkpoints", HandlerFunc: withCancelHandler(s.createContainerCheckpoint)},
{Method: http.MethodGet, Path: "/containers/{name:.*}/checkpoints", HandlerFunc: withCancelHandler(s.listContainerCheckpoint)},
{Method: http.MethodDelete, Path: "/containers/{name}/checkpoints/{id}", HandlerFunc: withCancelHandler(s.deleteContainerCheckpoint)},
{Method: http.MethodPost, Path: "/containers/create", HandlerFunc: s.createContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/start", HandlerFunc: s.startContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/stop", HandlerFunc: s.stopContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/kill", HandlerFunc: s.killContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/attach", HandlerFunc: s.attachContainer},
{Method: http.MethodGet, Path: "/containers/json", HandlerFunc: s.getContainers},
{Method: http.MethodGet, Path: "/containers/{name:.*}/json", HandlerFunc: s.getContainer},
{Method: http.MethodDelete, Path: "/containers/{name:.*}", HandlerFunc: s.removeContainers},
{Method: http.MethodPost, Path: "/containers/{name:.*}/exec", HandlerFunc: s.createContainerExec},
{Method: http.MethodGet, Path: "/exec/{name:.*}/json", HandlerFunc: s.getExecInfo},
{Method: http.MethodPost, Path: "/exec/{name:.*}/start", HandlerFunc: s.startContainerExec},
{Method: http.MethodPost, Path: "/exec/{name:.*}/resize", HandlerFunc: s.resizeExec},
{Method: http.MethodPost, Path: "/containers/{name:.*}/rename", HandlerFunc: s.renameContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/restart", HandlerFunc: s.restartContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/pause", HandlerFunc: s.pauseContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/unpause", HandlerFunc: s.unpauseContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/update", HandlerFunc: s.updateContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/upgrade", HandlerFunc: s.upgradeContainer},
{Method: http.MethodGet, Path: "/containers/{name:.*}/top", HandlerFunc: s.topContainer},
{Method: http.MethodGet, Path: "/containers/{name:.*}/logs", HandlerFunc: withCancelHandler(s.logsContainer)},
{Method: http.MethodGet, Path: "/containers/{name:.*}/stats", HandlerFunc: withCancelHandler(s.statsContainer)},
{Method: http.MethodPost, Path: "/containers/{name:.*}/resize", HandlerFunc: s.resizeContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/restart", HandlerFunc: s.restartContainer},
{Method: http.MethodPost, Path: "/containers/{name:.*}/wait", HandlerFunc: withCancelHandler(s.waitContainer)},
{Method: http.MethodPost, Path: "/commit", HandlerFunc: withCancelHandler(s.commitContainer)},
// image
{Method: http.MethodPost, Path: "/images/create", HandlerFunc: withCancelHandler(s.pullImage)},
{Method: http.MethodPost, Path: "/images/search", HandlerFunc: s.searchImages},
{Method: http.MethodGet, Path: "/images/json", HandlerFunc: s.listImages},
{Method: http.MethodDelete, Path: "/images/{name:.*}", HandlerFunc: s.removeImage},
{Method: http.MethodGet, Path: "/images/{name:.*}/json", HandlerFunc: s.getImage},
{Method: http.MethodPost, Path: "/images/{name:.*}/tag", HandlerFunc: s.postImageTag},
{Method: http.MethodPost, Path: "/images/load", HandlerFunc: withCancelHandler(s.loadImage)},
{Method: http.MethodGet, Path: "/images/save", HandlerFunc: withCancelHandler(s.saveImage)},
{Method: http.MethodGet, Path: "/images/{name:.*}/history", HandlerFunc: s.getImageHistory},
{Method: http.MethodPost, Path: "/images/{name:.*}/push", HandlerFunc: s.pushImage},
// volume
{Method: http.MethodGet, Path: "/volumes", HandlerFunc: s.listVolume},
{Method: http.MethodPost, Path: "/volumes/create", HandlerFunc: s.createVolume},
{Method: http.MethodGet, Path: "/volumes/{name:.*}", HandlerFunc: s.getVolume},
{Method: http.MethodDelete, Path: "/volumes/{name:.*}", HandlerFunc: s.removeVolume},
// network
{Method: http.MethodGet, Path: "/networks", HandlerFunc: s.listNetwork},
{Method: http.MethodPost, Path: "/networks/create", HandlerFunc: s.createNetwork},
{Method: http.MethodGet, Path: "/networks/{id:.*}", HandlerFunc: s.getNetwork},
{Method: http.MethodDelete, Path: "/networks/{id:.*}", HandlerFunc: s.deleteNetwork},
{Method: http.MethodPost, Path: "/networks/{id:.*}/connect", HandlerFunc: s.connectToNetwork},
{Method: http.MethodPost, Path: "/networks/{id:.*}/disconnect", HandlerFunc: s.disconnectNetwork},
// metrics
{Method: http.MethodGet, Path: "/metrics", HandlerFunc: s.metrics},
// cri stream
{Method: http.MethodGet, Path: "/exec/{token}", HandlerFunc: s.criExec},
{Method: http.MethodPost, Path: "/exec/{token}", HandlerFunc: s.criExec},
{Method: http.MethodGet, Path: "/attach/{token}", HandlerFunc: s.criAttach},
{Method: http.MethodPost, Path: "/attach/{token}", HandlerFunc: s.criAttach},
{Method: http.MethodGet, Path: "/portforward/{token}", HandlerFunc: s.criPortForward},
{Method: http.MethodPost, Path: "/portforward/{token}", HandlerFunc: s.criPortForward},
// copy
{Method: http.MethodPut, Path: "/containers/{name:.*}/archive", HandlerFunc: s.putContainersArchive},
{Method: http.MethodHead, Path: "/containers/{name:.*}/archive", HandlerFunc: s.headContainersArchive},
{Method: http.MethodGet, Path: "/containers/{name:.*}/archive", HandlerFunc: s.getContainersArchive},
}
if s.APIPlugin != nil {
handlers = s.APIPlugin.UpdateHandler(context.Background(), handlers)
}
// register API
for _, h := range handlers {
if h != nil {
r.Path(versionMatcher + h.Path).Methods(h.Method).Handler(filter(h.HandlerFunc, s))
r.Path(h.Path).Methods(h.Method).Handler(filter(h.HandlerFunc, s))
}
}
if s.Config.Debug || s.Config.EnableProfiler {
profilerSetup(r)
}
return r
}
func profilerSetup(mainRouter *mux.Router) {
var r = mainRouter.PathPrefix("/debug/").Subrouter()
r.HandleFunc("/pprof/", pprof.Index)
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/pprof/profile", pprof.Profile)
r.HandleFunc("/pprof/symbol", pprof.Symbol)
r.HandleFunc("/pprof/trace", pprof.Trace)
r.HandleFunc("/pprof/block", pprof.Handler("block").ServeHTTP)
r.HandleFunc("/pprof/heap", pprof.Handler("heap").ServeHTTP)
r.HandleFunc("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
r.HandleFunc("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
}
// withCancelHandler will use context to cancel the handler. Otherwise, if the
// the connection has been cut by the client or firewall, the server handler
// will hang and cause goroutine leak.
func withCancelHandler(h serverTypes.Handler) serverTypes.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
notifier, ok := rw.(http.CloseNotifier)
if !ok {
return h(ctx, rw, req)
}
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
waitCh := make(chan struct{})
defer close(waitCh)
// NOTE: in order to avoid the race , we should get the
// channel before select.
//
// Related issue: https://github.com/grpc-ecosystem/grpc-gateway/pull/120.
closeNotify := notifier.CloseNotify()
go func() {
select {
case <-closeNotify:
cancel()
case <-waitCh:
}
}()
return h(ctx, rw, req)
}
}
func filter(handler serverTypes.Handler, s *Server) http.HandlerFunc {
pctx := context.Background()
return func(w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(pctx)
defer cancel()
ctx = log.NewContext(ctx, map[string]interface{}{
"RequestID": randomid.Generate()[:10],
})
if flyingReqDecider(req) {
atomic.AddInt32(&s.FlyingReq, 1)
defer atomic.AddInt32(&s.FlyingReq, -1)
}
s.lock.RLock()
if len(s.ManagerWhiteList) > 0 && req.TLS != nil && len(req.TLS.PeerCertificates) > 0 {
if _, isManager := s.ManagerWhiteList[req.TLS.PeerCertificates[0].Subject.CommonName]; !isManager {
s.lock.RUnlock()
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("tls verified error."))
return
}
}
s.lock.RUnlock()
t := time.Now()
clientInfo := req.RemoteAddr
defer func() {
d := time.Since(t) / (time.Millisecond)
if req.Method != http.MethodGet {
log.With(ctx).Infof("End of Calling %s %s, costs %d ms. client %s", req.Method, req.URL.Path, d, clientInfo)
} else {
log.With(ctx).Debugf("End of Calling %s %s, costs %d ms. client %s", req.Method, req.URL.Path, d, clientInfo)
}
}()
if req.TLS != nil && len(req.TLS.PeerCertificates) > 0 {
issuer := req.TLS.PeerCertificates[0].Issuer.CommonName
clientName := req.TLS.PeerCertificates[0].Subject.CommonName
ctx = utils.SetTLSIssuer(ctx, issuer)
ctx = utils.SetTLSCommonName(ctx, clientName)
clientInfo = fmt.Sprintf("%s %s %s", clientInfo, issuer, clientName)
}
if req.Method != http.MethodGet {
log.With(ctx).Infof("Calling %s %s, client %s", req.Method, req.URL.RequestURI(), clientInfo)
} else {
log.With(ctx).Debugf("Calling %s %s, client %s", req.Method, req.URL.RequestURI(), clientInfo)
}
// Start to handle request.
err := handler(ctx, w, req)
if err == nil {
return
}
// Handle error if request handling fails.
log.With(ctx).Errorf("Handler for %s %s, client %s returns error: %s", req.Method, req.URL.RequestURI(), clientInfo, err)
HandleErrorResponse(w, err)
}
}
var routeGroupToWait = []string{"/containers/", "/volumes/", "/networks/"}
func flyingReqDecider(req *http.Request) bool {
for _, r := range routeGroupToWait {
if strings.Contains(req.URL.Path, r) {
return true
}
}
return false
}
// EncodeResponse encodes response in json.
func EncodeResponse(rw http.ResponseWriter, statusCode int, data interface{}) error {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(statusCode)
return json.NewEncoder(rw).Encode(data)
}
// HandleErrorResponse handles err from daemon side and constructs response for client side.
func HandleErrorResponse(w http.ResponseWriter, err error) {
var (
code int
errMsg string
)
// By default, daemon side returns code 500 if error happens.
code = http.StatusInternalServerError
errMsg = err.Error()
httpErr, ok := err.(httputils.HTTPError)
if ok {
code = httpErr.Code()
} else if errtypes.IsNotfound(err) {
code = http.StatusNotFound
} else if errtypes.IsInvalidParam(err) {
code = http.StatusBadRequest
} else if errtypes.IsAlreadyExisted(err) {
code = http.StatusConflict
} else if errtypes.IsNotModified(err) {
code = http.StatusNotModified
} else if errtypes.IsInvalidAuthorization(err) {
code = http.StatusForbidden
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
resp := types.Error{
Message: errMsg,
}
enc.Encode(resp)
}