Bläddra i källkod

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

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 4 år sedan
förälder
incheckning
d720eb6c03
3 ändrade filer med 5 tillägg och 21 borttagningar
  1. 0 14
      local/compose/containers.go
  2. 4 4
      local/compose/down.go
  3. 1 3
      local/compose/down_test.go

+ 0 - 14
local/compose/containers.go

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

+ 4 - 4
local/compose/down.go

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

+ 1 - 3
local/compose/down_test.go

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