瀏覽代碼

refactor create to not expose internal data structures

 - use existing exposed type

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
Morgan Bauer 9 年之前
父節點
當前提交
93bd57b0b2
共有 3 個文件被更改,包括 23 次插入19 次删除
  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 {
 	if err := checkForJSON(r); err != nil {
 		return err
 		return err
 	}
 	}
-	var (
-		warnings []string
-		name     = r.Form.Get("name")
-	)
+
+	name := r.Form.Get("name")
 
 
 	config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
 	config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
 	if err != nil {
 	if err != nil {
@@ -378,15 +376,12 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
 	version := ctx.Version()
 	version := ctx.Version()
 	adjustCPUShares := version.LessThan("1.19")
 	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 {
 	if err != nil {
 		return err
 		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 {
 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
 		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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}
+	container, err := b.Daemon.Get(ctx, ccr.ID)
+	if err != nil {
+		return err
+	}
+
 	b.TmpContainers[container.ID] = struct{}{}
 	b.TmpContainers[container.ID] = struct{}{}
 
 
 	if err := container.Mount(ctx); err != nil {
 	if err := container.Mount(ctx); err != nil {
@@ -621,13 +626,17 @@ func (b *builder) create(ctx context.Context) (*daemon.Container, error) {
 	config := *b.Config
 	config := *b.Config
 
 
 	// Create the container
 	// 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 {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	for _, warning := range warnings {
+	for _, warning := range ccr.Warnings {
 		fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
 		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{}{}
 	b.TmpContainers[c.ID] = struct{}{}
 	fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
 	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.
 // 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 {
 	if config == nil {
-		return nil, nil, derr.ErrorCodeEmptyConfig
+		return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
 	}
 	}
 
 
 	warnings, err := daemon.verifyContainerSettings(ctx, hostConfig, config)
 	warnings, err := daemon.verifyContainerSettings(ctx, hostConfig, config)
 	if err != nil {
 	if err != nil {
-		return nil, warnings, err
+		return types.ContainerCreateResponse{"", warnings}, err
 	}
 	}
 
 
 	daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
 	daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
@@ -32,20 +32,20 @@ func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *
 	if err != nil {
 	if err != nil {
 		if daemon.Graph(ctx).IsNotExist(err, config.Image) {
 		if daemon.Graph(ctx).IsNotExist(err, config.Image) {
 			if strings.Contains(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)
 			img, tag := parsers.ParseRepositoryTag(config.Image)
 			if tag == "" {
 			if tag == "" {
 				tag = tags.DefaultTag
 				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...)
 	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.
 // Create creates a new container from the given configuration with a given name.