From 03a8188a9ad0f6cd618e1b3a38032dd24a4e4b3a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 5 Jun 2022 21:45:32 +0200 Subject: [PATCH] builder/dockerfile: remove containerManager.removeContainer() This was just a very thin wrapper for backend.ContainerRm(), and the error it returned was not handled, so moving this code inline. Moving it inline also allows differentiating the error message to distinguish the "removing all intermediate containers" from "removing container" (when cancelling a build). Signed-off-by: Sebastiaan van Stijn --- builder/dockerfile/containerbackend.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/builder/dockerfile/containerbackend.go b/builder/dockerfile/containerbackend.go index a4a8b08569..6fe5af7606 100644 --- a/builder/dockerfile/containerbackend.go +++ b/builder/dockerfile/containerbackend.go @@ -62,7 +62,10 @@ func (c *containerManager) Run(ctx context.Context, cID string, stdout, stderr i case <-ctx.Done(): log.G(ctx).Debugln("Build cancelled, killing and removing container:", cID) c.backend.ContainerKill(cID, "") - c.removeContainer(cID, stdout) + err = c.backend.ContainerRm(cID, &backend.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}) + if err != nil { + _, _ = fmt.Fprintf(stdout, "Removing container %s: %v\n", stringid.TruncateID(cID), err) + } cancelErrCh <- errCancelled case <-finished: cancelErrCh <- nil @@ -122,22 +125,11 @@ func (e *statusCodeError) StatusCode() int { return e.code } -func (c *containerManager) removeContainer(containerID string, stdout io.Writer) error { - rmConfig := &backend.ContainerRmConfig{ - ForceRemove: true, - RemoveVolume: true, - } - if err := c.backend.ContainerRm(containerID, rmConfig); err != nil { - fmt.Fprintf(stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(containerID), err) - return err - } - return nil -} - // RemoveAll containers managed by this container manager func (c *containerManager) RemoveAll(stdout io.Writer) { for containerID := range c.tmpContainers { - if err := c.removeContainer(containerID, stdout); err != nil { + if err := c.backend.ContainerRm(containerID, &backend.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil { + _, _ = fmt.Fprintf(stdout, "Removing intermediate container %s: %v\n", stringid.TruncateID(containerID), err) return } delete(c.tmpContainers, containerID)