浏览代码

introduce stopAndRemoveContainer to share logic scaling down

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 1 年之前
父节点
当前提交
1551fcb4e6
共有 3 个文件被更改,包括 26 次插入29 次删除
  1. 1 8
      pkg/compose/convergence.go
  2. 1 2
      pkg/compose/create.go
  3. 24 19
      pkg/compose/down.go

+ 1 - 8
pkg/compose/convergence.go

@@ -134,14 +134,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
 			container := container
 			traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
 			eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
-				timeoutInSecond := utils.DurationSecondToInt(timeout)
-				err := c.service.apiClient().ContainerStop(ctx, container.ID, containerType.StopOptions{
-					Timeout: timeoutInSecond,
-				})
-				if err != nil {
-					return err
-				}
-				return c.service.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{})
+				return c.service.stopAndRemoveContainer(ctx, container, timeout, false)
 			}))
 			continue
 		}

+ 1 - 2
pkg/compose/create.go

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

+ 24 - 19
pkg/compose/down.go

@@ -76,7 +76,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
 
 	err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
 		serviceContainers := containers.filter(isService(service))
-		err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes)
+		err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes)
 		return err
 	}, WithRootNodesAndDown(options.Services))
 	if err != nil {
@@ -85,7 +85,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
 
 	orphans := containers.filter(isOrphaned(project))
 	if options.RemoveOrphans && len(orphans) > 0 {
-		err := s.removeContainers(ctx, w, orphans, options.Timeout, false)
+		err := s.removeContainers(ctx, orphans, options.Timeout, false)
 		if err != nil {
 			return err
 		}
@@ -303,32 +303,37 @@ func (s *composeService) stopContainers(ctx context.Context, w progress.Writer,
 	return eg.Wait()
 }
 
-func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration, volumes bool) error {
+func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, timeout *time.Duration, volumes bool) error {
 	eg, _ := errgroup.WithContext(ctx)
 	for _, container := range containers {
 		container := container
 		eg.Go(func() error {
-			eventName := getContainerProgressName(container)
-			err := s.stopContainer(ctx, w, container, timeout)
-			if err != nil {
-				return err
-			}
-			w.Event(progress.RemovingEvent(eventName))
-			err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{
-				Force:         true,
-				RemoveVolumes: volumes,
-			})
-			if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) {
-				w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
-				return err
-			}
-			w.Event(progress.RemovedEvent(eventName))
-			return nil
+			return s.stopAndRemoveContainer(ctx, container, timeout, volumes)
 		})
 	}
 	return eg.Wait()
 }
 
+func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, timeout *time.Duration, volumes bool) error {
+	w := progress.ContextWriter(ctx)
+	eventName := getContainerProgressName(container)
+	err := s.stopContainer(ctx, w, container, timeout)
+	if err != nil {
+		return err
+	}
+	w.Event(progress.RemovingEvent(eventName))
+	err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{
+		Force:         true,
+		RemoveVolumes: volumes,
+	})
+	if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) {
+		w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
+		return err
+	}
+	w.Event(progress.RemovedEvent(eventName))
+	return nil
+}
+
 func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
 	containers = containers.filter(isNotOneOff)
 	project, err := s.projectFromName(containers, projectName)