Forráskód Böngészése

embed: mutate /v3alpha requests with /v3beta for backward compatibilities

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
Gyu-Ho Lee 8 éve
szülő
commit
c706c6e238
1 módosított fájl, 17 hozzáadás és 2 törlés
  1. 17 2
      embed/serve.go

+ 17 - 2
embed/serve.go

@@ -104,7 +104,7 @@ func (sctx *serveCtx) serve(
 		httpmux := sctx.createMux(gwmux, handler)
 		httpmux := sctx.createMux(gwmux, handler)
 
 
 		srvhttp := &http.Server{
 		srvhttp := &http.Server{
-			Handler:  httpmux,
+			Handler:  wrapMux(httpmux),
 			ErrorLog: logger, // do not log user error
 			ErrorLog: logger, // do not log user error
 		}
 		}
 		httpl := m.Match(cmux.HTTP1())
 		httpl := m.Match(cmux.HTTP1())
@@ -144,7 +144,7 @@ func (sctx *serveCtx) serve(
 		httpmux := sctx.createMux(gwmux, handler)
 		httpmux := sctx.createMux(gwmux, handler)
 
 
 		srv := &http.Server{
 		srv := &http.Server{
-			Handler:   httpmux,
+			Handler:   wrapMux(httpmux),
 			TLSConfig: tlscfg,
 			TLSConfig: tlscfg,
 			ErrorLog:  logger, // do not log user error
 			ErrorLog:  logger, // do not log user error
 		}
 		}
@@ -234,6 +234,21 @@ func (sctx *serveCtx) createMux(gwmux *gw.ServeMux, handler http.Handler) *http.
 	return httpmux
 	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) {
 func (sctx *serveCtx) registerUserHandler(s string, h http.Handler) {
 	if sctx.userHandlers[s] != nil {
 	if sctx.userHandlers[s] != nil {
 		plog.Warningf("path %s already registered by user handler", s)
 		plog.Warningf("path %s already registered by user handler", s)