2014-07-29 21:35:09 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-07-29 21:35:09 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
2015-05-12 02:41:13 +00:00
|
|
|
"math/rand"
|
2014-07-29 21:35:09 +00:00
|
|
|
"net/http"
|
2015-03-24 04:07:22 +00:00
|
|
|
"reflect"
|
2014-09-17 15:13:17 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-05-12 02:41:13 +00:00
|
|
|
"time"
|
2014-07-29 21:35:09 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-07-29 21:35:09 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-09-17 04:33:48 +00:00
|
|
|
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
|
2014-11-06 01:22:18 +00:00
|
|
|
|
2015-03-24 04:07:22 +00:00
|
|
|
"github.com/emicklei/go-restful"
|
2014-11-06 01:22:18 +00:00
|
|
|
"github.com/golang/glog"
|
2014-11-05 23:48:29 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2014-07-29 21:35:09 +00:00
|
|
|
)
|
|
|
|
|
2015-05-12 02:41:13 +00:00
|
|
|
var (
|
|
|
|
connectionUpgradeRegex = regexp.MustCompile("(^|.*,\\s*)upgrade($|\\s*,)")
|
|
|
|
|
|
|
|
// nothing will ever be sent down this channel
|
|
|
|
neverExitWatch <-chan time.Time = make(chan time.Time)
|
|
|
|
)
|
2014-09-17 15:13:17 +00:00
|
|
|
|
|
|
|
func isWebsocketRequest(req *http.Request) bool {
|
|
|
|
return connectionUpgradeRegex.MatchString(strings.ToLower(req.Header.Get("Connection"))) && strings.ToLower(req.Header.Get("Upgrade")) == "websocket"
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:41:13 +00:00
|
|
|
// timeoutFactory abstracts watch timeout logic for testing
|
|
|
|
type timeoutFactory interface {
|
|
|
|
TimeoutCh() (<-chan time.Time, func() bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// realTimeoutFactory implements timeoutFactory
|
|
|
|
type realTimeoutFactory struct {
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeoutChan returns a channel which will receive something when the watch times out,
|
|
|
|
// and a cleanup function to call when this happens.
|
|
|
|
func (w *realTimeoutFactory) TimeoutCh() (<-chan time.Time, func() bool) {
|
|
|
|
if w.timeout == 0 {
|
|
|
|
return neverExitWatch, func() bool { return false }
|
|
|
|
}
|
|
|
|
t := time.NewTimer(w.timeout)
|
|
|
|
return t.C, t.Stop
|
|
|
|
}
|
|
|
|
|
2015-03-24 04:07:22 +00:00
|
|
|
// serveWatch handles serving requests to the server
|
2015-06-16 02:39:31 +00:00
|
|
|
func serveWatch(watcher watch.Interface, scope RequestScope, w http.ResponseWriter, req *restful.Request, minRequestTimeout time.Duration) {
|
2015-05-27 01:39:42 +00:00
|
|
|
var timeout time.Duration
|
|
|
|
if minRequestTimeout > 0 {
|
2015-05-28 22:50:39 +00:00
|
|
|
// Each watch gets a random timeout between minRequestTimeout and 2*minRequestTimeout to avoid thundering herds.
|
2015-06-16 02:39:31 +00:00
|
|
|
timeout = time.Duration(float64(minRequestTimeout) * (rand.Float64() + 1.0))
|
2015-05-27 01:39:42 +00:00
|
|
|
}
|
2015-03-24 04:07:22 +00:00
|
|
|
watchServer := &WatchServer{watcher, scope.Codec, func(obj runtime.Object) {
|
|
|
|
if err := setSelfLink(obj, req, scope.Namer); err != nil {
|
|
|
|
glog.V(5).Infof("Failed to set self link for object %v: %v", reflect.TypeOf(obj), err)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
2015-05-12 02:41:13 +00:00
|
|
|
}, &realTimeoutFactory{timeout}}
|
2015-03-24 04:07:22 +00:00
|
|
|
if isWebsocketRequest(req.Request) {
|
|
|
|
websocket.Handler(watchServer.HandleWS).ServeHTTP(httplog.Unlogged(w), req.Request)
|
2015-01-12 05:33:25 +00:00
|
|
|
} else {
|
2015-03-24 04:07:22 +00:00
|
|
|
watchServer.ServeHTTP(w, req.Request)
|
2015-03-22 21:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:35:09 +00:00
|
|
|
// WatchServer serves a watch.Interface over a websocket or vanilla HTTP.
|
|
|
|
type WatchServer struct {
|
|
|
|
watching watch.Interface
|
2014-09-11 17:02:53 +00:00
|
|
|
codec runtime.Codec
|
2014-11-06 01:22:18 +00:00
|
|
|
fixup func(runtime.Object)
|
2015-05-12 02:41:13 +00:00
|
|
|
t timeoutFactory
|
2014-07-29 21:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleWS implements a websocket handler.
|
|
|
|
func (w *WatchServer) HandleWS(ws *websocket.Conn) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
var unused interface{}
|
|
|
|
// Expect this to block until the connection is closed. Client should not
|
|
|
|
// send anything.
|
|
|
|
websocket.JSON.Receive(ws, &unused)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
w.watching.Stop()
|
|
|
|
return
|
|
|
|
case event, ok := <-w.watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
// End of results.
|
|
|
|
return
|
|
|
|
}
|
2014-11-06 01:22:18 +00:00
|
|
|
w.fixup(event.Object)
|
2014-09-17 04:33:48 +00:00
|
|
|
obj, err := watchjson.Object(w.codec, &event)
|
2014-07-29 21:35:09 +00:00
|
|
|
if err != nil {
|
|
|
|
// Client disconnect.
|
|
|
|
w.watching.Stop()
|
|
|
|
return
|
|
|
|
}
|
2014-09-11 17:02:53 +00:00
|
|
|
if err := websocket.JSON.Send(ws, obj); err != nil {
|
|
|
|
// Client disconnect.
|
|
|
|
w.watching.Stop()
|
|
|
|
return
|
|
|
|
}
|
2014-07-29 21:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP serves a series of JSON encoded events via straight HTTP with
|
|
|
|
// Transfer-Encoding: chunked.
|
|
|
|
func (self *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2014-09-09 21:05:18 +00:00
|
|
|
loggedW := httplog.LogOf(req, w)
|
2014-07-29 21:35:09 +00:00
|
|
|
w = httplog.Unlogged(w)
|
2015-05-12 02:41:13 +00:00
|
|
|
timeoutCh, cleanup := self.t.TimeoutCh()
|
|
|
|
defer cleanup()
|
|
|
|
defer self.watching.Stop()
|
2014-07-29 21:35:09 +00:00
|
|
|
|
|
|
|
cn, ok := w.(http.CloseNotifier)
|
|
|
|
if !ok {
|
|
|
|
loggedW.Addf("unable to get CloseNotifier")
|
2014-09-11 20:48:06 +00:00
|
|
|
http.NotFound(w, req)
|
2014-07-29 21:35:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
loggedW.Addf("unable to get Flusher")
|
2014-09-11 20:48:06 +00:00
|
|
|
http.NotFound(w, req)
|
2014-07-29 21:35:09 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-11 20:48:06 +00:00
|
|
|
w.Header().Set("Transfer-Encoding", "chunked")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2014-07-29 21:35:09 +00:00
|
|
|
flusher.Flush()
|
2014-09-17 04:33:48 +00:00
|
|
|
encoder := watchjson.NewEncoder(w, self.codec)
|
2014-07-29 21:35:09 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-cn.CloseNotify():
|
2015-05-12 02:41:13 +00:00
|
|
|
return
|
|
|
|
case <-timeoutCh:
|
2014-07-29 21:35:09 +00:00
|
|
|
return
|
|
|
|
case event, ok := <-self.watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
// End of results.
|
|
|
|
return
|
|
|
|
}
|
2014-11-06 01:22:18 +00:00
|
|
|
self.fixup(event.Object)
|
2014-09-17 04:33:48 +00:00
|
|
|
if err := encoder.Encode(&event); err != nil {
|
2014-09-11 17:02:53 +00:00
|
|
|
// Client disconnect.
|
|
|
|
return
|
|
|
|
}
|
2014-07-29 21:35:09 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|