add support for COMPOSE_IGNORE_ORPHANS

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-24 08:22:50 +02:00
parent 99e75639d6
commit bc25259f07
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
4 changed files with 11 additions and 1 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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)