ソースを参照

Merge pull request #18721 from tiborvass/remove-dependencies-from-builder

Remove image and daemon dependencies from builder
Vincent Demeester 9 年 前
コミット
64d70de0a2

+ 1 - 1
api/server/router/container/backend.go

@@ -32,7 +32,7 @@ type copyBackend interface {
 
 
 // stateBackend includes functions to implement to provide container state lifecycle functionality.
 // stateBackend includes functions to implement to provide container state lifecycle functionality.
 type stateBackend interface {
 type stateBackend interface {
-	ContainerCreate(params *daemon.ContainerCreateConfig) (types.ContainerCreateResponse, error)
+	ContainerCreate(types.ContainerCreateConfig) (types.ContainerCreateResponse, error)
 	ContainerKill(name string, sig uint64) error
 	ContainerKill(name string, sig uint64) error
 	ContainerPause(name string) error
 	ContainerPause(name string) error
 	ContainerRename(oldName, newName string) error
 	ContainerRename(oldName, newName string) error

+ 1 - 1
api/server/router/container/container_routes.go

@@ -339,7 +339,7 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
 	version := httputils.VersionFromContext(ctx)
 	version := httputils.VersionFromContext(ctx)
 	adjustCPUShares := version.LessThan("1.19")
 	adjustCPUShares := version.LessThan("1.19")
 
 
-	ccr, err := s.backend.ContainerCreate(&daemon.ContainerCreateConfig{
+	ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{
 		Name:            name,
 		Name:            name,
 		Config:          config,
 		Config:          config,
 		HostConfig:      hostConfig,
 		HostConfig:      hostConfig,

+ 8 - 0
api/types/configs.go

@@ -8,6 +8,14 @@ import (
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/runconfig"
 )
 )
 
 
+// ContainerCreateConfig is the parameter set to ContainerCreate()
+type ContainerCreateConfig struct {
+	Name            string
+	Config          *runconfig.Config
+	HostConfig      *runconfig.HostConfig
+	AdjustCPUShares bool
+}
+
 // ContainerRmConfig holds arguments for the container remove
 // ContainerRmConfig holds arguments for the container remove
 // operation. This struct is used to tell the backend what operations
 // operation. This struct is used to tell the backend what operations
 // to perform.
 // to perform.

+ 5 - 7
builder/builder.go

@@ -10,8 +10,6 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/daemon"
-	"github.com/docker/docker/image"
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/runconfig"
 )
 )
 
 
@@ -112,13 +110,13 @@ type Backend interface {
 	// TODO: use digest reference instead of name
 	// TODO: use digest reference instead of name
 
 
 	// GetImage looks up a Docker image referenced by `name`.
 	// GetImage looks up a Docker image referenced by `name`.
-	GetImage(name string) (*image.Image, error)
+	GetImage(name string) (Image, error)
 	// Pull tells Docker to pull image referenced by `name`.
 	// Pull tells Docker to pull image referenced by `name`.
-	Pull(name string) (*image.Image, error)
-	// ContainerWsAttachWithLogs attaches to container.
-	ContainerWsAttachWithLogs(name string, cfg *daemon.ContainerWsAttachWithLogsConfig) error
+	Pull(name string) (Image, error)
+	// ContainerAttach attaches to container.
+	ContainerAttach(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error
 	// ContainerCreate creates a new Docker container and returns potential warnings
 	// ContainerCreate creates a new Docker container and returns potential warnings
-	ContainerCreate(params *daemon.ContainerCreateConfig) (types.ContainerCreateResponse, error)
+	ContainerCreate(types.ContainerCreateConfig) (types.ContainerCreateResponse, error)
 	// ContainerRm removes a container specified by `id`.
 	// ContainerRm removes a container specified by `id`.
 	ContainerRm(name string, config *types.ContainerRmConfig) error
 	ContainerRm(name string, config *types.ContainerRmConfig) error
 	// Commit creates a new Docker image from an existing Docker container.
 	// Commit creates a new Docker image from an existing Docker container.

+ 2 - 2
builder/dockerfile/dispatchers.go

@@ -18,8 +18,8 @@ import (
 	"strings"
 	"strings"
 
 
 	"github.com/Sirupsen/logrus"
 	"github.com/Sirupsen/logrus"
+	"github.com/docker/docker/builder"
 	derr "github.com/docker/docker/errors"
 	derr "github.com/docker/docker/errors"
-	"github.com/docker/docker/image"
 	flag "github.com/docker/docker/pkg/mflag"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/nat"
 	"github.com/docker/docker/pkg/nat"
 	"github.com/docker/docker/pkg/signal"
 	"github.com/docker/docker/pkg/signal"
@@ -210,7 +210,7 @@ func from(b *Builder, args []string, attributes map[string]bool, original string
 	}
 	}
 
 
 	var (
 	var (
-		image *image.Image
+		image builder.Image
 		err   error
 		err   error
 	)
 	)
 	// TODO: don't use `name`, instead resolve it to a digest
 	// TODO: don't use `name`, instead resolve it to a digest

+ 6 - 12
builder/dockerfile/internals.go

@@ -23,8 +23,6 @@ import (
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/builder"
 	"github.com/docker/docker/builder"
 	"github.com/docker/docker/builder/dockerfile/parser"
 	"github.com/docker/docker/builder/dockerfile/parser"
-	"github.com/docker/docker/daemon"
-	"github.com/docker/docker/image"
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/httputils"
 	"github.com/docker/docker/pkg/httputils"
 	"github.com/docker/docker/pkg/ioutils"
 	"github.com/docker/docker/pkg/ioutils"
@@ -185,7 +183,7 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalD
 		return nil
 		return nil
 	}
 	}
 
 
-	container, err := b.docker.ContainerCreate(&daemon.ContainerCreateConfig{Config: b.runConfig})
+	container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig})
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
@@ -395,11 +393,11 @@ func containsWildcards(name string) bool {
 	return false
 	return false
 }
 }
 
 
-func (b *Builder) processImageFrom(img *image.Image) error {
-	b.image = img.ID().String()
+func (b *Builder) processImageFrom(img builder.Image) error {
+	b.image = img.ID()
 
 
 	if img.Config != nil {
 	if img.Config != nil {
-		b.runConfig = img.Config
+		b.runConfig = img.Config()
 	}
 	}
 
 
 	// The default path will be blank on Windows (set by HCS)
 	// The default path will be blank on Windows (set by HCS)
@@ -500,7 +498,7 @@ func (b *Builder) create() (string, error) {
 	config := *b.runConfig
 	config := *b.runConfig
 
 
 	// Create the container
 	// Create the container
-	c, err := b.docker.ContainerCreate(&daemon.ContainerCreateConfig{
+	c, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
 		Config:     b.runConfig,
 		Config:     b.runConfig,
 		HostConfig: hostConfig,
 		HostConfig: hostConfig,
 	})
 	})
@@ -528,11 +526,7 @@ func (b *Builder) run(cID string) (err error) {
 	errCh := make(chan error)
 	errCh := make(chan error)
 	if b.Verbose {
 	if b.Verbose {
 		go func() {
 		go func() {
-			errCh <- b.docker.ContainerWsAttachWithLogs(cID, &daemon.ContainerWsAttachWithLogsConfig{
-				OutStream: b.Stdout,
-				ErrStream: b.Stderr,
-				Stream:    true,
-			})
+			errCh <- b.docker.ContainerAttach(cID, nil, b.Stdout, b.Stderr, true)
 		}()
 		}()
 	}
 	}
 
 

+ 9 - 0
builder/image.go

@@ -0,0 +1,9 @@
+package builder
+
+import "github.com/docker/docker/runconfig"
+
+// Image represents a Docker image used by the builder.
+type Image interface {
+	ID() string
+	Config() *runconfig.Config
+}

+ 7 - 14
daemon/create.go

@@ -13,23 +13,15 @@ import (
 	"github.com/opencontainers/runc/libcontainer/label"
 	"github.com/opencontainers/runc/libcontainer/label"
 )
 )
 
 
-// ContainerCreateConfig is the parameter set to ContainerCreate()
-type ContainerCreateConfig struct {
-	Name            string
-	Config          *runconfig.Config
-	HostConfig      *runconfig.HostConfig
-	AdjustCPUShares bool
-}
-
-// ContainerCreate takes configs and creates a container.
-func (daemon *Daemon) ContainerCreate(params *ContainerCreateConfig) (types.ContainerCreateResponse, error) {
+// ContainerCreate creates a container.
+func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig) (types.ContainerCreateResponse, error) {
 	if params.Config == nil {
 	if params.Config == nil {
 		return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
 		return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
 	}
 	}
 
 
 	warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config)
 	warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config)
 	if err != nil {
 	if err != nil {
-		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
+		return types.ContainerCreateResponse{Warnings: warnings}, err
 	}
 	}
 
 
 	if params.HostConfig == nil {
 	if params.HostConfig == nil {
@@ -37,24 +29,25 @@ func (daemon *Daemon) ContainerCreate(params *ContainerCreateConfig) (types.Cont
 	}
 	}
 	err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
 	err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
 	if err != nil {
 	if err != nil {
-		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
+		return types.ContainerCreateResponse{Warnings: warnings}, err
 	}
 	}
 
 
 	container, err := daemon.create(params)
 	container, err := daemon.create(params)
 	if err != nil {
 	if err != nil {
-		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, daemon.imageNotExistToErrcode(err)
+		return types.ContainerCreateResponse{Warnings: warnings}, daemon.imageNotExistToErrcode(err)
 	}
 	}
 
 
 	return types.ContainerCreateResponse{ID: container.ID, Warnings: warnings}, nil
 	return types.ContainerCreateResponse{ID: container.ID, Warnings: warnings}, nil
 }
 }
 
 
 // Create creates a new container from the given configuration with a given name.
 // Create creates a new container from the given configuration with a given name.
