From 38abba9e2c8f7ac27bd26bf98685b51585922317 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Tue, 24 Nov 2015 12:55:45 -0500 Subject: [PATCH] Move versioned references of inspect functions to the daemon. Leaving only one versioned main function that a backend must implement. Signed-off-by: David Calavera --- .../{backend_windows.go => backend.go} | 14 ++--- api/server/router/container/backend_unix.go | 56 ------------------- api/server/router/container/inspect.go | 14 +---- daemon/inspect.go | 17 +++++- daemon/inspect_unix.go | 4 +- daemon/inspect_windows.go | 6 +- 6 files changed, 25 insertions(+), 86 deletions(-) rename api/server/router/container/{backend_windows.go => backend.go} (83%) delete mode 100644 api/server/router/container/backend_unix.go diff --git a/api/server/router/container/backend_windows.go b/api/server/router/container/backend.go similarity index 83% rename from api/server/router/container/backend_windows.go rename to api/server/router/container/backend.go index 7caa16b8e5..97ba7c8611 100644 --- a/api/server/router/container/backend_windows.go +++ b/api/server/router/container/backend.go @@ -1,4 +1,4 @@ -// +build windows +// +build !windows package container @@ -7,9 +7,10 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/versions/v1p20" "github.com/docker/docker/daemon" + "github.com/docker/docker/daemon/exec" "github.com/docker/docker/pkg/archive" + "github.com/docker/docker/pkg/version" "github.com/docker/docker/runconfig" ) @@ -22,17 +23,12 @@ type Backend interface { ContainerCopy(name string, res string) (io.ReadCloser, error) ContainerCreate(params *daemon.ContainerCreateConfig) (types.ContainerCreateResponse, error) ContainerExecCreate(config *runconfig.ExecConfig) (string, error) - ContainerExecInspect(id string) (*daemon.ExecConfig, error) + ContainerExecInspect(id string) (*exec.Config, error) ContainerExecResize(name string, height, width int) error ContainerExecStart(name string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error ContainerExport(name string, out io.Writer) error ContainerExtractToDir(name, path string, noOverwriteDirNonDir bool, content io.Reader) error - ContainerInspect(name string, size bool) (*types.ContainerJSON, error) - ContainerInspect120(name string) (*v1p20.ContainerJSON, error) - // unix version - //ContainerInspectPre120(name string) (*v1p19.ContainerJSON, error) - // windows version - ContainerInspectPre120(name string) (*types.ContainerJSON, error) + ContainerInspect(name string, size bool, version version.Version) (interface{}, error) ContainerKill(name string, sig uint64) error ContainerLogs(containerName string, config *daemon.ContainerLogsConfig) error ContainerPause(name string) error diff --git a/api/server/router/container/backend_unix.go b/api/server/router/container/backend_unix.go deleted file mode 100644 index f973f4f22c..0000000000 --- a/api/server/router/container/backend_unix.go +++ /dev/null @@ -1,56 +0,0 @@ -// +build !windows - -package container - -import ( - "io" - "time" - - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/versions/v1p19" - "github.com/docker/docker/api/types/versions/v1p20" - "github.com/docker/docker/daemon" - "github.com/docker/docker/pkg/archive" - "github.com/docker/docker/runconfig" -) - -// Backend is all the methods that need to be implemented to provide -// container specific functionality -type Backend interface { - ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) - ContainerAttachWithLogs(prefixOrName string, c *daemon.ContainerAttachWithLogsConfig) error - ContainerChanges(name string) ([]archive.Change, error) - ContainerCopy(name string, res string) (io.ReadCloser, error) - ContainerCreate(params *daemon.ContainerCreateConfig) (types.ContainerCreateResponse, error) - ContainerExecCreate(config *runconfig.ExecConfig) (string, error) - ContainerExecInspect(id string) (*daemon.ExecConfig, error) - ContainerExecResize(name string, height, width int) error - ContainerExecStart(name string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error - ContainerExport(name string, out io.Writer) error - ContainerExtractToDir(name, path string, noOverwriteDirNonDir bool, content io.Reader) error - ContainerInspect(name string, size bool) (*types.ContainerJSON, error) - ContainerInspect120(name string) (*v1p20.ContainerJSON, error) - // unix version - ContainerInspectPre120(name string) (*v1p19.ContainerJSON, error) - // windows version - //ContainerInspectPre120(name string) (*types.ContainerJSON, error) - ContainerKill(name string, sig uint64) error - ContainerLogs(containerName string, config *daemon.ContainerLogsConfig) error - ContainerPause(name string) error - ContainerRename(oldName, newName string) error - ContainerResize(name string, height, width int) error - ContainerRestart(name string, seconds int) error - ContainerRm(name string, config *daemon.ContainerRmConfig) error - Containers(config *daemon.ContainersConfig) ([]*types.Container, error) - ContainerStart(name string, hostConfig *runconfig.HostConfig) error - ContainerStatPath(name string, path string) (stat *types.ContainerPathStat, err error) - ContainerStats(prefixOrName string, config *daemon.ContainerStatsConfig) error - ContainerStop(name string, seconds int) error - ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) - ContainerUnpause(name string) error - ContainerWait(name string, timeout time.Duration) (int, error) - ContainerWsAttachWithLogs(prefixOrName string, c *daemon.ContainerWsAttachWithLogsConfig) error - ExecExists(name string) (bool, error) - Exists(id string) bool - IsPaused(id string) bool -} diff --git a/api/server/router/container/inspect.go b/api/server/router/container/inspect.go index 51cf1c3460..e3bb09a345 100644 --- a/api/server/router/container/inspect.go +++ b/api/server/router/container/inspect.go @@ -11,20 +11,8 @@ import ( func (s *containerRouter) getContainersByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { displaySize := httputils.BoolValue(r, "size") - var json interface{} - var err error - version := httputils.VersionFromContext(ctx) - - switch { - case version.LessThan("1.20"): - json, err = s.backend.ContainerInspectPre120(vars["name"]) - case version.Equal("1.20"): - json, err = s.backend.ContainerInspect120(vars["name"]) - default: - json, err = s.backend.ContainerInspect(vars["name"], displaySize) - } - + json, err := s.backend.ContainerInspect(vars["name"], displaySize, version) if err != nil { return err } diff --git a/daemon/inspect.go b/daemon/inspect.go index 9adea0608c..dc27d7abb4 100644 --- a/daemon/inspect.go +++ b/daemon/inspect.go @@ -9,12 +9,23 @@ import ( "github.com/docker/docker/daemon/exec" "github.com/docker/docker/daemon/network" "github.com/docker/docker/layer" + "github.com/docker/docker/pkg/version" ) // ContainerInspect returns low-level information about a // container. Returns an error if the container cannot be found, or if // there is an error getting the data. -func (daemon *Daemon) ContainerInspect(name string, size bool) (*types.ContainerJSON, error) { +func (daemon *Daemon) ContainerInspect(name string, size bool, version version.Version) (interface{}, error) { + switch { + case version.LessThan("1.20"): + return daemon.containerInspectPre120(name) + case version.Equal("1.20"): + return daemon.containerInspect120(name) + } + return daemon.containerInspectCurrent(name, size) +} + +func (daemon *Daemon) containerInspectCurrent(name string, size bool) (*types.ContainerJSON, error) { container, err := daemon.Get(name) if err != nil { return nil, err @@ -53,8 +64,8 @@ func (daemon *Daemon) ContainerInspect(name string, size bool) (*types.Container }, nil } -// ContainerInspect120 serializes the master version of a container into a json type. -func (daemon *Daemon) ContainerInspect120(name string) (*v1p20.ContainerJSON, error) { +// containerInspect120 serializes the master version of a container into a json type. +func (daemon *Daemon) containerInspect120(name string) (*v1p20.ContainerJSON, error) { container, err := daemon.Get(name) if err != nil { return nil, err diff --git a/daemon/inspect_unix.go b/daemon/inspect_unix.go index d445660ff9..91f1cb75f2 100644 --- a/daemon/inspect_unix.go +++ b/daemon/inspect_unix.go @@ -17,8 +17,8 @@ func setPlatformSpecificContainerFields(container *Container, contJSONBase *type return contJSONBase } -// ContainerInspectPre120 gets containers for pre 1.20 APIs. -func (daemon *Daemon) ContainerInspectPre120(name string) (*v1p19.ContainerJSON, error) { +// containerInspectPre120 gets containers for pre 1.20 APIs. +func (daemon *Daemon) containerInspectPre120(name string) (*v1p19.ContainerJSON, error) { container, err := daemon.Get(name) if err != nil { return nil, err diff --git a/daemon/inspect_windows.go b/daemon/inspect_windows.go index 26b386160d..f571619ac7 100644 --- a/daemon/inspect_windows.go +++ b/daemon/inspect_windows.go @@ -21,7 +21,7 @@ func addMountPoints(container *Container) []types.MountPoint { return mountPoints } -// ContainerInspectPre120 get containers for pre 1.20 APIs. -func (daemon *Daemon) ContainerInspectPre120(name string) (*types.ContainerJSON, error) { - return daemon.ContainerInspect(name, false) +// containerInspectPre120 get containers for pre 1.20 APIs. +func (daemon *Daemon) containerInspectPre120(name string) (*types.ContainerJSON, error) { + return daemon.containerInspectCurrent(name, false) }