Skip to content

Commit

Permalink
Merge pull request moby#19772 from calavera/decouple-server-routers
Browse files Browse the repository at this point in the history
[Carry 19133] Decouple server routers from the daemon package.
  • Loading branch information
tiborvass committed Feb 8, 2016
2 parents 3846951 + 06d8f50 commit 8c6887c
Show file tree
Hide file tree
Showing 22 changed files with 382 additions and 371 deletions.
7 changes: 2 additions & 5 deletions api/server/router/build/build.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package build

import (
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/local"
)
import "github.com/docker/docker/api/server/router"

// buildRouter is a router to talk with the build controller
type buildRouter struct {
Expand All @@ -27,6 +24,6 @@ func (r *buildRouter) Routes() []router.Route {

func (r *buildRouter) initRoutes() {
r.routes = []router.Route{
local.NewPostRoute("/build", r.postBuild),
router.NewPostRoute("/build", r.postBuild),
}
}
12 changes: 6 additions & 6 deletions api/server/router/container/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io"
"time"

"github.com/docker/docker/daemon"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/daemon/exec"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/version"
Expand Down Expand Up @@ -51,17 +51,17 @@ type stateBackend interface {
type monitorBackend interface {
ContainerChanges(name string) ([]archive.Change, error)
ContainerInspect(name string, size bool, version version.Version) (interface{}, error)
ContainerLogs(name string, config *daemon.ContainerLogsConfig) error
ContainerStats(name string, config *daemon.ContainerStatsConfig) error
ContainerLogs(name string, config *backend.ContainerLogsConfig) error
ContainerStats(name string, config *backend.ContainerStatsConfig) error
ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error)

Containers(config *daemon.ContainersConfig) ([]*types.Container, error)
Containers(config *types.ContainerListOptions) ([]*types.Container, error)
}

// attachBackend includes function to implement to provide container attaching functionality.
type attachBackend interface {
ContainerAttachWithLogs(name string, c *daemon.ContainerAttachWithLogsConfig) error
ContainerWsAttachWithLogs(name string, c *daemon.ContainerWsAttachWithLogsConfig) error
ContainerAttachWithLogs(name string, c *backend.ContainerAttachWithLogsConfig) error
ContainerWsAttachWithLogs(name string, c *backend.ContainerWsAttachWithLogsConfig) error
}

// Backend is all the methods that need to be implemented to provide container specific functionality.
Expand Down
65 changes: 31 additions & 34 deletions api/server/router/container/container.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package container

import (
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/local"
)
import "github.com/docker/docker/api/server/router"

// containerRouter is a router to talk with the container controller
type containerRouter struct {
Expand All @@ -20,7 +17,7 @@ func NewRouter(b Backend) router.Router {
return r
}

// Routes returns the available routers to the container controller
// Routes returns the available routes to the container controller
func (r *containerRouter) Routes() []router.Route {
return r.routes
}
Expand All @@ -29,38 +26,38 @@ func (r *containerRouter) Routes() []router.Route {
func (r *containerRouter) initRoutes() {
r.routes = []router.Route{
// HEAD
local.NewHeadRoute("/containers/{name:.*}/archive", r.headContainersArchive),
router.NewHeadRoute("/containers/{name:.*}/archive", r.headContainersArchive),
// GET
local.NewGetRoute("/containers/json", r.getContainersJSON),
local.NewGetRoute("/containers/{name:.*}/export", r.getContainersExport),
local.NewGetRoute("/containers/{name:.*}/changes", r.getContainersChanges),
local.NewGetRoute("/containers/{name:.*}/json", r.getContainersByName),
local.NewGetRoute("/containers/{name:.*}/top", r.getContainersTop),
local.NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs),
local.NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats),
local.NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
local.NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
local.NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
router.NewGetRoute("/containers/json", r.getContainersJSON),
router.NewGetRoute("/containers/{name:.*}/export", r.getContainersExport),
router.NewGetRoute("/containers/{name:.*}/changes", r.getContainersChanges),
router.NewGetRoute("/containers/{name:.*}/json", r.getContainersByName),
router.NewGetRoute("/containers/{name:.*}/top", r.getContainersTop),
router.NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs),
router.NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats),
router.NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
router.NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
router.NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
// POST
local.NewPostRoute("/containers/create", r.postContainersCreate),
local.NewPostRoute("/containers/{name:.*}/kill", r.postContainersKill),
local.NewPostRoute("/containers/{name:.*}/pause", r.postContainersPause),
local.NewPostRoute("/containers/{name:.*}/unpause", r.postContainersUnpause),
local.NewPostRoute("/containers/{name:.*}/restart", r.postContainersRestart),
local.NewPostRoute("/containers/{name:.*}/start", r.postContainersStart),
local.NewPostRoute("/containers/{name:.*}/stop", r.postContainersStop),
local.NewPostRoute("/containers/{name:.*}/wait", r.postContainersWait),
local.NewPostRoute("/containers/{name:.*}/resize", r.postContainersResize),
local.NewPostRoute("/containers/{name:.*}/attach", r.postContainersAttach),
local.NewPostRoute("/containers/{name:.*}/copy", r.postContainersCopy),
local.NewPostRoute("/containers/{name:.*}/exec", r.postContainerExecCreate),
local.NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
local.NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),
local.NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename),
local.NewPostRoute("/containers/{name:.*}/update", r.postContainerUpdate),
router.NewPostRoute("/containers/create", r.postContainersCreate),
router.NewPostRoute("/containers/{name:.*}/kill", r.postContainersKill),
router.NewPostRoute("/containers/{name:.*}/pause", r.postContainersPause),
router.NewPostRoute("/containers/{name:.*}/unpause", r.postContainersUnpause),
router.NewPostRoute("/containers/{name:.*}/restart", r.postContainersRestart),
router.NewPostRoute("/containers/{name:.*}/start", r.postContainersStart),
router.NewPostRoute("/containers/{name:.*}/stop", r.postContainersStop),
router.NewPostRoute("/containers/{name:.*}/wait", r.postContainersWait),
router.NewPostRoute("/containers/{name:.*}/resize", r.postContainersResize),
router.NewPostRoute("/containers/{name:.*}/attach", r.postContainersAttach),
router.NewPostRoute("/containers/{name:.*}/copy", r.postContainersCopy),
router.NewPostRoute("/containers/{name:.*}/exec", r.postContainerExecCreate),
router.NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
router.NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),
router.NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename),
router.NewPostRoute("/containers/{name:.*}/update", r.postContainerUpdate),
// PUT
local.NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive),
router.NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive),
// DELETE
local.NewDeleteRoute("/containers/{name:.*}", r.deleteContainers),
router.NewDeleteRoute("/containers/{name:.*}", r.deleteContainers),
}
}
57 changes: 27 additions & 30 deletions api/server/router/container/container_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/daemon"
"github.com/docker/docker/api/types/backend"
derr "github.com/docker/docker/errors"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/signal"
Expand All @@ -22,7 +22,7 @@ import (
"github.com/docker/docker/utils"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
timetypes "github.com/docker/engine-api/types/time"
"github.com/docker/engine-api/types/filters"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
)
Expand All @@ -31,13 +31,17 @@ func (s *containerRouter) getContainersJSON(ctx context.Context, w http.Response
if err := httputils.ParseForm(r); err != nil {
return err
}
filter, err := filters.FromParam(r.Form.Get("filters"))
if err != nil {
return err
}

config := &daemon.ContainersConfig{
All: httputils.BoolValue(r, "all"),
Size: httputils.BoolValue(r, "size"),
Since: r.Form.Get("since"),
Before: r.Form.Get("before"),
Filters: r.Form.Get("filters"),
config := &types.ContainerListOptions{
All: httputils.BoolValue(r, "all"),
Size: httputils.BoolValue(r, "size"),
Since: r.Form.Get("since"),
Before: r.Form.Get("before"),
Filter: filter,
}

if tmpLimit := r.Form.Get("limit"); tmpLimit != "" {
Expand Down Expand Up @@ -77,11 +81,11 @@ func (s *containerRouter) getContainersStats(ctx context.Context, w http.Respons
closeNotifier = notifier.CloseNotify()
}

config := &daemon.ContainerStatsConfig{
config := &backend.ContainerStatsConfig{
Stream: stream,
OutStream: out,
Stop: closeNotifier,
Version: httputils.VersionFromContext(ctx),
Version: string(httputils.VersionFromContext(ctx)),
}

return s.backend.ContainerStats(vars["name"], config)
Expand All @@ -102,15 +106,6 @@ func (s *containerRouter) getContainersLogs(ctx context.Context, w http.Response
return fmt.Errorf("Bad parameters: you must choose at least one stream")
}

var since time.Time
if r.Form.Get("since") != "" {
s, n, err := timetypes.ParseTimestamps(r.Form.Get("since"), 0)
if err != nil {
return err
}
since = time.Unix(s, n)
}

var closeNotifier <-chan bool
if notifier, ok := w.(http.CloseNotifier); ok {
closeNotifier = notifier.CloseNotify()
Expand All @@ -133,15 +128,17 @@ func (s *containerRouter) getContainersLogs(ctx context.Context, w http.Response
output := ioutils.NewWriteFlusher(w)
defer output.Close()

logsConfig := &daemon.ContainerLogsConfig{
Follow: httputils.BoolValue(r, "follow"),
Timestamps: httputils.BoolValue(r, "timestamps"),
Since: since,
Tail: r.Form.Get("tail"),
UseStdout: stdout,
UseStderr: stderr,
OutStream: output,
Stop: closeNotifier,
logsConfig := &backend.ContainerLogsConfig{
ContainerLogsOptions: types.ContainerLogsOptions{
Follow: httputils.BoolValue(r, "follow"),
Timestamps: httputils.BoolValue(r, "timestamps"),
Since: r.Form.Get("since"),
Tail: r.Form.Get("tail"),
ShowStdout: stdout,
ShowStderr: stderr,
},
OutStream: output,
Stop: closeNotifier,
}

if err := s.backend.ContainerLogs(containerName, logsConfig); err != nil {
Expand Down Expand Up @@ -446,7 +443,7 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo
}
}

attachWithLogsConfig := &daemon.ContainerAttachWithLogsConfig{
attachWithLogsConfig := &backend.ContainerAttachWithLogsConfig{
Hijacker: w.(http.Hijacker),
Upgrade: upgrade,
UseStdin: httputils.BoolValue(r, "stdin"),
Expand Down Expand Up @@ -483,7 +480,7 @@ func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.Respons
h := websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()

wsAttachWithLogsConfig := &daemon.ContainerWsAttachWithLogsConfig{
wsAttachWithLogsConfig := &backend.ContainerWsAttachWithLogsConfig{
InStream: ws,
OutStream: ws,
ErrStream: ws,
Expand Down
44 changes: 44 additions & 0 deletions api/server/router/image/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package image

import (
"io"

"github.com/docker/docker/reference"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/registry"
)

// 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 {
Commit(name string, config *types.ContainerCommitConfig) (imageID string, err error)
Exists(containerName string) bool
}

type imageBackend interface {
ImageDelete(imageRef string, force, prune bool) ([]types.ImageDelete, error)
ImageHistory(imageName string) ([]*types.ImageHistory, error)
Images(filterArgs string, filter string, all bool) ([]*types.Image, error)
LookupImage(name string) (*types.ImageInspect, error)
TagImage(newTag reference.Named, imageName string) error
}

type importExportBackend interface {
LoadImage(inTar io.ReadCloser, outStream io.Writer, quiet bool) error
ImportImage(src string, newRef reference.Named, msg string, inConfig io.ReadCloser, outStream io.Writer, config *container.Config) error
ExportImage(names []string, outStream io.Writer) error
}

type registryBackend interface {
PullImage(ref reference.Named, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error
PushImage(ref reference.Named, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error
SearchRegistryForImages(term string, authConfig *types.AuthConfig, metaHeaders map[string][]string) (*registry.SearchResults, error)
}
44 changes: 44 additions & 0 deletions api/server/router/image/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package image

import "github.com/docker/docker/api/server/router"

// imageRouter is a router to talk with the image controller
type imageRouter struct {
daemon Backend
routes []router.Route
}

// NewRouter initializes a new image router
func NewRouter(daemon Backend) router.Router {
r := &imageRouter{
daemon: daemon,
}
r.initRoutes()
return r
}

// Routes returns the available routes to the image controller
func (r *imageRouter) Routes() []router.Route {
return r.routes
}

// initRoutes initializes the routes in the image router
func (r *imageRouter) initRoutes() {
r.routes = []router.Route{
// GET
router.NewGetRoute("/images/json", r.getImagesJSON),
router.NewGetRoute("/images/search", r.getImagesSearch),
router.NewGetRoute("/images/get", r.getImagesGet),
router.NewGetRoute("/images/{name:.*}/get", r.getImagesGet),
router.NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
router.NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
// POST
router.NewPostRoute("/commit", r.postCommit),
router.NewPostRoute("/images/create", r.postImagesCreate),
router.NewPostRoute("/images/load", r.postImagesLoad),
router.NewPostRoute("/images/{name:.*}/push", r.postImagesPush),
router.NewPostRoute("/images/{name:.*}/tag", r.postImagesTag),
// DELETE
router.NewDeleteRoute("/images/{name:.*}", r.deleteImages),
}
}
Loading

0 comments on commit 8c6887c

Please sign in to comment.