From 697d456e359f861462b9ace18a2edfdd16671f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Tyczy=C5=84ski?= Date: Wed, 29 Nov 2023 22:31:05 +0100 Subject: [PATCH] Minor cleanup in watch handlers Kubernetes-commit: d907062308563b1a9e52152c48f4240a6e11aade --- pkg/endpoints/handlers/watch.go | 19 +++++++++---------- pkg/endpoints/watch_test.go | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/endpoints/handlers/watch.go b/pkg/endpoints/handlers/watch.go index c022a0fdd..6043fec3b 100644 --- a/pkg/endpoints/handlers/watch.go +++ b/pkg/endpoints/handlers/watch.go @@ -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{}) diff --git a/pkg/endpoints/watch_test.go b/pkg/endpoints/watch_test.go index e18e0c4f0..d1a138b0a 100644 --- a/pkg/endpoints/watch_test.go +++ b/pkg/endpoints/watch_test.go @@ -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) } }