forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
276 lines (234 loc) · 7.21 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
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
package stream
import (
"io"
"net/http"
"net/url"
"path"
"time"
"github.com/alibaba/pouch/cri/stream/constant"
"github.com/alibaba/pouch/cri/stream/portforward"
"github.com/alibaba/pouch/cri/stream/remotecommand"
"github.com/gorilla/mux"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
// Keep these constants consistent with the peers in official package:
// k8s.io/kubernetes/pkg/kubelet/server.
const (
// DefaultStreamIdleTimeout is the timeout for idle stream.
DefaultStreamIdleTimeout = 4 * time.Hour
// DefaultStreamCreationTimeout is the timeout for stream creation.
DefaultStreamCreationTimeout = 30 * time.Second
)
// TODO: StreamProtocolV2Name, StreamProtocolV3Name, StreamProtocolV4Name support.
// SupportedStreamingProtocols is the streaming protocols which server supports.
var SupportedStreamingProtocols = []string{constant.StreamProtocolV1Name, constant.StreamProtocolV2Name}
// SupportedPortForwardProtocols is the portforward protocols which server supports.
var SupportedPortForwardProtocols = []string{constant.PortForwardProtocolV1Name}
// Server as an interface defines all operations against stream server.
type Server interface {
// GetExec get the serving URL for Exec request.
GetExec(*runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error)
// GetAttach get the serving URL for Attach request.
GetAttach(*runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error)
// GetPortForward get the serving URL for PortForward request.
GetPortForward(*runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error)
// Start starts the stream server.
Start() error
}
// Runtime is the interface to execute the commands and provide the streams.
type Runtime interface {
// Exec executes the command in pod.
Exec(containerID string, cmd []string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error
// Attach attaches to pod.
Attach(containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error
// PortForward forward port to pod.
PortForward(name string, port int32, stream io.ReadWriteCloser) error
}
// Config defines the options used for running the stream server.
type Config struct {
// Address is the addr:port address the server will listen on.
Address string
// BaseURL is the optional base URL for constructing streaming URLs. If empty, the baseURL will be constructed from the serve address.
BaseURL *url.URL
// StreamIdleTimeout is how long to leave idle connections open for.
StreamIdleTimeout time.Duration
// StreamCreationTimeout is how long to wait for clients to create streams. Only used for SPDY streaming.
StreamCreationTimeout time.Duration
// SupportedStreamingProtocols is the streaming protocols which server supports.
SupportedRemoteCommandProtocols []string
// SupportedPortForwardProtocol is the portforward protocols which server supports.
SupportedPortForwardProtocols []string
}
// DefaultConfig provides default values for server Config.
var DefaultConfig = Config{
StreamIdleTimeout: 4 * time.Hour,
StreamCreationTimeout: DefaultStreamCreationTimeout,
SupportedRemoteCommandProtocols: SupportedStreamingProtocols,
SupportedPortForwardProtocols: SupportedPortForwardProtocols,
}
type server struct {
config Config
runtime Runtime
cache *requestCache
server *http.Server
}
// NewServer creates a new stream server.
func NewServer(config Config, runtime Runtime) (Server, error) {
s := &server{
config: config,
runtime: runtime,
cache: newRequestCache(),
}
if s.config.BaseURL == nil {
s.config.BaseURL = &url.URL{
Scheme: "http",
Host: s.config.Address,
}
}
endpoints := []struct {
path string
handler http.HandlerFunc
}{
{"/exec/{token}", s.serveExec},
{"/attach/{token}", s.serveAttach},
{"/portforward/{token}", s.servePortForward},
}
r := mux.NewRouter()
for _, e := range endpoints {
for _, method := range []string{"GET", "POST"} {
r.Path(e.path).Methods(method).Handler(e.handler)
}
}
s.server = &http.Server{
Addr: s.config.Address,
Handler: r,
}
return s, nil
}
// Start starts the stream server.
func (s *server) Start() error {
return s.server.ListenAndServe()
}
func (s *server) serveExec(w http.ResponseWriter, r *http.Request) {
token := mux.Vars(r)["token"]
cachedRequest, ok := s.cache.Consume(token)
if !ok {
http.NotFound(w, r)
return
}
exec, ok := cachedRequest.(*runtimeapi.ExecRequest)
if !ok {
http.NotFound(w, r)
return
}
streamOpts := &remotecommand.Options{
Stdin: exec.Stdin,
Stdout: exec.Stdout,
Stderr: exec.Stderr,
TTY: exec.Tty,
}
remotecommand.ServeExec(
w,
r,
s.runtime,
exec.ContainerId,
exec.Cmd,
streamOpts,
s.config.SupportedRemoteCommandProtocols,
s.config.StreamIdleTimeout,
s.config.StreamCreationTimeout,
)
}
func (s *server) serveAttach(w http.ResponseWriter, r *http.Request) {
token := mux.Vars(r)["token"]
cachedRequest, ok := s.cache.Consume(token)
if !ok {
http.NotFound(w, r)
return
}
attach, ok := cachedRequest.(*runtimeapi.AttachRequest)
if !ok {
http.NotFound(w, r)
return
}
streamOpts := &remotecommand.Options{
Stdin: attach.Stdin,
Stdout: attach.Stdout,
Stderr: attach.Stderr,
TTY: attach.Tty,
}
remotecommand.ServeAttach(
w,
r,
s.runtime,
attach.ContainerId,
streamOpts,
s.config.StreamIdleTimeout,
s.config.StreamCreationTimeout,
s.config.SupportedRemoteCommandProtocols,
)
}
func (s *server) servePortForward(w http.ResponseWriter, r *http.Request) {
token := mux.Vars(r)["token"]
cachedRequest, ok := s.cache.Consume(token)
if !ok {
http.NotFound(w, r)
return
}
pf, ok := cachedRequest.(*runtimeapi.PortForwardRequest)
if !ok {
http.NotFound(w, r)
return
}
portforward.ServePortForward(
w,
r,
s.runtime,
pf.PodSandboxId,
s.config.StreamIdleTimeout,
s.config.StreamCreationTimeout,
s.config.SupportedPortForwardProtocols,
)
}
func (s *server) buildURL(method string, token string) string {
return s.config.BaseURL.ResolveReference(&url.URL{
Path: path.Join(method, token),
}).String()
}
// GetExec gets the serving URL for the Exec requests.
func (s *server) GetExec(req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
// TODO: validate the request.
token, err := s.cache.Insert(req)
if err != nil {
return nil, err
}
return &runtimeapi.ExecResponse{
Url: s.buildURL("exec", token),
}, nil
}
// GetAttach gets the serving URL for the Attach requests.
func (s *server) GetAttach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
// TODO: validate the request.
token, err := s.cache.Insert(req)
if err != nil {
return nil, err
}
return &runtimeapi.AttachResponse{
Url: s.buildURL("attach", token),
}, nil
}
// GetPortForward gets the serving URL for the PortForward requests.
func (s *server) GetPortForward(req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
if req.PodSandboxId == "" {
return nil, grpc.Errorf(codes.InvalidArgument, "missing required pod_sandbox_id")
}
token, err := s.cache.Insert(req)
if err != nil {
return nil, err
}
return &runtimeapi.PortForwardResponse{
Url: s.buildURL("portforward", token),
}, nil
}