Fix race (parallel update of collection) to remove orphan containers

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2021-02-12 10:13:14 +01:00
parent fbe1ebc054
commit d720eb6c03
3 changed files with 5 additions and 21 deletions

View File

@ -70,20 +70,6 @@ func (containers Containers) filter(predicate containerPredicate) Containers {
return filtered
}
// split return Containers with elements to match and those not to match predicate
func (containers Containers) split(predicate containerPredicate) (Containers, Containers) {
var right Containers
var left Containers
for _, c := range containers {
if predicate(c) {
right = append(right, c)
} else {
left = append(left, c)
}
}
return right, left
}
func (containers Containers) names() []string {
var names []string
for _, c := range containers {

View File

@ -57,17 +57,17 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
}
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
serviceContainers, others := containers.split(isService(service.Name))
serviceContainers := containers.filter(isService(service.Name))
err := s.removeContainers(ctx, w, serviceContainers)
containers = others
return err
})
if err != nil {
return err
}
if options.RemoveOrphans && len(containers) > 0 {
err := s.removeContainers(ctx, w, containers)
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
if options.RemoveOrphans && len(orphans) > 0 {
err := s.removeContainers(ctx, w, orphans)
if err != nil {
return err
}

View File

@ -64,9 +64,7 @@ func TestDownRemoveOrphans(t *testing.T) {
ctx := context.Background()
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
[]apitypes.Container{testContainer("service1", "123"),
testContainer("service2", "789"),
testContainer("service_orphan", "321")}, nil).Times(2)
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)