From c706c6e2388cd0fcfea010cbc50d1710b34917d9 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Wed, 15 Nov 2017 01:41:36 -0800 Subject: [PATCH] embed: mutate /v3alpha requests with /v3beta for backward compatibilities Signed-off-by: Gyu-Ho Lee --- embed/serve.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/embed/serve.go b/embed/serve.go index 7b6cac2d7a9..12af13cb884 100644 --- a/embed/serve.go +++ b/embed/serve.go @@ -104,7 +104,7 @@ func (sctx *serveCtx) serve( httpmux := sctx.createMux(gwmux, handler) srvhttp := &http.Server{ - Handler: httpmux, + Handler: wrapMux(httpmux), ErrorLog: logger, // do not log user error } httpl := m.Match(cmux.HTTP1()) @@ -144,7 +144,7 @@ func (sctx *serveCtx) serve( httpmux := sctx.createMux(gwmux, handler) srv := &http.Server{ - Handler: httpmux, + Handler: wrapMux(httpmux), TLSConfig: tlscfg, ErrorLog: logger, // do not log user error } @@ -234,6 +234,21 @@ func (sctx *serveCtx) createMux(gwmux *gw.ServeMux, handler http.Handler) *http. return httpmux } +// wraps HTTP multiplexer to mute requests to /v3alpha +// TODO: deprecate this in 3.4 release +func wrapMux(mux *http.ServeMux) http.Handler { return &v3alphaMutator{mux: mux} } + +type v3alphaMutator struct { + mux *http.ServeMux +} + +func (m *v3alphaMutator) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if req != nil && req.URL != nil && strings.HasPrefix(req.URL.Path, "/v3alpha/") { + req.URL.Path = strings.Replace(req.URL.Path, "/v3alpha/", "/v3beta/", 1) + } + m.mux.ServeHTTP(rw, req) +} + func (sctx *serveCtx) registerUserHandler(s string, h http.Handler) { if sctx.userHandlers[s] != nil { plog.Warningf("path %s already registered by user handler", s)