Minor cleanup in watch handlers

Kubernetes-commit: d907062308563b1a9e52152c48f4240a6e11aade
This commit is contained in:
Wojciech Tyczyński 2023-11-29 22:31:05 +01:00 committed by Kubernetes Publisher
parent 442cc39449
commit 697d456e35
2 changed files with 11 additions and 12 deletions

View File

@ -160,7 +160,12 @@ func serveWatch(watcher watch.Interface, scope *RequestScope, mediaTypeOptions n
metricsScope: metricsScope,
}
server.ServeHTTP(w, req)
if wsstream.IsWebSocketRequest(req) {
w.Header().Set("Content-Type", server.MediaType)
websocket.Handler(server.HandleWS).ServeHTTP(w, req)
return
}
server.HandleHTTP(w, req)
}
// WatchServer serves a watch.Interface over a websocket or vanilla HTTP.
@ -185,15 +190,9 @@ type WatchServer struct {
metricsScope string
}
// ServeHTTP serves a series of encoded events via HTTP with Transfer-Encoding: chunked
// HandleHTTP serves a series of encoded events via HTTP with Transfer-Encoding: chunked.
// or over a websocket connection.
func (s *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if wsstream.IsWebSocketRequest(req) {
w.Header().Set("Content-Type", s.MediaType)
websocket.Handler(s.HandleWS).ServeHTTP(w, req)
return
}
func (s *WatchServer) HandleHTTP(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
err := fmt.Errorf("unable to start watch - can't get http.Flusher: %#v", w)
@ -265,7 +264,7 @@ func (s *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}
// HandleWS implements a websocket handler.
// HandleWS serves a series of encoded events over a websocket connection.
func (s *WatchServer) HandleWS(ws *websocket.Conn) {
defer ws.Close()
done := make(chan struct{})

View File

@ -612,7 +612,7 @@ func (t *fakeTimeoutFactory) TimeoutCh() (<-chan time.Time, func() bool) {
}
// serveWatch will serve a watch response according to the watcher and watchServer.
// Before watchServer.ServeHTTP, an error may occur like k8s.io/apiserver/pkg/endpoints/handlers/watch.go#serveWatch does.
// Before watchServer.HandleHTTP, an error may occur like k8s.io/apiserver/pkg/endpoints/handlers/watch.go#serveWatch does.
func serveWatch(watcher watch.Interface, watchServer *handlers.WatchServer, preServeErr error) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
defer watcher.Stop()
@ -622,7 +622,7 @@ func serveWatch(watcher watch.Interface, watchServer *handlers.WatchServer, preS
return
}
watchServer.ServeHTTP(w, req)
watchServer.HandleHTTP(w, req)
}
}