Skip to content

Commit

Permalink
introduce stopAndRemoveContainer to share logic scaling down
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Jan 23, 2024
1 parent 143ac0f commit 1551fcb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
9 changes: 1 addition & 8 deletions pkg/compose/convergence.go
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/compose/create.go
Expand Up @@ -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
}
Expand Down
43 changes: 24 additions & 19 deletions pkg/compose/down.go
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1551fcb

Please sign in to comment.