瀏覽代碼

add support for COMPOSE_IGNORE_ORPHANS

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 年之前
父節點
當前提交
bc25259f07
共有 4 個文件被更改,包括 11 次插入1 次删除
  1. 2 0
      cmd/compose/create.go
  2. 6 0
      cmd/compose/up.go
  3. 2 0
      pkg/api/api.go
  4. 1 1
      pkg/compose/create.go

+ 2 - 0
cmd/compose/create.go

@@ -31,6 +31,7 @@ type createOptions struct {
 	Build         bool
 	noBuild       bool
 	removeOrphans bool
+	ignoreOrphans bool
 	forceRecreate bool
 	noRecreate    bool
 	recreateDeps  bool
@@ -57,6 +58,7 @@ func createCommand(p *projectOptions, backend api.Service) *cobra.Command {
 		RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
 			return backend.Create(ctx, project, api.CreateOptions{
 				RemoveOrphans:        opts.removeOrphans,
+				IgnoreOrphans:        opts.ignoreOrphans,
 				Recreate:             opts.recreateStrategy(),
 				RecreateDependencies: opts.dependenciesRecreateStrategy(),
 				Inherit:              !opts.noInherit,

+ 6 - 0
cmd/compose/up.go

@@ -120,6 +120,11 @@ func upCommand(p *projectOptions, backend api.Service) *cobra.Command {
 			return nil
 		}),
 		RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
+			ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"]
+			create.ignoreOrphans = strings.ToLower(ignore) == "true"
+			if create.ignoreOrphans && create.removeOrphans {
+				return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined")
+			}
 			return runUp(ctx, backend, create, up, project, services)
 		}),
 		ValidArgsFunction: serviceCompletion(p),
@@ -177,6 +182,7 @@ func runUp(ctx context.Context, backend api.Service, createOptions createOptions
 	create := api.CreateOptions{
 		Services:             services,
 		RemoveOrphans:        createOptions.removeOrphans,
+		IgnoreOrphans:        createOptions.ignoreOrphans,
 		Recreate:             createOptions.recreateStrategy(),
 		RecreateDependencies: createOptions.dependenciesRecreateStrategy(),
 		Inherit:              !createOptions.noInherit,

+ 2 - 0
pkg/api/api.go

@@ -100,6 +100,8 @@ type CreateOptions struct {
 	Services []string
 	// Remove legacy containers for services that are not defined in the project
 	RemoveOrphans bool
+	// Ignore legacy containers for services that are not defined in the project
+	IgnoreOrphans bool
 	// Recreate define the strategy to apply on existing containers
 	Recreate string
 	// RecreateDependencies define the strategy to apply on dependencies services

+ 1 - 1
pkg/compose/create.go

@@ -86,7 +86,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
 		allServiceNames = append(allServiceNames, service.Name)
 	}
 	orphans := observedState.filter(isNotService(allServiceNames...))
-	if len(orphans) > 0 {
+	if len(orphans) > 0 && !options.IgnoreOrphans {
 		if options.RemoveOrphans {
 			w := progress.ContextWriter(ctx)
 			err := s.removeContainers(ctx, w, orphans, nil, false)