Browse code

api/types: move Container to api/types/container

This moves the `Container` type to the containere package, rename
it to `Summary`, and deprecates the old location.

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

Sebastiaan van Stijn authored on 2024/06/26 07:21:30
Showing 16 changed files
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"io"
6 6
 
7
-	"github.com/docker/docker/api/types"
8 7
 	"github.com/docker/docker/api/types/backend"
9 8
 	"github.com/docker/docker/api/types/container"
10 9
 	"github.com/docker/docker/api/types/filters"
... ...
@@ -52,7 +51,7 @@ type monitorBackend interface {
52 52
 	ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
53 53
 	ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
54 54
 	ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error)
55
-	Containers(ctx context.Context, config *container.ListOptions) ([]*types.Container, error)
55
+	Containers(ctx context.Context, config *container.ListOptions) ([]*container.Summary, error)
56 56
 }
57 57
 
58 58
 // attachBackend includes function to implement to provide container attaching functionality.
... ...
@@ -105,3 +105,26 @@ type State struct {
105 105
 	FinishedAt string
106 106
 	Health     *Health `json:",omitempty"`
107 107
 }
108
+
109
+// Summary contains response of Engine API:
110
+// GET "/containers/json"
111
+type Summary struct {
112
+	ID         string `json:"Id"`
113
+	Names      []string
114
+	Image      string
115
+	ImageID    string
116
+	Command    string
117
+	Created    int64
118
+	Ports      []Port
119
+	SizeRw     int64 `json:",omitempty"`
120
+	SizeRootFs int64 `json:",omitempty"`
121
+	Labels     map[string]string
122
+	State      string
123
+	Status     string
124
+	HostConfig struct {
125
+		NetworkMode string            `json:",omitempty"`
126
+		Annotations map[string]string `json:",omitempty"`
127
+	}
128
+	NetworkSettings *NetworkSettingsSummary
129
+	Mounts          []MountPoint
130
+}
... ...
@@ -19,29 +19,6 @@ const (
19 19
 	MediaTypeMultiplexedStream = "application/vnd.docker.multiplexed-stream"
20 20
 )
21 21
 
22
-// Container contains response of Engine API:
23
-// GET "/containers/json"
24
-type Container struct {
25
-	ID         string `json:"Id"`
26
-	Names      []string
27
-	Image      string
28
-	ImageID    string
29
-	Command    string
30
-	Created    int64
31
-	Ports      []container.Port
32
-	SizeRw     int64 `json:",omitempty"`
33
-	SizeRootFs int64 `json:",omitempty"`
34
-	Labels     map[string]string
35
-	State      string
36
-	Status     string
37
-	HostConfig struct {
38
-		NetworkMode string            `json:",omitempty"`
39
-		Annotations map[string]string `json:",omitempty"`
40
-	}
41
-	NetworkSettings *container.NetworkSettingsSummary
42
-	Mounts          []container.MountPoint
43
-}
44
-
45 22
 // Ping contains response of Engine API:
46 23
 // GET "/_ping"
