Browse Source

Merge pull request #16508 from MHBauer/create-refactor

refactor create to not expose internal data structures
Alexandre Beslic 9 năm trước cách đây
mục cha
commit
07cfac3d2d
3 tập tin đã thay đổi với 23 bổ sung19 xóa
  1. 4 9
      api/server/container.go
  2. 12 3
      builder/internals.go
  3. 7 7
      daemon/create.go

+ 4 - 9
api/server/container.go

@@ -366,10 +366,8 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
 	if err := checkForJSON(r); err != nil {
 		return err
 	}
-	var (
-		warnings []string
-		name     = r.Form.Get("name")
-	)
+
+	name := r.Form.Get("name")
 
 	config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
 	if err != nil {
@@ -378,15 +376,12 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
 	version := ctx.Version()
 	adjustCPUShares := version.LessThan("1.19")
 
-	container, warnings, err := s.daemon.ContainerCreate(ctx, name, config, hostConfig, adjustCPUShares)
+	ccr, err := s.daemon.ContainerCreate(ctx, name, config, hostConfig, adjustCPUShares)
 	if err != nil {
 		return err
 	}
 
-	return writeJSON(w, http.StatusCreated, &types.ContainerCreateResponse{
-		ID:       container.ID,
-		Warnings: warnings,
-	})
+	return writeJSON(w, http.StatusCreated, ccr)
 }
 
 func (s *Server) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

+ 12 - 3
builder/internals.go

@@ -233,10 +233,15 @@ func (b *builder) runContextCommand(ctx context.Context, args []string, allowRem
 		return nil
 	}
 
-	container, _, err := b.Daemon.ContainerCreate(ctx, "", b.Config, nil, true)
+	ccr, err := b.Daemon.ContainerCreate(ctx, "", b.Config, nil, true)
 	if err != nil {
 		return err
 	}
+	container, err := b.Daemon.Get(ctx, ccr.ID)
+	if err != nil {
+		return err
+	}
+
 	b.TmpContainers[container.ID] = struct{}{}
 
 	if err := container.Mount(ctx); err != nil {
@@ -621,13 +626,17 @@ func (b *builder) create(ctx context.Context) (*daemon.Container, error) {
 	config := *b.Config
 
 	// Create the container
-	c, warnings, err := b.Daemon.ContainerCreate(ctx, "", b.Config, hostConfig, true)
+	ccr, err := b.Daemon.ContainerCreate(ctx, "", b.Config, hostConfig, true)
 	if err != nil {
 		return nil, err
 	}
-	for _, warning := range warnings {
+	for _, warning := range ccr.Warnings {
 		fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
 	}
+	c, err := b.Daemon.Get(ctx, ccr.ID)
+	if err != nil {
+		return nil, err
+	}
 
 	b.TmpContainers[c.ID] = struct{}{}
 	fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))

+ 7 - 7
daemon/create.go

@@ -16,14 +16,14 @@ import (
 )
 
 // ContainerCreate takes configs and creates a container.
-func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (*Container, []string, error) {
+func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (types.ContainerCreateResponse, error) {
 	if config == nil {
-		return nil, nil, derr.ErrorCodeEmptyConfig
+		return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
 	}
 
 	warnings, err := daemon.verifyContainerSettings(ctx, hostConfig, config)
 	if err != nil {
-		return nil, warnings, err
+		return types.ContainerCreateResponse{"", warnings}, err
 	}
 
 	daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
@@ -32,20 +32,20 @@ func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *
 	if err != nil {
 		if daemon.Graph(ctx).IsNotExist(err, config.Image) {
 			if strings.Contains(config.Image, "@") {
-				return nil, warnings, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image)
+				return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image)
 			}
 			img, tag := parsers.ParseRepositoryTag(config.Image)
 			if tag == "" {
 				tag = tags.DefaultTag
 			}
-			return nil, warnings, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag)
+			return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag)
 		}
-		return nil, warnings, err
+		return types.ContainerCreateResponse{"", warnings}, err
 	}
 
 	warnings = append(warnings, buildWarnings...)
 
-	return container, warnings, nil
+	return types.ContainerCreateResponse{container.ID, warnings}, nil
 }
 
 // Create creates a new container from the given configuration with a given name.