소스 검색

introduce --force-recreate and --no-recreate

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 년 전
부모
커밋
9e77499c2a
4개의 변경된 파일41개의 추가작업 그리고 4개의 파일을 삭제
  1. 11 0
      api/compose/api.go
  2. 22 1
      cli/cmd/compose/up.go
  3. 7 2
      local/compose/convergence.go
  4. 1 1
      local/compose/create.go

+ 11 - 0
api/compose/api.go

@@ -57,6 +57,8 @@ type Service interface {
 type CreateOptions struct {
 	// Remove legacy containers for services that are not defined in the project
 	RemoveOrphans bool
+	// Recreate define the strategy to apply on existing containers
+	Recreate string
 }
 
 // UpOptions group options of the Up API
@@ -137,6 +139,15 @@ const (
 	FAILED string = "Failed"
 )
 
+const (
+	// RecreateDiverged to recreate services which configuration diverges from compose model
+	RecreateDiverged = "diverged"
+	// RecreateForce to force service container being recreated
+	RecreateForce = "force"
+	// RecreateNever to never recreate existing service containers
+	RecreateNever = "never"
+)
+
 // Stack holds the name and state of a compose application/stack
 type Stack struct {
 	ID     string

+ 22 - 1
cli/cmd/compose/up.go

@@ -45,6 +45,18 @@ type upOptions struct {
 	Detach        bool
 	Environment   []string
 	removeOrphans bool
+	forceRecreate bool
+	noRecreate    bool
+}
+
+func (o upOptions) recreateStrategy() string {
+	if o.noRecreate {
+		return compose.RecreateNever
+	}
+	if o.forceRecreate {
+		return compose.RecreateForce
+	}
+	return compose.RecreateDiverged
 }
 
 func upCommand(p *projectOptions, contextType string) *cobra.Command {
@@ -59,6 +71,9 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 		RunE: func(cmd *cobra.Command, args []string) error {
 			switch contextType {
 			case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
+				if opts.forceRecreate && opts.noRecreate {
+					return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
+				}
 				return runCreateStart(cmd.Context(), opts, args)
 			default:
 				return runUp(cmd.Context(), opts, args)
@@ -71,8 +86,13 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 	flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
 	flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
 
-	if contextType == store.AciContextType {
+	switch contextType {
+	case store.AciContextType:
 		flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
+	case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
+		flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
+		flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
+
 	}
 
 	return upCmd
@@ -101,6 +121,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
 	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
 		return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
 			RemoveOrphans: opts.removeOrphans,
+			Recreate:      opts.recreateStrategy(),
 		})
 	})
 	if err != nil {

+ 7 - 2
local/compose/convergence.go

@@ -28,6 +28,7 @@ import (
 	"github.com/docker/docker/api/types/network"
 	"golang.org/x/sync/errgroup"
 
+	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/progress"
 	status "github.com/docker/compose-cli/local/moby"
 )
@@ -74,7 +75,7 @@ func (s *composeService) ensureScale(ctx context.Context, actual []moby.Containe
 	return eg, actual, nil
 }
 
-func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig) error {
+func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig, recreate string) error {
 	actual := observedState.filter(isService(service.Name))
 
 	scale, err := getScale(service)
@@ -87,6 +88,10 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
 		return err
 	}
 
+	if recreate == compose.RecreateNever {
+		return nil
+	}
+
 	expected, err := jsonHash(service)
 	if err != nil {
 		return err
@@ -96,7 +101,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
 		name := getCanonicalContainerName(container)
 
 		diverged := container.Labels[configHashLabel] != expected
-		if diverged || service.Extensions[extLifecycle] == forceRecreate {
+		if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
 			eg.Go(func() error {
 				return s.recreateContainer(ctx, project, service, container)
 			})

+ 1 - 1
local/compose/create.go

@@ -95,7 +95,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
 	}
 
 	return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
-		return s.ensureService(c, observedState, project, service)
+		return s.ensureService(c, observedState, project, service, opts.Recreate)
 	})
 }