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>
This commit is contained in:
Sebastiaan van Stijn
2024-06-26 00:21:30 +02:00
parent 84ffc644ef
commit 05b0e653dd
16 changed files with 54 additions and 55 deletions

View File

@@ -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.

View File

@@ -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
}

View File

@@ -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.

View File

@@ -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].

View File

@@ -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
}

View File

@@ -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",
},

View File

@@ -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

View File

@@ -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{}{}

View File

@@ -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()

View File

@@ -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]

View File

@@ -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

View File

@@ -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:

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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{},
})

View File

@@ -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,