diff --git a/daemon/containerd/image_commit.go b/daemon/containerd/image_commit.go index eb0b424104..e2d1f6e87a 100644 --- a/daemon/containerd/image_commit.go +++ b/daemon/containerd/image_commit.go @@ -20,6 +20,7 @@ import ( "github.com/moby/go-archive" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/daemon/server/backend" + "github.com/moby/moby/v2/errdefs" "github.com/opencontainers/go-digest" "github.com/opencontainers/image-spec/identity" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -320,8 +321,7 @@ func cleanup(ctx context.Context, do func(context.Context)) { func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) { ctr := i.containers.Get(c.ContainerID) if ctr == nil { - // TODO: use typed error - return "", fmt.Errorf("container not found: %s", c.ContainerID) + return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID)) } c.ContainerMountLabel = ctr.MountLabel c.ContainerOS = ctr.ImagePlatform.OS diff --git a/daemon/daemon_unix_test.go b/daemon/daemon_unix_test.go index 0c043879a8..b4c739023c 100644 --- a/daemon/daemon_unix_test.go +++ b/daemon/daemon_unix_test.go @@ -12,6 +12,7 @@ import ( containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/v2/daemon/config" "github.com/moby/moby/v2/daemon/container" + "github.com/moby/moby/v2/errdefs" "github.com/moby/moby/v2/pkg/sysinfo" "github.com/opencontainers/selinux/go-selinux" "golang.org/x/sys/unix" @@ -26,7 +27,7 @@ type fakeContainerGetter struct { func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) { ctr, ok := f.containers[cid] if !ok { - return nil, errors.New("container not found") + return nil, errdefs.NotFound(errors.New("container not found")) } return ctr, nil } diff --git a/daemon/errors.go b/daemon/errors.go index 78026ea215..a7bb1c09b4 100644 --- a/daemon/errors.go +++ b/daemon/errors.go @@ -54,11 +54,6 @@ func errExecPaused(id string) error { return errdefs.Conflict(cause) } -func errNotPaused(id string) error { - cause := errors.Errorf("Container %s is already paused", id) - return errdefs.Conflict(cause) -} - type nameConflictError struct { id string name string diff --git a/daemon/images/image_commit.go b/daemon/images/image_commit.go index e3101fc374..32e2b2e99c 100644 --- a/daemon/images/image_commit.go +++ b/daemon/images/image_commit.go @@ -3,14 +3,15 @@ package images import ( "context" "encoding/json" + "fmt" "io" "github.com/moby/moby/api/types/events" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/daemon/internal/layer" "github.com/moby/moby/v2/daemon/server/backend" + "github.com/moby/moby/v2/errdefs" "github.com/moby/moby/v2/pkg/ioutils" - "github.com/pkg/errors" ) // CommitImage creates a new image from a commit config @@ -122,8 +123,7 @@ func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.R func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) { ctr := i.containers.Get(c.ContainerID) if ctr == nil { - // TODO: use typed error - return "", errors.Errorf("container not found: %s", c.ContainerID) + return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID)) } c.ContainerMountLabel = ctr.MountLabel c.ContainerOS = ctr.ImagePlatform.OS diff --git a/daemon/pause.go b/daemon/pause.go index db65f80530..d84bdaae67 100644 --- a/daemon/pause.go +++ b/daemon/pause.go @@ -7,6 +7,7 @@ import ( "github.com/containerd/log" "github.com/moby/moby/api/types/events" "github.com/moby/moby/v2/daemon/container" + "github.com/moby/moby/v2/errdefs" ) // ContainerPause pauses a container @@ -32,7 +33,7 @@ func (daemon *Daemon) containerPause(container *container.Container) error { // We cannot Pause the container which is already paused if container.State.Paused { - return errNotPaused(container.ID) + return errdefs.Conflict(fmt.Errorf("container %s is already paused", container.ID)) } // We cannot Pause the container which is restarting diff --git a/daemon/stats/collector.go b/daemon/stats/collector.go index 5450f7cecf..0735d17c03 100644 --- a/daemon/stats/collector.go +++ b/daemon/stats/collector.go @@ -95,9 +95,12 @@ func (s *Collector) Run() { // but saves allocations in further iterations pairs = pairs[:0] - for container, publisher := range s.publishers { + for ctr, publisher := range s.publishers { // copy pointers here to release the lock ASAP - pairs = append(pairs, publishersPair{container, publisher}) + pairs = append(pairs, publishersPair{ + container: ctr, + publisher: publisher, + }) } s.cond.L.Unlock() diff --git a/daemon/stats_unix.go b/daemon/stats_unix.go index 5d2abd43bf..68bd8ca864 100644 --- a/daemon/stats_unix.go +++ b/daemon/stats_unix.go @@ -13,6 +13,7 @@ import ( statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats" statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats" + cerrdefs "github.com/containerd/errdefs" containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/v2/daemon/container" "github.com/pkg/errors" @@ -40,7 +41,7 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon } cs, err := task.Stats(context.Background()) if err != nil { - if strings.Contains(err.Error(), "container not found") { + if cerrdefs.IsNotFound(err) || strings.Contains(err.Error(), "container not found") { return nil, containerNotFound(c.ID) } return nil, err