forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_http_server.go
482 lines (409 loc) · 16.4 KB
/
router_http_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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package router
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"github.com/golang/glog"
"golang.org/x/net/websocket"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/endpoints/discovery"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/cmd/util"
)
// GetDefaultLocalAddress returns an address at which the local host can
// be reached, or 0.0.0.0 (which should work for locations from the host
// to itself) if the actual default local address cannot be determined.
func GetDefaultLocalAddress() string {
addr := "0.0.0.0"
if a := os.Getenv("OPENSHIFT_ROUTER_SERVER_ADDRESS"); len(a) > 0 {
return a
}
ip, err := util.DefaultLocalIP4()
if err == nil {
addr = ip.String()
}
return addr
}
func NewTestHttpService() *TestHttpService {
return NewTestHttpServiceExtended("")
}
// NewTestHttpServer creates a new TestHttpService using default locations for listening address
// as well as default certificates. New channels will be initialized which can be used by test clients
// to feed events through the server to anything listening.
func NewTestHttpServiceExtended(namespaceListResponse string) *TestHttpService {
endpointChannel := make(chan string)
routeChannel := make(chan string)
ingressChannel := make(chan string)
secretChannel := make(chan string)
nodeChannel := make(chan string)
svcChannel := make(chan string)
addr := GetDefaultLocalAddress()
masterHttpAddr := fmt.Sprintf("%s:8080", addr)
podHttpAddr := fmt.Sprintf("%s:8888", addr)
alternatePodHttpAddr := fmt.Sprintf("%s:8889", addr)
podHttpsAddr := fmt.Sprintf("%s:8443", addr)
// Ensure an empty namespace response is valid json
if namespaceListResponse == "" {
namespaceListResponse = "{}"
}
return &TestHttpService{
MasterHttpAddr: masterHttpAddr,
PodHttpAddr: podHttpAddr,
AlternatePodHttpAddr: alternatePodHttpAddr,
PodHttpsAddr: podHttpsAddr,
PodTestPath: "test",
PodHttpsCert: []byte(Example2Cert),
PodHttpsKey: []byte(Example2Key),
PodHttpsCaCert: []byte(ExampleCACert),
EndpointChannel: endpointChannel,
RouteChannel: routeChannel,
IngressChannel: ingressChannel,
SecretChannel: secretChannel,
NodeChannel: nodeChannel,
SvcChannel: svcChannel,
NamespaceListResponse: namespaceListResponse,
}
}
// TestHttpService is a service that simulates a master k8s server for the router. It provides endpoints that
// a router running in docker can attach to for endpoint watches and route watches. It also simulates a client
// application so that routes can have a destination.
//
// Two channels are provided to simulate watch events: EndpointChannel and RouteChannel. Use these channels in
// you test cases to feed information to the router that would normally come from client CRUD actions.
//
// List events will return empty data for all calls.
type TestHttpService struct {
MasterHttpAddr string
PodHttpAddr string
AlternatePodHttpAddr string
PodHttpsAddr string
PodHttpsCert []byte
PodHttpsKey []byte
PodHttpsCaCert []byte
PodTestPath string
EndpointChannel chan string
RouteChannel chan string
IngressChannel chan string
SecretChannel chan string
NodeChannel chan string
SvcChannel chan string
NamespaceListResponse string
listeners []net.Listener
}
const (
// HelloPod is the expected response to a call to PodHttpAddr (usually called through a route)
HelloPod = "Hello Pod!"
HelloPodAlternate = "Alternate Hello Pod!"
// HelloPod is the expected response to a call to PodHttpAddr (usually called through a route)
HelloPodPath = "Hello Pod Path!"
// HelloPodSecure is the expected response to a call to PodHttpsAddr (usually called through a route)
HelloPodSecure = "Hello Pod Secure!"
// HelloPodPathSecure is the expected response to a call to PodHttpsAddr (usually called through a route)
HelloPodPathSecure = "Hello Pod Path Secure!"
)
type TestHttpSocketService struct {
ServeMux *http.ServeMux
WebSocketHandler websocket.Handler
}
func (s *TestHttpSocketService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Upgrade") == "websocket" {
s.WebSocketHandler.ServeHTTP(w, r)
} else {
s.ServeMux.ServeHTTP(w, r)
}
}
// handleHelloPod handles calls to PodHttpAddr (usually called through a route)
func (s *TestHttpService) handleHelloPod(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, HelloPod)
}
// handleHelloPod handles calls to PodHttpAddr (usually called through a route)
func (s *TestHttpService) handleHelloPod2(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, HelloPodAlternate)
}
// handleHelloPodTest handles calls to PodHttpAddr (usually called through a route) with the /test/ path
func (s *TestHttpService) handleHelloPodTest(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, HelloPodPath)
}
// handleHelloPodSecure handles calls to PodHttpsAddr (usually called through a route)
func (s *TestHttpService) handleHelloPodSecure(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, HelloPodSecure)
}
// handleHelloPodTestSecure handles calls to PodHttpsAddr (usually called through a route) with the /test/ path
func (s *TestHttpService) handleHelloPodTestSecure(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, HelloPodPathSecure)
}
// handleNamespaceList handles calls to /api/v1/namespaces/* and returns a canned response
func (s *TestHttpService) handleNamespaceList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
glog.Errorf("Returning response: %s", s.NamespaceListResponse)
fmt.Fprint(w, s.NamespaceListResponse)
}
// handleSvcList handles calls to /api/v1beta1/services and always returns empty data
func (s *TestHttpService) handleSvcList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleSvcWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleSvcWatch handles calls to /api/v1beta1/watch/services and uses the svc channel to simulate watch events
func (s *TestHttpService) handleSvcWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, <-s.SvcChannel)
}
// handleNodeList handles calls to /api/v1beta1/nodes and always returns empty data
func (s *TestHttpService) handleNodeList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleNodeWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleNodeWatch handles calls to /api/v1beta1/watch/nodes and uses the node channel to simulate watch events
func (s *TestHttpService) handleNodeWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, <-s.NodeChannel)
}
// handleRouteWatch handles calls to /osapi/v1beta1/watch/routes and uses the route channel to simulate watch events
func (s *TestHttpService) handleRouteWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
routeJSON := <-s.RouteChannel
// TODO: avoids a more extensive rewrite, future should send an event and the http service
// should have two codecs
if strings.HasPrefix(r.URL.Path, "/apis/route.openshift.io/v1") {
routeJSON = rewriteEventAPIVersion(routeJSON, "v1", "route.openshift.io/v1")
}
io.WriteString(w, routeJSON)
}
// handleRouteList handles calls to /osapi/v1beta1/routes and always returns empty data
func (s *TestHttpService) handleRouteList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleRouteWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleRouteCalls handles calls to /osapi/v1/routes/* and returns whatever the client sent
func (s *TestHttpService) handleRouteCalls(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleEndpointWatch handles calls to /api/v1beta1/watch/endpoints and uses the endpoint channel to simulate watch events
func (s *TestHttpService) handleEndpointWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, <-s.EndpointChannel)
}
// handleEndpointList handles calls to /api/v1beta1/endpoints and always returns empty data
func (s *TestHttpService) handleEndpointList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleEndpointWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleIngressWatch handles calls to /api/extensions/v1beta1/watch/ingresses and uses the ingress channel to simulate watch events
func (s *TestHttpService) handleIngressWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, <-s.IngressChannel)
}
// handleIngressList handles calls to /api/extensions/v1beta1/ingresses and always returns empty data
func (s *TestHttpService) handleIngressList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleIngressWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleSecretWatch handles calls to /api/v1/watch/secrets and uses the endpoint channel to simulate watch events
func (s *TestHttpService) handleSecretWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, <-s.SecretChannel)
}
// handleSecretList handles calls to /api/v1/secrets and always returns empty data
func (s *TestHttpService) handleSecretList(w http.ResponseWriter, r *http.Request) {
if len(r.FormValue("watch")) > 0 {
s.handleSecretWatch(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}")
}
// handleWebSocket copies whatever is written to the web socket back to the socket
func (s *TestHttpService) handleWebSocket(ws *websocket.Conn) {
_, err := io.Copy(ws, ws)
if err != nil {
panic(err)
}
}
// Stop stops the service by closing any registered listeners
func (s *TestHttpService) Stop() {
if s.listeners != nil && len(s.listeners) > 0 {
for _, l := range s.listeners {
if l != nil {
glog.Infof("Stopping listener at %s\n", l.Addr().String())
l.Close()
}
}
}
}
// Start will start the http service to simulate the master and client urls. It sets up the appropriate watch
// endpoints and serves the secure and non-secure traffic.
func (s *TestHttpService) Start() error {
s.listeners = make([]net.Listener, 3)
if err := s.startMaster(); err != nil {
return err
}
if err := s.startPod(); err != nil {
return err
}
return nil
}
func (s *TestHttpService) startMaster() error {
masterServer := http.NewServeMux()
apis := []string{"v1"}
for _, version := range apis {
masterServer.HandleFunc(fmt.Sprintf("/api/%s/namespaces/", version), s.handleNamespaceList)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/endpoints", version), s.handleEndpointList)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/watch/endpoints", version), s.handleEndpointWatch)
masterServer.HandleFunc(fmt.Sprintf("/oapi/%s/routes", version), s.handleRouteList)
masterServer.HandleFunc(fmt.Sprintf("/oapi/%s/namespaces/", version), s.handleRouteCalls)
masterServer.HandleFunc(fmt.Sprintf("/oapi/%s/watch/routes", version), s.handleRouteWatch)
masterServer.HandleFunc(fmt.Sprintf("/apis/route.openshift.io/%s/routes", version), s.handleRouteList)
masterServer.HandleFunc(fmt.Sprintf("/apis/route.openshift.io/%s/namespaces/", version), s.handleRouteCalls)
masterServer.HandleFunc(fmt.Sprintf("/apis/route.openshift.io/%s/watch/routes", version), s.handleRouteWatch)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/nodes", version), s.handleNodeList)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/watch/nodes", version), s.handleNodeWatch)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/services", version), s.handleSvcList)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/watch/services", version), s.handleSvcWatch)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/secrets", version), s.handleSecretList)
masterServer.HandleFunc(fmt.Sprintf("/api/%s/watch/secrets", version), s.handleSecretWatch)
}
masterServer.HandleFunc("/apis/extensions/v1beta1/ingresses", s.handleIngressList)
masterServer.HandleFunc("/apis/extensions/v1beta1/watch/ingresses", s.handleIngressWatch)
contextMapper := apirequest.NewRequestContextMapper()
h := discovery.NewRootAPIsHandler(discovery.DefaultAddresses{DefaultAddress: s.MasterHttpAddr}, kapi.Codecs, contextMapper)
h.AddGroup(metav1.APIGroup{
Name: "route.openshift.io",
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "route.openshift.io/v1", Version: "v1"}},
})
masterServer.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
glog.Infof("%s %s", req.Method, req.URL)
switch req.URL.Path {
case "/":
data, _ := json.Marshal(rootAPI{Paths: []string{"/oapi", "/oapi/v1", "/apis", "/apis/route.openshift.io", "/apis/route.openshift.io/v1"}})
w.WriteHeader(200)
w.Write(data)
return
case "/apis":
h.ServeHTTP(w, req)
return
}
glog.Infof("%s %s 404", req.Method, req.URL)
w.WriteHeader(404)
})
if err := s.startServing(s.MasterHttpAddr, http.Handler(masterServer)); err != nil {
return err
}
return nil
}
type rootAPI struct {
Paths []string `json:"paths"`
}
func (s *TestHttpService) startPod() error {
unsecurePodServeMux := http.NewServeMux()
unsecurePodServeMux.HandleFunc("/", s.handleHelloPod)
unsecurePodServeMux.HandleFunc("/"+s.PodTestPath, s.handleHelloPodTest)
unsecurePodServer := &TestHttpSocketService{
ServeMux: unsecurePodServeMux,
WebSocketHandler: websocket.Handler(s.handleWebSocket),
}
if err := s.startServing(s.PodHttpAddr, http.Handler(unsecurePodServer)); err != nil {
return err
}
alternatePodServeMux := http.NewServeMux()
alternatePodServeMux.HandleFunc("/", s.handleHelloPod2)
alternatePodServeMux.HandleFunc("/"+s.PodTestPath, s.handleHelloPod2)
alternatePodServer := &TestHttpSocketService{
ServeMux: alternatePodServeMux,
WebSocketHandler: websocket.Handler(s.handleWebSocket),
}
if err := s.startServing(s.AlternatePodHttpAddr, http.Handler(alternatePodServer)); err != nil {
return err
}
securePodServeMux := http.NewServeMux()
securePodServeMux.HandleFunc("/", s.handleHelloPodSecure)
securePodServeMux.HandleFunc("/"+s.PodTestPath, s.handleHelloPodTestSecure)
securePodServer := &TestHttpSocketService{
ServeMux: securePodServeMux,
WebSocketHandler: websocket.Handler(s.handleWebSocket),
}
if err := s.startServingTLS(s.PodHttpsAddr, s.PodHttpsCert, s.PodHttpsKey, s.PodHttpsCaCert, http.Handler(securePodServer)); err != nil {
return err
}
return nil
}
// startServing creates and registers a non-secure listener and begins serving traffic
func (s *TestHttpService) startServing(addr string, handler http.Handler) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
s.listeners = append(s.listeners, listener)
glog.Infof("Started, serving at %s\n", listener.Addr().String())
go func() {
err := http.Serve(listener, handler)
if err != nil {
glog.Errorf("HTTP server failed: %v", err)
}
}()
return nil
}
// startServingTLS creates and registers a secure listener and begins serving traffic.
func (s *TestHttpService) startServingTLS(addr string, cert []byte, key []byte, caCert []byte, handler http.Handler) error {
tlsCert, err := tls.X509KeyPair(append(cert, caCert...), key)
if err != nil {
return err
}
cfg := &tls.Config{
Certificates: []tls.Certificate{tlsCert},
}
listener, err := tls.Listen("tcp", addr, cfg)
if err != nil {
return err
}
s.listeners = append(s.listeners, listener)
glog.Infof("Started, serving TLS at %s\n", listener.Addr().String())
go func() {
if err := http.Serve(listener, handler); err != nil {
glog.Errorf("HTTPS server failed: %v", err)
}
}()
return nil
}
func rewriteEventAPIVersion(s string, fromVersion, toVersion string) string {
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(s), &m); err != nil {
panic(err)
}
obj := m["object"].(map[string]interface{})
if obj["apiVersion"].(string) != fromVersion {
panic(obj["apiVersion"])
}
obj["apiVersion"] = toVersion
data, err := json.Marshal(m)
if err != nil {
panic(err)
}
return string(data)
}