Skip to content

Commit c8df751

Browse files
authored
Merge pull request moby#51294 from thaJeztah/daemon_cleanups
daemon: some minor cleanups
2 parents bc2d0b4 + 5cf4d68 commit c8df751

File tree

8 files changed

+27
-23
lines changed

8 files changed

+27
-23
lines changed

daemon/containerd/image_commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/moby/go-archive"
2121
"github.com/moby/moby/v2/daemon/internal/image"
2222
"github.com/moby/moby/v2/daemon/server/backend"
23+
"github.com/moby/moby/v2/errdefs"
2324
"github.com/opencontainers/go-digest"
2425
"github.com/opencontainers/image-spec/identity"
2526
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -320,8 +321,7 @@ func cleanup(ctx context.Context, do func(context.Context)) {
320321
func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
321322
ctr := i.containers.Get(c.ContainerID)
322323
if ctr == nil {
323-
// TODO: use typed error
324-
return "", fmt.Errorf("container not found: %s", c.ContainerID)
324+
return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
325325
}
326326
c.ContainerMountLabel = ctr.MountLabel
327327
c.ContainerOS = ctr.ImagePlatform.OS

daemon/daemon_unix_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
containertypes "github.com/moby/moby/api/types/container"
1313
"github.com/moby/moby/v2/daemon/config"
1414
"github.com/moby/moby/v2/daemon/container"
15+
"github.com/moby/moby/v2/errdefs"
1516
"github.com/moby/moby/v2/pkg/sysinfo"
1617
"github.com/opencontainers/selinux/go-selinux"
1718
"golang.org/x/sys/unix"
@@ -26,7 +27,7 @@ type fakeContainerGetter struct {
2627
func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
2728
ctr, ok := f.containers[cid]
2829
if !ok {
29-
return nil, errors.New("container not found")
30+
return nil, errdefs.NotFound(errors.New("container not found"))
3031
}
3132
return ctr, nil
3233
}

daemon/errors.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ func errExecPaused(id string) error {
5454
return errdefs.Conflict(cause)
5555
}
5656

57-
func errNotPaused(id string) error {
58-
cause := errors.Errorf("Container %s is already paused", id)
59-
return errdefs.Conflict(cause)
60-
}
61-
6257
type nameConflictError struct {
6358
id string
6459
name string

daemon/images/image_commit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package images
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"io"
78

89
"github.com/moby/moby/api/types/events"
910
"github.com/moby/moby/v2/daemon/internal/image"
1011
"github.com/moby/moby/v2/daemon/internal/layer"
1112
"github.com/moby/moby/v2/daemon/server/backend"
13+
"github.com/moby/moby/v2/errdefs"
1214
"github.com/moby/moby/v2/pkg/ioutils"
13-
"github.com/pkg/errors"
1415
)
1516

1617
// CommitImage creates a new image from a commit config
@@ -122,8 +123,7 @@ func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.R
122123
func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
123124
ctr := i.containers.Get(c.ContainerID)
124125
if ctr == nil {
125-
// TODO: use typed error
126-
return "", errors.Errorf("container not found: %s", c.ContainerID)
126+
return "", errdefs.NotFound(fmt.Errorf("container not found: %s", c.ContainerID))
127127
}
128128
c.ContainerMountLabel = ctr.MountLabel
129129
c.ContainerOS = ctr.ImagePlatform.OS

daemon/pause.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/containerd/log"
88
"github.com/moby/moby/api/types/events"
99
"github.com/moby/moby/v2/daemon/container"
10+
"github.com/moby/moby/v2/errdefs"
1011
)
1112

1213
// ContainerPause pauses a container
@@ -32,7 +33,7 @@ func (daemon *Daemon) containerPause(container *container.Container) error {
3233

3334
// We cannot Pause the container which is already paused
3435
if container.State.Paused {
35-
return errNotPaused(container.ID)
36+
return errdefs.Conflict(fmt.Errorf("container %s is already paused", container.ID))
3637
}
3738

3839
// We cannot Pause the container which is restarting

daemon/stats.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
5757
return &ss
5858
}
5959

60-
updates := daemon.subscribeToContainerStats(ctr)
61-
defer daemon.unsubscribeToContainerStats(ctr, updates)
60+
updates, cancel := daemon.subscribeToContainerStats(ctr)
61+
defer cancel()
6262

6363
noStreamFirstFrame := !config.OneShot
6464

@@ -90,12 +90,15 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
9090
}
9191
}
9292

93-
func (daemon *Daemon) subscribeToContainerStats(c *container.Container) chan any {
94-
return daemon.statsCollector.Collect(c)
95-
}
96-
97-
func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch chan any) {
98-
daemon.statsCollector.Unsubscribe(c, ch)
93+
// subscribeToContainerStats starts collecting stats for the given container.
94+
// It returns a channel containing [containertypes.StatsResponse] records,
95+
// and a cancel function to unsubscribe and stop collecting stats.
96+
func (daemon *Daemon) subscribeToContainerStats(c *container.Container) (updates chan any, cancel func()) {
97+
ch := daemon.statsCollector.Collect(c)
98+
cancel = func() {
99+
daemon.statsCollector.Unsubscribe(c, ch)
100+
}
101+
return ch, cancel
99102
}
100103

101104
// GetContainerStats collects all the stats published by a container

daemon/stats/collector.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ func (s *Collector) Run() {
9595
// but saves allocations in further iterations
9696
pairs = pairs[:0]
9797

98-
for container, publisher := range s.publishers {
98+
for ctr, publisher := range s.publishers {
9999
// 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+
})
101104
}
102105

103106
s.cond.L.Unlock()

daemon/stats_unix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats"
1515
statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats"
16+
cerrdefs "github.com/containerd/errdefs"
1617
containertypes "github.com/moby/moby/api/types/container"
1718
"github.com/moby/moby/v2/daemon/container"
1819
"github.com/pkg/errors"
@@ -40,7 +41,7 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon
4041
}
4142
cs, err := task.Stats(context.Background())
4243
if err != nil {
43-
if strings.Contains(err.Error(), "container not found") {
44+
if cerrdefs.IsNotFound(err) || strings.Contains(err.Error(), "container not found") {
4445
return nil, containerNotFound(c.ID)
4546
}
4647
return nil, err

0 commit comments

Comments
 (0)