mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
daemon: use errdefs instead of string-matching in some places
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/moby/go-archive"
|
"github.com/moby/go-archive"
|
||||||
"github.com/moby/moby/v2/daemon/internal/image"
|
"github.com/moby/moby/v2/daemon/internal/image"
|
||||||
"github.com/moby/moby/v2/daemon/server/backend"
|
"github.com/moby/moby/v2/daemon/server/backend"
|
||||||
|
"github.com/moby/moby/v2/errdefs"
|
||||||
"github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
"github.com/opencontainers/image-spec/identity"
|
"github.com/opencontainers/image-spec/identity"
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
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) {
|
func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
|
||||||
ctr := i.containers.Get(c.ContainerID)
|
ctr := i.containers.Get(c.ContainerID)
|
||||||
if ctr == nil {
|
if ctr == nil {
|
||||||
// TODO: use typed error
|
return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
|
||||||
return "", fmt.Errorf("container not found: %s", c.ContainerID)
|
|
||||||
}
|
}
|
||||||
c.ContainerMountLabel = ctr.MountLabel
|
c.ContainerMountLabel = ctr.MountLabel
|
||||||
c.ContainerOS = ctr.ImagePlatform.OS
|
c.ContainerOS = ctr.ImagePlatform.OS
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
containertypes "github.com/moby/moby/api/types/container"
|
containertypes "github.com/moby/moby/api/types/container"
|
||||||
"github.com/moby/moby/v2/daemon/config"
|
"github.com/moby/moby/v2/daemon/config"
|
||||||
"github.com/moby/moby/v2/daemon/container"
|
"github.com/moby/moby/v2/daemon/container"
|
||||||
|
"github.com/moby/moby/v2/errdefs"
|
||||||
"github.com/moby/moby/v2/pkg/sysinfo"
|
"github.com/moby/moby/v2/pkg/sysinfo"
|
||||||
"github.com/opencontainers/selinux/go-selinux"
|
"github.com/opencontainers/selinux/go-selinux"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -26,7 +27,7 @@ type fakeContainerGetter struct {
|
|||||||
func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
|
func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
|
||||||
ctr, ok := f.containers[cid]
|
ctr, ok := f.containers[cid]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("container not found")
|
return nil, errdefs.NotFound(errors.New("container not found"))
|
||||||
}
|
}
|
||||||
return ctr, nil
|
return ctr, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,11 +54,6 @@ func errExecPaused(id string) error {
|
|||||||
return errdefs.Conflict(cause)
|
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 {
|
type nameConflictError struct {
|
||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ package images
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/moby/moby/api/types/events"
|
"github.com/moby/moby/api/types/events"
|
||||||
"github.com/moby/moby/v2/daemon/internal/image"
|
"github.com/moby/moby/v2/daemon/internal/image"
|
||||||
"github.com/moby/moby/v2/daemon/internal/layer"
|
"github.com/moby/moby/v2/daemon/internal/layer"
|
||||||
"github.com/moby/moby/v2/daemon/server/backend"
|
"github.com/moby/moby/v2/daemon/server/backend"
|
||||||
|
"github.com/moby/moby/v2/errdefs"
|
||||||
"github.com/moby/moby/v2/pkg/ioutils"
|
"github.com/moby/moby/v2/pkg/ioutils"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CommitImage creates a new image from a commit config
|
// 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) {
|
func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
|
||||||
ctr := i.containers.Get(c.ContainerID)
|
ctr := i.containers.Get(c.ContainerID)
|
||||||
if ctr == nil {
|
if ctr == nil {
|
||||||
// TODO: use typed error
|
return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
|
||||||
return "", errors.Errorf("container not found: %s", c.ContainerID)
|
|
||||||
}
|
}
|
||||||
c.ContainerMountLabel = ctr.MountLabel
|
c.ContainerMountLabel = ctr.MountLabel
|
||||||
c.ContainerOS = ctr.ImagePlatform.OS
|
c.ContainerOS = ctr.ImagePlatform.OS
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/containerd/log"
|
"github.com/containerd/log"
|
||||||
"github.com/moby/moby/api/types/events"
|
"github.com/moby/moby/api/types/events"
|
||||||
"github.com/moby/moby/v2/daemon/container"
|
"github.com/moby/moby/v2/daemon/container"
|
||||||
|
"github.com/moby/moby/v2/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContainerPause pauses a container
|
// 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
|
// We cannot Pause the container which is already paused
|
||||||
if container.State.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
|
// We cannot Pause the container which is restarting
|
||||||
|
|||||||
@@ -95,9 +95,12 @@ func (s *Collector) Run() {
|
|||||||
// but saves allocations in further iterations
|
// but saves allocations in further iterations
|
||||||
pairs = pairs[:0]
|
pairs = pairs[:0]
|
||||||
|
|
||||||
for container, publisher := range s.publishers {
|
for ctr, publisher := range s.publishers {
|
||||||
// copy pointers here to release the lock ASAP
|
// 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()
|
s.cond.L.Unlock()
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats"
|
statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats"
|
||||||
statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats"
|
statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats"
|
||||||
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
containertypes "github.com/moby/moby/api/types/container"
|
containertypes "github.com/moby/moby/api/types/container"
|
||||||
"github.com/moby/moby/v2/daemon/container"
|
"github.com/moby/moby/v2/daemon/container"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -40,7 +41,7 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon
|
|||||||
}
|
}
|
||||||
cs, err := task.Stats(context.Background())
|
cs, err := task.Stats(context.Background())
|
||||||
if err != nil {
|
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, containerNotFound(c.ID)
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user