47 24
 type Ping struct {
... ...
@@ -149,7 +126,7 @@ type DiskUsageOptions struct {
149 149
 type DiskUsage struct {
150 150
 	LayersSize  int64
151 151
 	Images      []*image.Summary
152
-	Containers  []*Container
152
+	Containers  []*container.Summary
153 153
 	Volumes     []*volume.Volume
154 154
 	BuildCache  []*BuildCache
155 155
 	BuilderSize int64 `json:",omitempty"` // Deprecated: deprecated in API 1.38, and no longer used since API 1.40.
... ...
@@ -210,6 +210,12 @@ type ContainerNode struct {
210 210
 	Labels    map[string]string
211 211
 }
212 212
 
213
+// Container contains response of Engine API:
214
+// GET "/containers/json"
215
+//
216
+// Deprecated: use [container.Summary].
217
+type Container = container.Summary
218
+
213 219
 // ContainerState stores container's running state
214 220
 //
215 221
 // Deprecated: use [container.State].
... ...
@@ -6,13 +6,12 @@ import (
6 6
 	"net/url"
7 7
 	"strconv"
8 8
 
9
-	"github.com/docker/docker/api/types"
10 9
 	"github.com/docker/docker/api/types/container"
11 10
 	"github.com/docker/docker/api/types/filters"
12 11
 )
13 12
 
14 13
 // ContainerList returns the list of containers in the docker host.
15
-func (cli *Client) ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) {
14
+func (cli *Client) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) {
16 15
 	query := url.Values{}
17 16
 
18 17
 	if options.All {
... ...
@@ -51,7 +50,7 @@ func (cli *Client) ContainerList(ctx context.Context, options container.ListOpti
51 51
 		return nil, err
52 52
 	}
53 53
 
54
-	var containers []types.Container
54
+	var containers []container.Summary
55 55
 	err = json.NewDecoder(resp.body).Decode(&containers)
56 56
 	return containers, err
57 57
 }
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"strings"
11 11
 	"testing"
12 12
 
13
-	"github.com/docker/docker/api/types"
14 13
 	"github.com/docker/docker/api/types/container"
15 14
 	"github.com/docker/docker/api/types/filters"
16 15
 	"github.com/docker/docker/errdefs"
... ...
@@ -60,7 +59,7 @@ func TestContainerList(t *testing.T) {
60 60
 				return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, fltrs)
61 61
 			}
62 62
 
63
-			b, err := json.Marshal([]types.Container{
63
+			b, err := json.Marshal([]container.Summary{
64 64
 				{
65 65
 					ID: "container_id1",
66 66
 				},
... ...
@@ -59,7 +59,7 @@ type ContainerAPIClient interface {
59 59
 	ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error)
60 60
 	ContainerInspectWithRaw(ctx context.Context, container string, getSize bool) (types.ContainerJSON, []byte, error)
61 61
 	ContainerKill(ctx context.Context, container, signal string) error
62
-	ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error)
62
+	ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error)
63 63
 	ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error)
64 64
 	ContainerPause(ctx context.Context, container string) error
65 65
 	ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error
... ...
@@ -13,7 +13,6 @@ import (
13 13
 	"time"
14 14
 
15 15
 	"github.com/containerd/log"
16
-	"github.com/docker/docker/api/types"
17 16
 	"github.com/docker/docker/api/types/container"
18 17
 	"github.com/docker/docker/api/types/network"
19 18
 	"github.com/docker/docker/errdefs"
... ...
@@ -39,7 +38,7 @@ var (
39 39
 // Snapshot is a read only view for Containers. It holds all information necessary to serve container queries in a
40 40
 // versioned ACID in-memory store.
41 41
 type Snapshot struct {
42
-	types.Container
42
+	container.Summary
43 43
 
44 44
 	// additional info queries need to filter on
45 45
 	// preserve nanosec resolution for queries
... ...
@@ -306,7 +305,7 @@ func (v *View) transform(ctr *Container) *Snapshot {
306 306
 		health = ctr.Health.Status()
307 307
 	}
308 308
 	snapshot := &Snapshot{
309
-		Container: types.Container{
309
+		Summary: container.Summary{
310 310
 			ID:      ctr.ID,
311 311
 			Names:   v.getNames(ctr.ID),
312 312
 			ImageID: ctr.ImageID.String(),
... ...
@@ -335,8 +334,8 @@ func (v *View) transform(ctr *Container) *Snapshot {
335 335
 	}
336 336
 
337 337
 	if ctr.HostConfig != nil {
338
-		snapshot.Container.HostConfig.NetworkMode = string(ctr.HostConfig.NetworkMode)
339
-		snapshot.Container.HostConfig.Annotations = maps.Clone(ctr.HostConfig.Annotations)
338
+		snapshot.Summary.HostConfig.NetworkMode = string(ctr.HostConfig.NetworkMode)
339
+		snapshot.Summary.HostConfig.Annotations = maps.Clone(ctr.HostConfig.Annotations)
340 340
 		snapshot.HostConfig.Isolation = string(ctr.HostConfig.Isolation)
341 341
 		for binding := range ctr.HostConfig.PortBindings {
342 342
 			snapshot.PortBindings[binding] = struct{}{}
... ...
@@ -53,7 +53,7 @@ type Backend interface {
53 53
 	SetContainerSecretReferences(name string, refs []*swarm.SecretReference) error
54 54
 	SetContainerConfigReferences(name string, refs []*swarm.ConfigReference) error
55 55
 	SystemInfo(context.Context) (*system.Info, error)
56
-	Containers(ctx context.Context, config *container.ListOptions) ([]*types.Container, error)
56
+	Containers(ctx context.Context, config *container.ListOptions) ([]*container.Summary, error)
57 57
 	SetNetworkBootstrapKeys([]*networktypes.EncryptionKey) error
58 58
 	DaemonJoinsCluster(provider cluster.Provider)
59 59
 	DaemonLeavesCluster()
... ...
@@ -32,7 +32,6 @@ import (
32 32
 	"github.com/containerd/log"
33 33
 	"github.com/distribution/reference"
34 34
 	dist "github.com/docker/distribution"
35
-	"github.com/docker/docker/api/types"
36 35
 	"github.com/docker/docker/api/types/backend"
37 36
 	containertypes "github.com/docker/docker/api/types/container"
38 37
 	imagetypes "github.com/docker/docker/api/types/image"
... ...
@@ -136,7 +135,7 @@ type Daemon struct {
136 136
 	seccompProfile     []byte
137 137
 	seccompProfilePath string
138 138
 
139
-	usageContainers singleflight.Group[struct{}, []*types.Container]
139
+	usageContainers singleflight.Group[struct{}, []*containertypes.Summary]
140 140
 	usageImages     singleflight.Group[struct{}, []*imagetypes.Summary]
141 141
 	usageVolumes    singleflight.Group[struct{}, []*volume.Volume]
142 142
 	usageLayer      singleflight.Group[struct{}, int64]
... ...
@@ -16,8 +16,8 @@ import (
16 16
 
17 17
 // containerDiskUsage obtains information about container data disk usage
18 18
 // and makes sure that only one calculation is performed at the same time.
19
-func (daemon *Daemon) containerDiskUsage(ctx context.Context) ([]*types.Container, error) {
20
-	res, _, err := daemon.usageContainers.Do(ctx, struct{}{}, func(ctx context.Context) ([]*types.Container, error) {
19
+func (daemon *Daemon) containerDiskUsage(ctx context.Context) ([]*container.Summary, error) {
20
+	res, _, err := daemon.usageContainers.Do(ctx, struct{}{}, func(ctx context.Context) ([]*container.Summary, error) {
21 21
 		// Retrieve container list
22 22
 		containers, err := daemon.Containers(ctx, &container.ListOptions{
23 23
 			Size: true,
... ...
@@ -81,7 +81,7 @@ func (daemon *Daemon) layerDiskUsage(ctx context.Context) (int64, error) {
81 81
 func (daemon *Daemon) SystemDiskUsage(ctx context.Context, opts system.DiskUsageOptions) (*types.DiskUsage, error) {
82 82
 	eg, ctx := errgroup.WithContext(ctx)
83 83
 
84
-	var containers []*types.Container
84
+	var containers []*container.Summary
85 85
 	if opts.Containers {
86 86
 		eg.Go(func() error {
87 87
 			var err error
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"strings"
9 9
 
10 10
 	"github.com/containerd/log"
11
-	"github.com/docker/docker/api/types"
12 11
 	"github.com/docker/docker/api/types/backend"
13 12
 	containertypes "github.com/docker/docker/api/types/container"
14 13
 	"github.com/docker/docker/api/types/filters"
... ...
@@ -99,14 +98,14 @@ func (r byCreatedDescending) Less(i, j int) bool {
99 99
 }
100 100
 
101 101
 // Containers returns the list of containers to show given the user's filtering.
102
-func (daemon *Daemon) Containers(ctx context.Context, config *containertypes.ListOptions) ([]*types.Container, error) {
102
+func (daemon *Daemon) Containers(ctx context.Context, config *containertypes.ListOptions) ([]*containertypes.Summary, error) {
103 103
 	if err := config.Filters.Validate(acceptedPsFilterTags); err != nil {
104 104
 		return nil, err
105 105
 	}
106 106
 
107 107
 	var (
108 108
 		view       = daemon.containersReplica.Snapshot()
109
-		containers = []*types.Container{}
109
+		containers = []*containertypes.Summary{}
110 110
 	)
111 111
 
112 112
 	filter, err := daemon.foldFilter(ctx, view, config)
... ...
@@ -576,8 +575,8 @@ func includeContainerInList(container *container.Snapshot, filter *listContext)
576 576
 // $ docker ps -a
577 577
 // CONTAINER ID   IMAGE          COMMAND   CREATED       STATUS                  PORTS     NAMES
578 578
 // b0318bca5aef   3fbc63216742   "sh"      3 years ago   Exited (0) 3 years ago            ecstatic_beaver
579
-func (daemon *Daemon) refreshImage(ctx context.Context, s *container.Snapshot) (*types.Container, error) {
580
-	c := s.Container
579
+func (daemon *Daemon) refreshImage(ctx context.Context, s *container.Snapshot) (*containertypes.Summary, error) {
580
+	c := s.Summary
581 581
 
582 582
 	// s.Image is the image reference passed by the user to create an image
583 583
 	//         can be a:
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"path/filepath"
7 7
 	"testing"
8 8
 
9
-	"github.com/docker/docker/api/types"
10 9
 	containertypes "github.com/docker/docker/api/types/container"
11 10
 	"github.com/docker/docker/api/types/filters"
12 11
 	"github.com/docker/docker/container"
... ...
@@ -68,7 +67,7 @@ func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *containe
68 68
 	return c
69 69
 }
70 70
 
71
-func containerListContainsName(containers []*types.Container, name string) bool {
71
+func containerListContainsName(containers []*containertypes.Summary, name string) bool {
72 72
 	for _, ctr := range containers {
73 73
 		for _, containerName := range ctr.Names {
74 74
 			if containerName == name {
... ...
@@ -3,7 +3,6 @@ package container // import "github.com/docker/docker/integration/container"
3 3
 import (
4 4
 	"testing"
5 5
 
6
-	"github.com/docker/docker/api/types"
7 6
 	containertypes "github.com/docker/docker/api/types/container"
8 7
 	"github.com/docker/docker/api/types/filters"
9 8
 	"github.com/docker/docker/integration/internal/container"
... ...
@@ -20,7 +19,7 @@ func TestPsFilter(t *testing.T) {
20 20
 	top := container.Create(ctx, t, apiClient)
21 21
 	next := container.Create(ctx, t, apiClient)
22 22
 
23
-	containerIDs := func(containers []types.Container) []string {
23
+	containerIDs := func(containers []containertypes.Summary) []string {
24 24
 		var entries []string
25 25
 		for _, c := range containers {
26 26
 			entries = append(entries, c.ID)
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"testing"
6 6
 
7 7
 	"github.com/docker/docker/api/types"
8
+	containertypes "github.com/docker/docker/api/types/container"
8 9
 	"github.com/docker/docker/api/types/image"
9 10
 	"github.com/docker/docker/api/types/volume"
10 11
 	"github.com/docker/docker/integration/internal/container"
... ...
@@ -49,7 +50,7 @@ func TestDiskUsage(t *testing.T) {
49 49
 				assert.DeepEqual(t, du, types.DiskUsage{
50 50
 					LayersSize: expectedLayersSize,
51 51
 					Images:     []*image.Summary{},
52
-					Containers: []*types.Container{},
52
+					Containers: []*containertypes.Summary{},
53 53
 					Volumes:    []*volume.Volume{},
54 54
 					BuildCache: []*types.BuildCache{},
55 55
 				})
... ...
@@ -53,7 +53,7 @@ func unpauseAllContainers(ctx context.Context, t testing.TB, client client.Conta
53 53
 	}
54 54
 }
55 55
 
56
-func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container {
56
+func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []container.Summary {
57 57
 	t.Helper()
58 58
 	containers, err := client.ContainerList(ctx, container.ListOptions{
59 59
 		Filters: filters.NewArgs(filters.Arg("status", "paused")),
... ...
@@ -87,7 +87,7 @@ func deleteAllContainers(ctx context.Context, t testing.TB, apiclient client.Con
87 87
 	}
88 88
 }
89 89
 
90
-func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container {
90
+func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []container.Summary {
91 91
 	t.Helper()
92 92
 	containers, err := client.ContainerList(ctx, container.ListOptions{
93 93
 		All: true,