From 3f2e9da0100af2ceb3ef0d6431cb2b27dc3e1e49 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 15 Oct 2024 17:01:14 +0200 Subject: [PATCH] api/server/router/container: move API adjustments to API The daemon used to have various implementation to adjust the container-inspect output for different API versions, which could return different go structs, and because of that required a function with a `interface{}` output type. Most of those adjustments have been removed, and we no longer need separate types for backward compatibility with old API versions. This patch; - Removes the Daemon.ContainerInspectCurrent method - Introduces a backend.ContainerInspectOptions struct - Updates the Daemon.ContainerInspect method's signature to accept the above - Moves API-version specific adjustments to api/server/router/container, similar to how such adjustments are made for other endpoints. Note that we should probably change the backend's signature further, and define separate types for the backend's inspect and the API's inspect response. Considering that the Backend signatures should be considered "internal", we can do that in a future change. Signed-off-by: Sebastiaan van Stijn --- api/server/router/container/backend.go | 2 +- api/server/router/container/inspect.go | 24 ++++++++++++---- api/types/backend/backend.go | 7 +++++ daemon/cluster/executor/backend.go | 2 +- daemon/cluster/executor/container/adapter.go | 2 +- daemon/inspect.go | 30 ++------------------ 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/api/server/router/container/backend.go b/api/server/router/container/backend.go index 7edb00bbb3..e28b6d194a 100644 --- a/api/server/router/container/backend.go +++ b/api/server/router/container/backend.go @@ -47,7 +47,7 @@ type stateBackend interface { // monitorBackend includes functions to implement to provide containers monitoring functionality. type monitorBackend interface { ContainerChanges(ctx context.Context, name string) ([]archive.Change, error) - ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error) + ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error) 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) diff --git a/api/server/router/container/inspect.go b/api/server/router/container/inspect.go index 5d593a35cd..b65342c629 100644 --- a/api/server/router/container/inspect.go +++ b/api/server/router/container/inspect.go @@ -5,17 +5,31 @@ import ( "net/http" "github.com/docker/docker/api/server/httputils" + "github.com/docker/docker/api/types/backend" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/versions" + "github.com/docker/docker/internal/sliceutil" + "github.com/docker/docker/pkg/stringid" ) // getContainersByName inspects container's configuration and serializes it as json. func (c *containerRouter) getContainersByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - displaySize := httputils.BoolValue(r, "size") - - version := httputils.VersionFromContext(ctx) - json, err := c.backend.ContainerInspect(ctx, vars["name"], displaySize, version) + ctr, err := c.backend.ContainerInspect(ctx, vars["name"], backend.ContainerInspectOptions{ + Size: httputils.BoolValue(r, "size"), + }) if err != nil { return err } - return httputils.WriteJSON(w, http.StatusOK, json) + version := httputils.VersionFromContext(ctx) + if versions.LessThan(version, "1.45") { + shortCID := stringid.TruncateID(ctr.ID) + for nwName, ep := range ctr.NetworkSettings.Networks { + if container.NetworkMode(nwName).IsUserDefined() { + ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname)) + } + } + } + + return httputils.WriteJSON(w, http.StatusOK, ctr) } diff --git a/api/types/backend/backend.go b/api/types/backend/backend.go index 584a734e0e..c5514d06d1 100644 --- a/api/types/backend/backend.go +++ b/api/types/backend/backend.go @@ -92,6 +92,13 @@ type ContainerStatsConfig struct { OutStream func() io.Writer } +// ContainerInspectOptions defines options for the backend.ContainerInspect +// call. +type ContainerInspectOptions struct { + // Size controls whether to propagate the container's size fields. + Size bool +} + // ExecStartConfig holds the options to start container's exec. type ExecStartConfig struct { Stdin io.Reader diff --git a/daemon/cluster/executor/backend.go b/daemon/cluster/executor/backend.go index ebbebecd7a..a4290c735b 100644 --- a/daemon/cluster/executor/backend.go +++ b/daemon/cluster/executor/backend.go @@ -44,7 +44,7 @@ type Backend interface { ActivateContainerServiceBinding(containerName string) error DeactivateContainerServiceBinding(containerName string) error UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error - ContainerInspectCurrent(ctx context.Context, name string, size bool) (*container.InspectResponse, error) + ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error) ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error) ContainerRm(name string, config *backend.ContainerRmConfig) error ContainerKill(name string, sig string) error diff --git a/daemon/cluster/executor/container/adapter.go b/daemon/cluster/executor/container/adapter.go index 810f87093e..72108f42aa 100644 --- a/daemon/cluster/executor/container/adapter.go +++ b/daemon/cluster/executor/container/adapter.go @@ -372,7 +372,7 @@ func (c *containerAdapter) start(ctx context.Context) error { } func (c *containerAdapter) inspect(ctx context.Context) (containertypes.InspectResponse, error) { - cs, err := c.backend.ContainerInspectCurrent(ctx, c.container.name(), false) + cs, err := c.backend.ContainerInspect(ctx, c.container.name(), backend.ContainerInspectOptions{}) if ctx.Err() != nil { return containertypes.InspectResponse{}, ctx.Err() } diff --git a/daemon/inspect.go b/daemon/inspect.go index f8d72db87d..37cfb35187 100644 --- a/daemon/inspect.go +++ b/daemon/inspect.go @@ -12,43 +12,17 @@ import ( "github.com/docker/docker/api/types/backend" containertypes "github.com/docker/docker/api/types/container" networktypes "github.com/docker/docker/api/types/network" - "github.com/docker/docker/api/types/versions" "github.com/docker/docker/container" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/network" "github.com/docker/docker/errdefs" - "github.com/docker/docker/internal/sliceutil" - "github.com/docker/docker/pkg/stringid" "github.com/docker/go-connections/nat" ) // ContainerInspect returns low-level information about a // container. Returns an error if the container cannot be found, or if // there is an error getting the data. -func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error) { - switch { - case versions.LessThan(version, "1.45"): - ctr, err := daemon.ContainerInspectCurrent(ctx, name, size) - if err != nil { - return nil, err - } - - shortCID := stringid.TruncateID(ctr.ID) - for nwName, ep := range ctr.NetworkSettings.Networks { - if containertypes.NetworkMode(nwName).IsUserDefined() { - ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname)) - } - } - - return ctr, nil - default: - return daemon.ContainerInspectCurrent(ctx, name, size) - } -} - -// ContainerInspectCurrent returns low-level information about a -// container in a most recent api version. -func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string, size bool) (*containertypes.InspectResponse, error) { +func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*containertypes.InspectResponse, error) { ctr, err := daemon.GetContainer(name) if err != nil { return nil, err @@ -94,7 +68,7 @@ func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string, ctr.Unlock() - if size { + if options.Size { sizeRw, sizeRootFs, err := daemon.imageService.GetContainerLayerSize(ctx, base.ID) if err != nil { return nil, err