mirror of
https://github.com/docker/compose.git
synced 2025-07-22 21:24:38 +02:00
Introduce removeOrphans
to cleanup injected AWS simulation container
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
db5467ce22
commit
855a879a6a
@ -102,14 +102,14 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciComposeService) Down(ctx context.Context, project string) error {
|
func (cs *aciComposeService) Down(ctx context.Context, projectName string, removeOrphans bool) error {
|
||||||
logrus.Debugf("Down on project with name %q", project)
|
logrus.Debugf("Down on projectName with name %q", projectName)
|
||||||
|
|
||||||
if err := cs.warnKeepVolumeOnDown(ctx, project); err != nil {
|
if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cg, err := deleteACIContainerGroup(ctx, cs.ctx, project)
|
cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ func (c *composeService) Up(context.Context, *types.Project, bool) error {
|
|||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *composeService) Down(context.Context, string) error {
|
func (c *composeService) Down(context.Context, string, bool) error {
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ type Service interface {
|
|||||||
// Up executes the equivalent to a `compose up`
|
// Up executes the equivalent to a `compose up`
|
||||||
Up(ctx context.Context, project *types.Project, detach bool) error
|
Up(ctx context.Context, project *types.Project, detach bool) error
|
||||||
// Down executes the equivalent to a `compose down`
|
// Down executes the equivalent to a `compose down`
|
||||||
Down(ctx context.Context, projectName string) error
|
Down(ctx context.Context, projectName string, removeOrphans bool) error
|
||||||
// Logs executes the equivalent to a `compose logs`
|
// Logs executes the equivalent to a `compose logs`
|
||||||
Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
|
Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
|
||||||
// Ps executes the equivalent to a `compose ps`
|
// Ps executes the equivalent to a `compose ps`
|
||||||
|
@ -52,7 +52,7 @@ func runDown(ctx context.Context, opts composeOptions) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return projectName, c.ComposeService().Down(ctx, projectName)
|
return projectName, c.ComposeService().Down(ctx, projectName, false)
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string)
|
|||||||
fmt.Println("Gracefully stopping...")
|
fmt.Println("Gracefully stopping...")
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||||
return "", c.ComposeService().Down(ctx, project.Name)
|
return "", c.ComposeService().Down(ctx, project.Name, false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
10
ecs/down.go
10
ecs/down.go
@ -22,8 +22,8 @@ import (
|
|||||||
"github.com/docker/compose-cli/progress"
|
"github.com/docker/compose-cli/progress"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *ecsAPIService) Down(ctx context.Context, project string) error {
|
func (b *ecsAPIService) Down(ctx context.Context, projectName string, removeOrphans bool) error {
|
||||||
resources, err := b.aws.ListStackResources(ctx, project)
|
resources, err := b.aws.ListStackResources(ctx, projectName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -38,16 +38,16 @@ func (b *ecsAPIService) Down(ctx context.Context, project string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
previousEvents, err := b.previousStackEvents(ctx, project)
|
previousEvents, err := b.previousStackEvents(ctx, projectName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.aws.DeleteStack(ctx, project)
|
err = b.aws.DeleteStack(ctx, projectName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return b.WaitStackCompletion(ctx, project, stackDelete, previousEvents...)
|
return b.WaitStackCompletion(ctx, projectName, stackDelete, previousEvents...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ecsAPIService) previousStackEvents(ctx context.Context, project string) ([]string, error) {
|
func (b *ecsAPIService) previousStackEvents(ctx context.Context, project string) ([]string, error) {
|
||||||
|
@ -147,8 +147,8 @@ func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (*
|
|||||||
return project, nil
|
return project, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ecsLocalSimulation) Down(ctx context.Context, projectName string) error {
|
func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, removeOrphans bool) error {
|
||||||
return e.compose.Down(ctx, projectName)
|
return e.compose.Down(ctx, projectName, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options componse.LogOptions) error {
|
func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options componse.LogOptions) error {
|
||||||
|
@ -90,7 +90,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
|
|||||||
go func() {
|
go func() {
|
||||||
<-signalChan
|
<-signalChan
|
||||||
fmt.Println("user interrupted deployment. Deleting stack...")
|
fmt.Println("user interrupted deployment. Deleting stack...")
|
||||||
b.Down(ctx, project.Name) // nolint:errcheck
|
b.Down(ctx, project.Name, false) // nolint:errcheck
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = b.WaitStackCompletion(ctx, project.Name, operation)
|
err = b.WaitStackCompletion(ctx, project.Name, operation)
|
||||||
|
@ -164,8 +164,8 @@ func (cs *composeService) Up(ctx context.Context, project *types.Project, detach
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *composeService) Down(ctx context.Context, project string) error {
|
func (cs *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error {
|
||||||
fmt.Printf("Down command on project %q", project)
|
fmt.Printf("Down command on project %q", projectName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
go.mod
1
go.mod
@ -51,7 +51,6 @@ require (
|
|||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/stretchr/testify v1.6.1
|
github.com/stretchr/testify v1.6.1
|
||||||
github.com/valyala/fasttemplate v1.2.1 // indirect
|
github.com/valyala/fasttemplate v1.2.1 // indirect
|
||||||
golang.org/x/mod v0.3.0
|
|
||||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
|
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
|
||||||
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
|
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
||||||
|
@ -30,7 +30,7 @@ import (
|
|||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) Down(ctx context.Context, projectName string) error {
|
func (s *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error {
|
||||||
eg, _ := errgroup.WithContext(ctx)
|
eg, _ := errgroup.WithContext(ctx)
|
||||||
w := progress.ContextWriter(ctx)
|
w := progress.ContextWriter(ctx)
|
||||||
|
|
||||||
@ -39,10 +39,27 @@ func (s *composeService) Down(ctx context.Context, projectName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
filter := filters.NewArgs(projectFilter(project.Name), serviceFilter(service.Name))
|
Filters: filters.NewArgs(projectFilter(project.Name)),
|
||||||
return s.removeContainers(ctx, w, eg, filter)
|
All: true,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
||||||
|
serviceContainers, others := split(containers, isService(service.Name))
|
||||||
|
err := s.removeContainers(ctx, w, eg, serviceContainers)
|
||||||
|
containers = others
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
if removeOrphans {
|
||||||
|
err := s.removeContainers(ctx, w, eg, containers)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -70,14 +87,7 @@ func (s *composeService) Down(ctx context.Context, projectName string) error {
|
|||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, eg *errgroup.Group, filter filters.Args) error {
|
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, eg *errgroup.Group, containers []moby.Container) error {
|
||||||
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
|
||||||
Filters: filter,
|
|
||||||
All: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
eventName := "Container " + getContainerName(container)
|
eventName := "Container " + getContainerName(container)
|
||||||
@ -147,3 +157,25 @@ func loadProjectOptionsFromLabels(c moby.Container) (*cli.ProjectOptions, error)
|
|||||||
cli.WithWorkingDirectory(c.Labels[workingDirLabel]),
|
cli.WithWorkingDirectory(c.Labels[workingDirLabel]),
|
||||||
cli.WithName(c.Labels[projectLabel]))
|
cli.WithName(c.Labels[projectLabel]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type containerPredicate func(c moby.Container) bool
|
||||||
|
|
||||||
|
func isService(service string) containerPredicate {
|
||||||
|
return func(c moby.Container) bool {
|
||||||
|
return c.Labels[serviceLabel] == service
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// split return a container slice with elements to match predicate
|
||||||
|
func split(containers []moby.Container, predicate containerPredicate) ([]moby.Container, []moby.Container) {
|
||||||
|
var right []moby.Container
|
||||||
|
var left []moby.Container
|
||||||
|
for _, c := range containers {
|
||||||
|
if predicate(c) {
|
||||||
|
right = append(right, c)
|
||||||
|
} else {
|
||||||
|
left = append(left, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return right, left
|
||||||
|
}
|
||||||
|
@ -42,7 +42,7 @@ func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest)
|
|||||||
}
|
}
|
||||||
projectName = project.Name
|
projectName = project.Name
|
||||||
}
|
}
|
||||||
return &composev1.ComposeDownResponse{ProjectName: projectName}, Client(ctx).ComposeService().Down(ctx, projectName)
|
return &composev1.ComposeDownResponse{ProjectName: projectName}, Client(ctx).ComposeService().Down(ctx, projectName, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServicesRequest) (*composev1.ComposeServicesResponse, error) {
|
func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServicesRequest) (*composev1.ComposeServicesResponse, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user