From 05b0e653dd1a9fdec903f9a110b23d29b353fe2e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 26 Jun 2024 00:21:30 +0200 Subject: [PATCH] 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 --- api/server/router/container/backend.go | 3 +-- api/types/container/container.go | 23 +++++++++++++++++++++++ api/types/types.go | 25 +------------------------ api/types/types_deprecated.go | 6 ++++++ client/container_list.go | 5 ++--- client/container_list_test.go | 3 +-- client/interface.go | 2 +- container/view.go | 9 ++++----- daemon/cluster/executor/backend.go | 2 +- daemon/daemon.go | 3 +-- daemon/disk_usage.go | 6 +++--- daemon/list.go | 9 ++++----- daemon/list_test.go | 3 +-- integration/container/ps_test.go | 3 +-- integration/system/disk_usage_test.go | 3 ++- testutil/environment/clean.go | 4 ++-- 16 files changed, 54 insertions(+), 55 deletions(-) diff --git a/api/server/router/container/backend.go b/api/server/router/container/backend.go index 07f2fedd7e..7edb00bbb3 100644 --- a/api/server/router/container/backend.go +++ b/api/server/router/container/backend.go @@ -4,7 +4,6 @@ import ( "context" "io" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" @@ -52,7 +51,7 @@ type monitorBackend interface { ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error) ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error) - Containers(ctx context.Context, config *container.ListOptions) ([]*types.Container, error) + Containers(ctx context.Context, config *container.ListOptions) ([]*container.Summary, error) } // attachBackend includes function to implement to provide container attaching functionality. diff --git a/api/types/container/container.go b/api/types/container/container.go index 629d7c323d..00a631b34e 100644 --- a/api/types/container/container.go +++ b/api/types/container/container.go @@ -105,3 +105,26 @@ type State struct { FinishedAt string Health *Health `json:",omitempty"` } + +// Summary contains response of Engine API: +// GET "/containers/json" +type Summary struct { + ID string `json:"Id"` + Names []string + Image string + ImageID string + Command string + Created int64 + Ports []Port + SizeRw int64 `json:",omitempty"` + SizeRootFs int64 `json:",omitempty"` + Labels map[string]string + State string + Status string + HostConfig struct { + NetworkMode string `json:",omitempty"` + Annotations map[string]string `json:",omitempty"` + } + NetworkSettings *NetworkSettingsSummary + Mounts []MountPoint +} diff --git a/api/types/types.go b/api/types/types.go index 7903e6a496..2df870cede 100644 --- a/api/types/types.go +++ b/api/types/types.go @@ -19,29 +19,6 @@ const ( MediaTypeMultiplexedStream = "application/vnd.docker.multiplexed-stream" ) -// Container contains response of Engine API: -// GET "/containers/json" -type Container struct { - ID string `json:"Id"` - Names []string - Image string - ImageID string - Command string - Created int64 - Ports []container.Port - SizeRw int64 `json:",omitempty"` - SizeRootFs int64 `json:",omitempty"` - Labels map[string]string - State string - Status string - HostConfig struct { - NetworkMode string `json:",omitempty"` - Annotations map[string]string `json:",omitempty"` - } - NetworkSettings *container.NetworkSettingsSummary - Mounts []container.MountPoint -} - // Ping contains response of Engine API: // GET "/_ping" type Ping struct { @@ -149,7 +126,7 @@ type DiskUsageOptions struct { type DiskUsage struct { LayersSize int64 Images []*image.Summary - Containers []*Container + Containers []*container.Summary Volumes []*volume.Volume BuildCache []*BuildCache BuilderSize int64 `json:",omitempty"` // Deprecated: deprecated in API 1.38, and no longer used since API 1.40. diff --git a/api/types/types_deprecated.go b/api/types/types_deprecated.go index 07d3f70858..d6d7fb6225 100644 --- a/api/types/types_deprecated.go +++ b/api/types/types_deprecated.go @@ -210,6 +210,12 @@ type ContainerNode struct { Labels map[string]string } +// Container contains response of Engine API: +// GET "/containers/json" +// +// Deprecated: use [container.Summary]. +type Container = container.Summary + // ContainerState stores container's running state // // Deprecated: use [container.State]. diff --git a/client/container_list.go b/client/container_list.go index 782e1b3c62..46c6950cd3 100644 --- a/client/container_list.go +++ b/client/container_list.go @@ -6,13 +6,12 @@ import ( "net/url" "strconv" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" ) // ContainerList returns the list of containers in the docker host. -func (cli *Client) ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) { +func (cli *Client) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) { query := url.Values{} if options.All { @@ -51,7 +50,7 @@ func (cli *Client) ContainerList(ctx context.Context, options container.ListOpti return nil, err } - var containers []types.Container + var containers []container.Summary err = json.NewDecoder(resp.body).Decode(&containers) return containers, err } diff --git a/client/container_list_test.go b/client/container_list_test.go index dbe4647af1..a6f404f06f 100644 --- a/client/container_list_test.go +++ b/client/container_list_test.go @@ -10,7 +10,6 @@ import ( "strings" "testing" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/errdefs" @@ -60,7 +59,7 @@ func TestContainerList(t *testing.T) { return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, fltrs) } - b, err := json.Marshal([]types.Container{ + b, err := json.Marshal([]container.Summary{ { ID: "container_id1", }, diff --git a/client/interface.go b/client/interface.go index 3b47271641..303ce1a65f 100644 --- a/client/interface.go +++ b/client/interface.go @@ -59,7 +59,7 @@ type ContainerAPIClient interface { ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error) ContainerInspectWithRaw(ctx context.Context, container string, getSize bool) (types.ContainerJSON, []byte, error) ContainerKill(ctx context.Context, container, signal string) error - ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) + ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error) ContainerPause(ctx context.Context, container string) error ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error diff --git a/container/view.go b/container/view.go index fb55af278a..0c6a5d4923 100644 --- a/container/view.go +++ b/container/view.go @@ -13,7 +13,6 @@ import ( "time" "github.com/containerd/log" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/network" "github.com/docker/docker/errdefs" @@ -39,7 +38,7 @@ var ( // Snapshot is a read only view for Containers. It holds all information necessary to serve container queries in a // versioned ACID in-memory store. type Snapshot struct { - types.Container + container.Summary // additional info queries need to filter on // preserve nanosec resolution for queries @@ -306,7 +305,7 @@ func (v *View) transform(ctr *Container) *Snapshot { health = ctr.Health.Status() } snapshot := &Snapshot{ - Container: types.Container{ + Summary: container.Summary{ ID: ctr.ID, Names: v.getNames(ctr.ID), ImageID: ctr.ImageID.String(), @@ -335,8 +334,8 @@ func (v *View) transform(ctr *Container) *Snapshot { } if ctr.HostConfig != nil { - snapshot.Container.HostConfig.NetworkMode = string(ctr.HostConfig.NetworkMode) - snapshot.Container.HostConfig.Annotations = maps.Clone(ctr.HostConfig.Annotations) + snapshot.Summary.HostConfig.NetworkMode = string(ctr.HostConfig.NetworkMode) + snapshot.Summary.HostConfig.Annotations = maps.Clone(ctr.HostConfig.Annotations) snapshot.HostConfig.Isolation = string(ctr.HostConfig.Isolation) for binding := range ctr.HostConfig.PortBindings { snapshot.PortBindings[binding] = struct{}{} diff --git a/daemon/cluster/executor/backend.go b/daemon/cluster/executor/backend.go index 1880ff96c0..d9d9f390f9 100644 --- a/daemon/cluster/executor/backend.go +++ b/daemon/cluster/executor/backend.go @@ -53,7 +53,7 @@ type Backend interface { SetContainerSecretReferences(name string, refs []*swarm.SecretReference) error SetContainerConfigReferences(name string, refs []*swarm.ConfigReference) error SystemInfo(context.Context) (*system.Info, error) - Containers(ctx context.Context, config *container.ListOptions) ([]*types.Container, error) + Containers(ctx context.Context, config *container.ListOptions) ([]*container.Summary, error) SetNetworkBootstrapKeys([]*networktypes.EncryptionKey) error DaemonJoinsCluster(provider cluster.Provider) DaemonLeavesCluster() diff --git a/daemon/daemon.go b/daemon/daemon.go index 2394e1b0e8..c10dbaf79b 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -32,7 +32,6 @@ import ( "github.com/containerd/log" "github.com/distribution/reference" dist "github.com/docker/distribution" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/backend" containertypes "github.com/docker/docker/api/types/container" imagetypes "github.com/docker/docker/api/types/image" @@ -136,7 +135,7 @@ type Daemon struct { seccompProfile []byte seccompProfilePath string - usageContainers singleflight.Group[struct{}, []*types.Container] + usageContainers singleflight.Group[struct{}, []*containertypes.Summary] usageImages singleflight.Group[struct{}, []*imagetypes.Summary] usageVolumes singleflight.Group[struct{}, []*volume.Volume] usageLayer singleflight.Group[struct{}, int64] diff --git a/daemon/disk_usage.go b/daemon/disk_usage.go index 5ed6c1b729..63b4de0c01 100644 --- a/daemon/disk_usage.go +++ b/daemon/disk_usage.go @@ -16,8 +16,8 @@ import ( // containerDiskUsage obtains information about container data disk usage // and makes sure that only one calculation is performed at the same time. -func (daemon *Daemon) containerDiskUsage(ctx context.Context) ([]*types.Container, error) { - res, _, err := daemon.usageContainers.Do(ctx, struct{}{}, func(ctx context.Context) ([]*types.Container, error) { +func (daemon *Daemon) containerDiskUsage(ctx context.Context) ([]*container.Summary, error) { + res, _, err := daemon.usageContainers.Do(ctx, struct{}{}, func(ctx context.Context) ([]*container.Summary, error) { // Retrieve container list containers, err := daemon.Containers(ctx, &container.ListOptions{ Size: true, @@ -81,7 +81,7 @@ func (daemon *Daemon) layerDiskUsage(ctx context.Context) (int64, error) { func (daemon *Daemon) SystemDiskUsage(ctx context.Context, opts system.DiskUsageOptions) (*types.DiskUsage, error) { eg, ctx := errgroup.WithContext(ctx) - var containers []*types.Container + var containers []*container.Summary if opts.Containers { eg.Go(func() error { var err error diff --git a/daemon/list.go b/daemon/list.go index c270401510..531f559075 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/containerd/log" - "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/backend" containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" @@ -99,14 +98,14 @@ func (r byCreatedDescending) Less(i, j int) bool { } // Containers returns the list of containers to show given the user's filtering. -func (daemon *Daemon) Containers(ctx context.Context, config *containertypes.ListOptions) ([]*types.Container, error) { +func (daemon *Daemon) Containers(ctx context.Context, config *containertypes.ListOptions) ([]*containertypes.Summary, error) { if err := config.Filters.Validate(acceptedPsFilterTags); err != nil { return nil, err } var ( view = daemon.containersReplica.Snapshot() - containers = []*types.Container{} + containers = []*containertypes.Summary{} ) filter, err := daemon.foldFilter(ctx, view, config) @@ -576,8 +575,8 @@ func includeContainerInList(container *container.Snapshot, filter *listContext) // $ docker ps -a // CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES // b0318bca5aef 3fbc63216742 "sh" 3 years ago Exited (0) 3 years ago ecstatic_beaver -func (daemon *Daemon) refreshImage(ctx context.Context, s *container.Snapshot) (*types.Container, error) { - c := s.Container +func (daemon *Daemon) refreshImage(ctx context.Context, s *container.Snapshot) (*containertypes.Summary, error) { + c := s.Summary // s.Image is the image reference passed by the user to create an image // can be a: diff --git a/daemon/list_test.go b/daemon/list_test.go index 33e54c0ded..78292b3f22 100644 --- a/daemon/list_test.go +++ b/daemon/list_test.go @@ -6,7 +6,6 @@ import ( "path/filepath" "testing" - "github.com/docker/docker/api/types" containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/container" @@ -68,7 +67,7 @@ func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *containe return c } -func containerListContainsName(containers []*types.Container, name string) bool { +func containerListContainsName(containers []*containertypes.Summary, name string) bool { for _, ctr := range containers { for _, containerName := range ctr.Names { if containerName == name { diff --git a/integration/container/ps_test.go b/integration/container/ps_test.go index d452ba2665..97e7038076 100644 --- a/integration/container/ps_test.go +++ b/integration/container/ps_test.go @@ -3,7 +3,6 @@ package container // import "github.com/docker/docker/integration/container" import ( "testing" - "github.com/docker/docker/api/types" containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/integration/internal/container" @@ -20,7 +19,7 @@ func TestPsFilter(t *testing.T) { top := container.Create(ctx, t, apiClient) next := container.Create(ctx, t, apiClient) - containerIDs := func(containers []types.Container) []string { + containerIDs := func(containers []containertypes.Summary) []string { var entries []string for _, c := range containers { entries = append(entries, c.ID) diff --git a/integration/system/disk_usage_test.go b/integration/system/disk_usage_test.go index d16fc0976a..396bc32c55 100644 --- a/integration/system/disk_usage_test.go +++ b/integration/system/disk_usage_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/docker/docker/api/types" + containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/volume" "github.com/docker/docker/integration/internal/container" @@ -49,7 +50,7 @@ func TestDiskUsage(t *testing.T) { assert.DeepEqual(t, du, types.DiskUsage{ LayersSize: expectedLayersSize, Images: []*image.Summary{}, - Containers: []*types.Container{}, + Containers: []*containertypes.Summary{}, Volumes: []*volume.Volume{}, BuildCache: []*types.BuildCache{}, }) diff --git a/testutil/environment/clean.go b/testutil/environment/clean.go index fcf670d662..4eea27d347 100644 --- a/testutil/environment/clean.go +++ b/testutil/environment/clean.go @@ -53,7 +53,7 @@ func unpauseAllContainers(ctx context.Context, t testing.TB, client client.Conta } } -func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container { +func getPausedContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []container.Summary { t.Helper() containers, err := client.ContainerList(ctx, container.ListOptions{ Filters: filters.NewArgs(filters.Arg("status", "paused")), @@ -87,7 +87,7 @@ func deleteAllContainers(ctx context.Context, t testing.TB, apiclient client.Con } } -func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []types.Container { +func getAllContainers(ctx context.Context, t testing.TB, client client.ContainerAPIClient) []container.Summary { t.Helper() containers, err := client.ContainerList(ctx, container.ListOptions{ All: true,