Skip to content

Commit 1551fcb

Browse files
committed
introduce stopAndRemoveContainer to share logic scaling down
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 143ac0f commit 1551fcb

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

pkg/compose/convergence.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
134134
container := container
135135
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
136136
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
137-
timeoutInSecond := utils.DurationSecondToInt(timeout)
138-
err := c.service.apiClient().ContainerStop(ctx, container.ID, containerType.StopOptions{
139-
Timeout: timeoutInSecond,
140-
})
141-
if err != nil {
142-
return err
143-
}
144-
return c.service.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{})
137+
return c.service.stopAndRemoveContainer(ctx, container, timeout, false)
145138
}))
146139
continue
147140
}

pkg/compose/create.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
9999
orphans := observedState.filter(isNotService(allServiceNames...))
100100
if len(orphans) > 0 && !options.IgnoreOrphans {
101101
if options.RemoveOrphans {
102-
w := progress.ContextWriter(ctx)
103-
err := s.removeContainers(ctx, w, orphans, nil, false)
102+
err := s.removeContainers(ctx, orphans, nil, false)
104103
if err != nil {
105104
return err
106105
}

pkg/compose/down.go

+24-19
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
7676

7777
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
7878
serviceContainers := containers.filter(isService(service))
79-
err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes)
79+
err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes)
8080
return err
8181
}, WithRootNodesAndDown(options.Services))
8282
if err != nil {
@@ -85,7 +85,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
8585

8686
orphans := containers.filter(isOrphaned(project))
8787
if options.RemoveOrphans && len(orphans) > 0 {
88-
err := s.removeContainers(ctx, w, orphans, options.Timeout, false)
88+
err := s.removeContainers(ctx, orphans, options.Timeout, false)
8989
if err != nil {
9090
return err
9191
}
@@ -303,32 +303,37 @@ func (s *composeService) stopContainers(ctx context.Context, w progress.Writer,
303303
return eg.Wait()
304304
}
305305

306-
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration, volumes bool) error {
306+
func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, timeout *time.Duration, volumes bool) error {
307307
eg, _ := errgroup.WithContext(ctx)
308308
for _, container := range containers {
309309
container := container
310310
eg.Go(func() error {
311-
eventName := getContainerProgressName(container)
312-
err := s.stopContainer(ctx, w, container, timeout)
313-
if err != nil {
314-
return err
315-
}
316-
w.Event(progress.RemovingEvent(eventName))
317-
err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{
318-
Force: true,
319-
RemoveVolumes: volumes,
320-
})
321-
if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) {
322-
w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
323-
return err
324-
}
325-
w.Event(progress.RemovedEvent(eventName))
326-
return nil
311+
return s.stopAndRemoveContainer(ctx, container, timeout, volumes)
327312
})
328313
}
329314
return eg.Wait()
330315
}
331316

317+
func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, timeout *time.Duration, volumes bool) error {
318+
w := progress.ContextWriter(ctx)
319+
eventName := getContainerProgressName(container)
320+
err := s.stopContainer(ctx, w, container, timeout)
321+
if err != nil {
322+
return err
323+
}
324+
w.Event(progress.RemovingEvent(eventName))
325+
err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{
326+
Force: true,
327+
RemoveVolumes: volumes,
328+
})
329+
if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) {
330+
w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing"))
331+
return err
332+
}
333+
w.Event(progress.RemovedEvent(eventName))
334+
return nil
335+
}
336+
332337
func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
333338
containers = containers.filter(isNotOneOff)
334339
project, err := s.projectFromName(containers, projectName)

0 commit comments

Comments
 (0)