From e574c5ae73f2f54c47319e5e4a17b16bd93213be Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 7 Feb 2018 15:33:20 -0500 Subject: [PATCH] Move commit to container backend Signed-off-by: Daniel Nephin --- api/server/router/container/backend.go | 5 +++ api/server/router/container/container.go | 1 + .../router/container/container_routes.go | 39 ++++++++++++++++++ api/server/router/image/backend.go | 6 --- api/server/router/image/image.go | 10 +---- api/server/router/image/image_routes.go | 41 ------------------- cmd/dockerd/daemon.go | 2 +- 7 files changed, 48 insertions(+), 56 deletions(-) diff --git a/api/server/router/container/backend.go b/api/server/router/container/backend.go index b6b1dec94c..5072083c69 100644 --- a/api/server/router/container/backend.go +++ b/api/server/router/container/backend.go @@ -68,8 +68,13 @@ type systemBackend interface { ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error) } +type commitBackend interface { + CreateImageFromContainer(name string, config *backend.CreateImageConfig) (imageID string, err error) +} + // Backend is all the methods that need to be implemented to provide container specific functionality. type Backend interface { + commitBackend execBackend copyBackend stateBackend diff --git a/api/server/router/container/container.go b/api/server/router/container/container.go index c763900c60..358f2bc2c1 100644 --- a/api/server/router/container/container.go +++ b/api/server/router/container/container.go @@ -61,6 +61,7 @@ func (r *containerRouter) initRoutes() { router.NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename), router.NewPostRoute("/containers/{name:.*}/update", r.postContainerUpdate), router.NewPostRoute("/containers/prune", r.postContainersPrune, router.WithCancel), + router.NewPostRoute("/commit", r.postCommit), // PUT router.NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive), // DELETE diff --git a/api/server/router/container/container_routes.go b/api/server/router/container/container_routes.go index 5de01e7639..0527810f23 100644 --- a/api/server/router/container/container_routes.go +++ b/api/server/router/container/container_routes.go @@ -24,6 +24,45 @@ import ( "golang.org/x/net/websocket" ) +func (s *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { + if err := httputils.ParseForm(r); err != nil { + return err + } + + if err := httputils.CheckForJSON(r); err != nil { + return err + } + + // TODO: remove pause arg, and always pause in backend + pause := httputils.BoolValue(r, "pause") + version := httputils.VersionFromContext(ctx) + if r.FormValue("pause") == "" && versions.GreaterThanOrEqualTo(version, "1.13") { + pause = true + } + + config, _, _, err := s.decoder.DecodeConfig(r.Body) + if err != nil && err != io.EOF { //Do not fail if body is empty. + return err + } + + commitCfg := &backend.CreateImageConfig{ + Pause: pause, + Repo: r.Form.Get("repo"), + Tag: r.Form.Get("tag"), + Author: r.Form.Get("author"), + Comment: r.Form.Get("comment"), + Config: config, + Changes: r.Form["changes"], + } + + imgID, err := s.backend.CreateImageFromContainer(r.Form.Get("container"), commitCfg) + if err != nil { + return err + } + + return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID}) +} + func (s *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := httputils.ParseForm(r); err != nil { return err diff --git a/api/server/router/image/backend.go b/api/server/router/image/backend.go index ffbc9c181a..e4d4b7d1bb 100644 --- a/api/server/router/image/backend.go +++ b/api/server/router/image/backend.go @@ -4,7 +4,6 @@ import ( "io" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/registry" @@ -14,16 +13,11 @@ import ( // Backend is all the methods that need to be implemented // to provide image specific functionality. type Backend interface { - containerBackend imageBackend importExportBackend registryBackend } -type containerBackend interface { - CreateImageFromContainer(name string, config *backend.CreateImageConfig) (imageID string, err error) -} - type imageBackend interface { ImageDelete(imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error) ImageHistory(imageName string) ([]*image.HistoryResponseItem, error) diff --git a/api/server/router/image/image.go b/api/server/router/image/image.go index 980bbd2421..6d5d87f63c 100644 --- a/api/server/router/image/image.go +++ b/api/server/router/image/image.go @@ -1,23 +1,18 @@ package image // import "github.com/docker/docker/api/server/router/image" import ( - "github.com/docker/docker/api/server/httputils" "github.com/docker/docker/api/server/router" ) // imageRouter is a router to talk with the image controller type imageRouter struct { backend Backend - decoder httputils.ContainerDecoder routes []router.Route } // NewRouter initializes a new image router -func NewRouter(backend Backend, decoder httputils.ContainerDecoder) router.Router { - r := &imageRouter{ - backend: backend, - decoder: decoder, - } +func NewRouter(backend Backend) router.Router { + r := &imageRouter{backend: backend} r.initRoutes() return r } @@ -38,7 +33,6 @@ func (r *imageRouter) initRoutes() { router.NewGetRoute("/images/{name:.*}/history", r.getImagesHistory), router.NewGetRoute("/images/{name:.*}/json", r.getImagesByName), // POST - router.NewPostRoute("/commit", r.postCommit), router.NewPostRoute("/images/load", r.postImagesLoad), router.NewPostRoute("/images/create", r.postImagesCreate, router.WithCancel), router.NewPostRoute("/images/{name:.*}/push", r.postImagesPush, router.WithCancel), diff --git a/api/server/router/image/image_routes.go b/api/server/router/image/image_routes.go index 19a9e74f45..cb497134a2 100644 --- a/api/server/router/image/image_routes.go +++ b/api/server/router/image/image_routes.go @@ -4,14 +4,12 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io" "net/http" "strconv" "strings" "github.com/docker/docker/api/server/httputils" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/versions" "github.com/docker/docker/errdefs" @@ -24,45 +22,6 @@ import ( "golang.org/x/net/context" ) -func (s *imageRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if err := httputils.ParseForm(r); err != nil { - return err - } - - if err := httputils.CheckForJSON(r); err != nil { - return err - } - - // TODO: remove pause arg, and always pause in backend - pause := httputils.BoolValue(r, "pause") - version := httputils.VersionFromContext(ctx) - if r.FormValue("pause") == "" && versions.GreaterThanOrEqualTo(version, "1.13") { - pause = true - } - - config, _, _, err := s.decoder.DecodeConfig(r.Body) - if err != nil && err != io.EOF { //Do not fail if body is empty. - return err - } - - commitCfg := &backend.CreateImageConfig{ - Pause: pause, - Repo: r.Form.Get("repo"), - Tag: r.Form.Get("tag"), - Author: r.Form.Get("author"), - Comment: r.Form.Get("comment"), - Config: config, - Changes: r.Form["changes"], - } - - imgID, err := s.backend.CreateImageFromContainer(r.Form.Get("container"), commitCfg) - if err != nil { - return err - } - - return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID}) -} - // Creates an image from Pull or from Import func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index d73b63a0f0..6665cf032e 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -514,7 +514,7 @@ func initRouter(opts routerOptions) { // we need to add the checkpoint router before the container router or the DELETE gets masked checkpointrouter.NewRouter(opts.daemon, decoder), container.NewRouter(opts.daemon, decoder), - image.NewRouter(opts.daemon, decoder), + image.NewRouter(opts.daemon), systemrouter.NewRouter(opts.daemon, opts.cluster, opts.buildCache), volume.NewRouter(opts.daemon), build.NewRouter(opts.buildBackend, opts.daemon),