-func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *container.Container, retErr error) {
+func (daemon *Daemon) create(params types.ContainerCreateConfig) (*container.Container, error) {
 	var (
 	var (
 		container *container.Container
 		container *container.Container
 		img       *image.Image
 		img       *image.Image
 		imgID     image.ID
 		imgID     image.ID
 		err       error
 		err       error
+		retErr    error
 	)
 	)
 
 
 	if params.Config.Image != "" {
 	if params.Config.Image != "" {

+ 18 - 12
daemon/daemonbuilder/builder.go

@@ -36,7 +36,7 @@ type Docker struct {
 var _ builder.Backend = Docker{}
 var _ builder.Backend = Docker{}
 
 
 // Pull tells Docker to pull image referenced by `name`.
 // Pull tells Docker to pull image referenced by `name`.
-func (d Docker) Pull(name string) (*image.Image, error) {
+func (d Docker) Pull(name string) (builder.Image, error) {
 	ref, err := reference.ParseNamed(name)
 	ref, err := reference.ParseNamed(name)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
@@ -61,8 +61,16 @@ func (d Docker) Pull(name string) (*image.Image, error) {
 	if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(d.OutOld)); err != nil {
 	if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(d.OutOld)); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
+	return d.GetImage(name)
+}
 
 
-	return d.Daemon.GetImage(name)
+// GetImage looks up a Docker image referenced by `name`.
+func (d Docker) GetImage(name string) (builder.Image, error) {
+	img, err := d.Daemon.GetImage(name)
+	if err != nil {
+		return nil, err
+	}
+	return imgWrap{img}, nil
 }
 }
 
 
 // ContainerUpdateCmd updates Path and Args for the container with ID cID.
 // ContainerUpdateCmd updates Path and Args for the container with ID cID.
@@ -76,16 +84,14 @@ func (d Docker) ContainerUpdateCmd(cID string, cmd []string) error {
 	return nil
 	return nil
 }
 }
 
 
-// Retain retains an image avoiding it to be removed or overwritten until a corresponding Release() call.
-func (d Docker) Retain(sessionID, imgID string) {
-	// FIXME: This will be solved with tags in client-side builder
-	//d.Daemon.Graph().Retain(sessionID, imgID)
-}
-
-// Release releases a list of images that were retained for the time of a build.
-func (d Docker) Release(sessionID string, activeImages []string) {
-	// FIXME: This will be solved with tags in client-side builder
-	//d.Daemon.Graph().Release(sessionID, activeImages...)
+// ContainerAttach attaches streams to the container cID. If stream is true, it streams the output.
+func (d Docker) ContainerAttach(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error {
+	return d.Daemon.ContainerWsAttachWithLogs(cID, &daemon.ContainerWsAttachWithLogsConfig{
+		InStream:  stdin,
+		OutStream: stdout,
+		ErrStream: stderr,
+		Stream:    stream,
+	})
 }
 }
 
 
 // BuilderCopy copies/extracts a source FileInfo to a destination path inside a container
 // BuilderCopy copies/extracts a source FileInfo to a destination path inside a container

+ 18 - 0
daemon/daemonbuilder/image.go

@@ -0,0 +1,18 @@
+package daemonbuilder
+
+import (
+	"github.com/docker/docker/image"
+	"github.com/docker/docker/runconfig"
+)
+
+type imgWrap struct {
+	inner *image.Image
+}
+
+func (img imgWrap) ID() string {
+	return string(img.inner.ID())
+}
+
+func (img imgWrap) Config() *runconfig.Config {
+	return img.inner.Config
+}