Browse code

daemon: use errdefs instead of string-matching in some places

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/10/25 23:13:30
Showing 7 changed files
... ...
@@ -20,6 +20,7 @@ import (
20 20
 	"github.com/moby/go-archive"
21 21
 	"github.com/moby/moby/v2/daemon/internal/image"
22 22
 	"github.com/moby/moby/v2/daemon/server/backend"
23
+	"github.com/moby/moby/v2/errdefs"
23 24
 	"github.com/opencontainers/go-digest"
24 25
 	"github.com/opencontainers/image-spec/identity"
25 26
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
... ...
@@ -320,8 +321,7 @@ func cleanup(ctx context.Context, do func(context.Context)) {
320 320
 func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
321 321
 	ctr := i.containers.Get(c.ContainerID)
322 322
 	if ctr == nil {
323
-		// TODO: use typed error
324
-		return "", fmt.Errorf("container not found: %s", c.ContainerID)
323
+		return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
325 324
 	}
326 325
 	c.ContainerMountLabel = ctr.MountLabel
327 326
 	c.ContainerOS = ctr.ImagePlatform.OS
... ...
@@ -12,6 +12,7 @@ import (
12 12
 	containertypes "github.com/moby/moby/api/types/container"
13 13
 	"github.com/moby/moby/v2/daemon/config"
14 14
 	"github.com/moby/moby/v2/daemon/container"
15
+	"github.com/moby/moby/v2/errdefs"
15 16
 	"github.com/moby/moby/v2/pkg/sysinfo"
16 17
 	"github.com/opencontainers/selinux/go-selinux"
17 18
 	"golang.org/x/sys/unix"
... ...
@@ -26,7 +27,7 @@ type fakeContainerGetter struct {
26 26
 func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
27 27
 	ctr, ok := f.containers[cid]
28 28
 	if !ok {
29
-		return nil, errors.New("container not found")
29
+		return nil, errdefs.NotFound(errors.New("container not found"))
30 30
 	}
31 31
 	return ctr, nil
32 32
 }
... ...
@@ -54,11 +54,6 @@ func errExecPaused(id string) error {
54 54
 	return errdefs.Conflict(cause)
55 55
 }
56 56
 
57
-func errNotPaused(id string) error {
58
-	cause := errors.Errorf("Container %s is already paused", id)
59
-	return errdefs.Conflict(cause)
60
-}
61
-
62 57
 type nameConflictError struct {
63 58
 	id   string
64 59
 	name string
... ...
@@ -3,14 +3,15 @@ package images
3 3
 import (
4 4
 	"context"
5 5
 	"encoding/json"
6
+	"fmt"
6 7
 	"io"
7 8
 
8 9
 	"github.com/moby/moby/api/types/events"
9 10
 	"github.com/moby/moby/v2/daemon/internal/image"
10 11
 	"github.com/moby/moby/v2/daemon/internal/layer"
11 12
 	"github.com/moby/moby/v2/daemon/server/backend"
13
+	"github.com/moby/moby/v2/errdefs"
12 14
 	"github.com/moby/moby/v2/pkg/ioutils"
13
-	"github.com/pkg/errors"
14 15
 )
15 16
 
16 17
 // CommitImage creates a new image from a commit config
... ...
@@ -122,8 +123,7 @@ func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.R
122 122
 func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
123 123
 	ctr := i.containers.Get(c.ContainerID)
124 124
 	if ctr == nil {
125
-		// TODO: use typed error
126
-		return "", errors.Errorf("container not found: %s", c.ContainerID)
125
+		return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
127 126
 	}
128 127
 	c.ContainerMountLabel = ctr.MountLabel
129 128
 	c.ContainerOS = ctr.ImagePlatform.OS
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"github.com/containerd/log"
8 8
 	"github.com/moby/moby/api/types/events"
9 9
 	"github.com/moby/moby/v2/daemon/container"
10
+	"github.com/moby/moby/v2/errdefs"
10 11
 )
11 12
 
12 13
 // ContainerPause pauses a container
... ...
@@ -32,7 +33,7 @@ func (daemon *Daemon) containerPause(container *container.Container) error {
32 32
 
33 33
 	// We cannot Pause the container which is already paused
34 34
 	if container.State.Paused {
35
-		return errNotPaused(container.ID)
35
+		return errdefs.Conflict(fmt.Errorf("container %s is already paused", container.ID))
36 36
 	}
37 37
 
38 38
 	// We cannot Pause the container which is restarting
... ...
@@ -95,9 +95,12 @@ func (s *Collector) Run() {
95 95
 		// but saves allocations in further iterations
96 96
 		pairs = pairs[:0]
97 97
 
98
-		for container, publisher := range s.publishers {
98
+		for ctr, publisher := range s.publishers {
99 99
 			// copy pointers here to release the lock ASAP
100
-			pairs = append(pairs, publishersPair{container, publisher})
100
+			pairs = append(pairs, publishersPair{
101
+				container: ctr,
102
+				publisher: publisher,
103
+			})
101 104
 		}
102 105
 
103 106
 		s.cond.L.Unlock()
... ...
@@ -13,6 +13,7 @@ import (
13 13
 
14 14
 	statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats"
15 15
 	statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats"
16
+	cerrdefs "github.com/containerd/errdefs"
16 17
 	containertypes "github.com/moby/moby/api/types/container"
17 18
 	"github.com/moby/moby/v2/daemon/container"
18 19
 	"github.com/pkg/errors"
... ...
@@ -40,7 +41,7 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon
40 40
 	}
41 41
 	cs, err := task.Stats(context.Background())
42 42
 	if err != nil {
43
-		if strings.Contains(err.Error(), "container not found") {
43
+		if cerrdefs.IsNotFound(err) || strings.Contains(err.Error(), "container not found") {
44 44
 			return nil, containerNotFound(c.ID)
45 45
 		}
46 46
 		return nil, err