Remove runconfig package dependency from image and container routers.

Use an interface to specify the behavior of a configuration decoder.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2016-03-28 14:22:23 -04:00
parent 3ca29823d4
commit f0d26e1665
9 changed files with 56 additions and 13 deletions

View File

@ -0,0 +1,16 @@
package httputils
import (
"io"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/network"
)
// ContainerDecoder specifies how
// to translate an io.Reader into
// container configuration.
type ContainerDecoder interface {
DecodeConfig(src io.Reader) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error)
DecodeHostConfig(src io.Reader) (*container.HostConfig, error)
}

View File

@ -1,17 +1,22 @@
package container
import "github.com/docker/docker/api/server/router"
import (
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/server/router"
)
// containerRouter is a router to talk with the container controller
type containerRouter struct {
backend Backend
decoder httputils.ContainerDecoder
routes []router.Route
}
// NewRouter initializes a new container router
func NewRouter(b Backend) router.Router {
func NewRouter(b Backend, decoder httputils.ContainerDecoder) router.Router {
r := &containerRouter{
backend: b,
decoder: decoder,
}
r.initRoutes()
return r

View File

@ -16,7 +16,6 @@ import (
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/term"
"github.com/docker/docker/runconfig"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/filters"
@ -149,7 +148,7 @@ func (s *containerRouter) postContainersStart(ctx context.Context, w http.Respon
return err
}
c, err := runconfig.DecodeHostConfig(r.Body)
c, err := s.decoder.DecodeHostConfig(r.Body)
if err != nil {
return err
}
@ -350,7 +349,7 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
name := r.Form.Get("name")
config, hostConfig, networkingConfig, err := runconfig.DecodeContainerConfig(r.Body)
config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body)
if err != nil {
return err
}

View File

@ -1,17 +1,22 @@
package image
import "github.com/docker/docker/api/server/router"
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) router.Router {
func NewRouter(backend Backend, decoder httputils.ContainerDecoder) router.Router {
r := &imageRouter{
backend: backend,
decoder: decoder,
}
r.initRoutes()
return r

View File

@ -17,7 +17,6 @@ import (
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/reference"
"github.com/docker/docker/runconfig"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"golang.org/x/net/context"
@ -40,7 +39,7 @@ func (s *imageRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *
pause = true
}
c, _, _, err := runconfig.DecodeContainerConfig(r.Body)
c, _, _, err := s.decoder.DecodeConfig(r.Body)
if err != nil && err != io.EOF { //Do not fail if body is empty.
return err
}

View File

@ -231,6 +231,8 @@ func (daemon *Daemon) DeleteNetwork(networkID string) error {
return nil
}
// FilterNetworks returns a list of networks filtered by the given arguments.
// It returns an error if the filters are not included in the list of accepted filters.
func (daemon *Daemon) FilterNetworks(netFilters filters.Args) ([]libnetwork.Network, error) {
if netFilters.Len() != 0 {
if err := netFilters.Validate(netsettings.AcceptedFilters); err != nil {

View File

@ -20,7 +20,7 @@ var (
"id": filterNetworkByID,
}
// acceptFilters is an acceptable filter flag list
// AcceptedFilters is an acceptable filter flag list
// generated for validation. e.g.
// acceptedFilters = map[string]bool{
// "type": true,
@ -84,7 +84,7 @@ func filterNetworkByID(nws []libnetwork.Network, id string) (retNws []libnetwork
return retNws, nil
}
// FilterAllNetworks filters network list according to user specified filter
// FilterNetworks filters network list according to user specified filter
// and returns user chosen networks
func FilterNetworks(nws []libnetwork.Network, filter filters.Args) ([]libnetwork.Network, error) {
// if filter is empty, return original network list

View File

@ -37,6 +37,7 @@ import (
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/registry"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
"github.com/docker/go-connections/tlsconfig"
)
@ -405,9 +406,11 @@ func loadDaemonCliConfig(config *daemon.Config, daemonFlags *flag.FlagSet, commo
}
func initRouter(s *apiserver.Server, d *daemon.Daemon) {
decoder := runconfig.ContainerDecoder{}
routers := []router.Router{
container.NewRouter(d),
image.NewRouter(d),
container.NewRouter(d, decoder),
image.NewRouter(d, decoder),
systemrouter.NewRouter(d),
volume.NewRouter(d),
build.NewRouter(dockerfile.NewBuildManager(d)),

View File

@ -10,6 +10,20 @@ import (
networktypes "github.com/docker/engine-api/types/network"
)
// ContainerDecoder implements httputils.ContainerDecoder
// calling DecodeContainerConfig.
type ContainerDecoder struct{}
// DecodeConfig makes ContainerDecoder to implement httputils.ContainerDecoder
func (r ContainerDecoder) DecodeConfig(src io.Reader) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
return DecodeContainerConfig(src)
}
// DecodeHostConfig makes ContainerDecoder to implement httputils.ContainerDecoder
func (r ContainerDecoder) DecodeHostConfig(src io.Reader) (*container.HostConfig, error) {
return DecodeHostConfig(src)
}
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
// struct and returns both a Config and an HostConfig struct
// Be aware this function is not checking whether the resulted structs are nil,