Skip to content

Commit

Permalink
Merge pull request moby#49051 from thaJeztah/cleanup_routers
Browse files Browse the repository at this point in the history
api/server/router: fix debug routes, and refactor
  • Loading branch information
thaJeztah authored Dec 9, 2024
2 parents d84cbab + 9da0e69 commit b249c5e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
14 changes: 7 additions & 7 deletions api/server/router/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type debugRouter struct {

func (r *debugRouter) initRoutes() {
r.routes = []router.Route{
router.NewGetRoute("/vars", frameworkAdaptHandler(expvar.Handler())),
router.NewGetRoute("/pprof/", frameworkAdaptHandlerFunc(pprof.Index)),
router.NewGetRoute("/pprof/cmdline", frameworkAdaptHandlerFunc(pprof.Cmdline)),
router.NewGetRoute("/pprof/profile", frameworkAdaptHandlerFunc(pprof.Profile)),
router.NewGetRoute("/pprof/symbol", frameworkAdaptHandlerFunc(pprof.Symbol)),
router.NewGetRoute("/pprof/trace", frameworkAdaptHandlerFunc(pprof.Trace)),
router.NewGetRoute("/pprof/{name}", handlePprof),
router.NewGetRoute("/debug/vars", frameworkAdaptHandler(expvar.Handler())),
router.NewGetRoute("/debug/pprof/", frameworkAdaptHandlerFunc(pprof.Index)),
router.NewGetRoute("/debug/pprof/cmdline", frameworkAdaptHandlerFunc(pprof.Cmdline)),
router.NewGetRoute("/debug/pprof/profile", frameworkAdaptHandlerFunc(pprof.Profile)),
router.NewGetRoute("/debug/pprof/symbol", frameworkAdaptHandlerFunc(pprof.Symbol)),
router.NewGetRoute("/debug/pprof/trace", frameworkAdaptHandlerFunc(pprof.Trace)),
router.NewGetRoute("/debug/pprof/{name}", handlePprof),
}
}

Expand Down
19 changes: 7 additions & 12 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/server/middleware"
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/debug"
"github.com/docker/docker/api/types"
"github.com/docker/docker/dockerversion"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -66,26 +65,22 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc, operation string) ht
}

// CreateMux returns a new mux with all the routers registered.
func (s *Server) CreateMux(routers ...router.Router) *mux.Router {
func (s *Server) CreateMux(ctx context.Context, routers ...router.Router) *mux.Router {
log.G(ctx).Debug("Registering routers")
m := mux.NewRouter()

log.G(context.TODO()).Debug("Registering routers")
for _, apiRouter := range routers {
for _, r := range apiRouter.Routes() {
if ctx.Err() != nil {
return m
}
log.G(ctx).WithFields(log.Fields{"method": r.Method(), "path": r.Path()}).Debug("Registering route")
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())

log.G(context.TODO()).Debugf("Registering %s, %s", r.Method(), r.Path())
m.Path(versionMatcher + r.Path()).Methods(r.Method()).Handler(f)
m.Path(r.Path()).Methods(r.Method()).Handler(f)
}
}

debugRouter := debug.NewRouter()
for _, r := range debugRouter.Routes() {
f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
m.Path("/debug" + r.Path()).Handler(f)
}

// Setup handlers for undefined paths and methods
notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = httputils.WriteJSON(w, http.StatusNotFound, &types.ErrorResponse{
Message: "page not found",
Expand Down
4 changes: 3 additions & 1 deletion cmd/dockerd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/docker/docker/api/server/router/build"
checkpointrouter "github.com/docker/docker/api/server/router/checkpoint"
"github.com/docker/docker/api/server/router/container"
debugrouter "github.com/docker/docker/api/server/router/debug"
distributionrouter "github.com/docker/docker/api/server/router/distribution"
grpcrouter "github.com/docker/docker/api/server/router/grpc"
"github.com/docker/docker/api/server/router/image"
Expand Down Expand Up @@ -306,7 +307,7 @@ func (cli *daemonCLI) start(ctx context.Context) (err error) {
}

routers := buildRouters(routerOpts)
httpServer.Handler = apiServer.CreateMux(routers...)
httpServer.Handler = apiServer.CreateMux(ctx, routers...)

go d.ProcessClusterNotifications(ctx, c.GetWatchStream())

Expand Down Expand Up @@ -716,6 +717,7 @@ func buildRouters(opts routerOptions) []router.Router {
pluginrouter.NewRouter(opts.daemon.PluginManager()),
distributionrouter.NewRouter(opts.daemon.ImageBackend()),
network.NewRouter(opts.daemon, opts.cluster),
debugrouter.NewRouter(),
}

if opts.buildBackend != nil {
Expand Down
4 changes: 4 additions & 0 deletions docs/api/version-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ keywords: "API, Docker, rcli, REST, documentation"
the one that sorts first is picked.
* `GET /containers/json` now returns a `GwPriority` field in `NetworkSettings`
for each network endpoint.
* API debug endpoints (`GET /debug/vars`, `GET /debug/pprof/`, `GET /debug/pprof/cmdline`,
`GET /debug/pprof/profile`, `GET /debug/pprof/symbol`, `GET /debug/pprof/trace`,
`GET /debug/pprof/{name}`) are now also accessible through the versioned-API
paths (`/v<API-version>/<endpoint>`).

## v1.47 API changes

Expand Down

0 comments on commit b249c5e

Please sign in to comment.