Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2024/10/16 00:01:14
Showing 6 changed files
... ...
@@ -47,7 +47,7 @@ type stateBackend interface {
47 47
 // monitorBackend includes functions to implement to provide containers monitoring functionality.
48 48
 type monitorBackend interface {
49 49
 	ContainerChanges(ctx context.Context, name string) ([]archive.Change, error)
50
-	ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error)
50
+	ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
51 51
 	ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
52 52
 	ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
53 53
 	ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error)
... ...
@@ -5,17 +5,31 @@ import (
5 5
 	"net/http"
6 6
 
7 7
 	"github.com/docker/docker/api/server/httputils"
8
+	"github.com/docker/docker/api/types/backend"
9
+	"github.com/docker/docker/api/types/container"
10
+	"github.com/docker/docker/api/types/versions"
11
+	"github.com/docker/docker/internal/sliceutil"
12
+	"github.com/docker/docker/pkg/stringid"
8 13
 )
9 14
 
10 15
 // getContainersByName inspects container's configuration and serializes it as json.
11 16
 func (c *containerRouter) getContainersByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
12
-	displaySize := httputils.BoolValue(r, "size")
13
-
14
-	version := httputils.VersionFromContext(ctx)
15
-	json, err := c.backend.ContainerInspect(ctx, vars["name"], displaySize, version)
17
+	ctr, err := c.backend.ContainerInspect(ctx, vars["name"], backend.ContainerInspectOptions{
18
+		Size: httputils.BoolValue(r, "size"),
19
+	})
16 20
 	if err != nil {
17 21
 		return err
18 22
 	}
19 23
 
20
-	return httputils.WriteJSON(w, http.StatusOK, json)
24
+	version := httputils.VersionFromContext(ctx)
25
+	if versions.LessThan(version, "1.45") {
26
+		shortCID := stringid.TruncateID(ctr.ID)
27
+		for nwName, ep := range ctr.NetworkSettings.Networks {
28
+			if container.NetworkMode(nwName).IsUserDefined() {
29
+				ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
30
+			}
31
+		}
32
+	}
33
+
34
+	return httputils.WriteJSON(w, http.StatusOK, ctr)
21 35
 }
... ...
@@ -92,6 +92,13 @@ type ContainerStatsConfig struct {
92 92
 	OutStream func() io.Writer
93 93
 }
94 94
 
95
+// ContainerInspectOptions defines options for the backend.ContainerInspect
96
+// call.
97
+type ContainerInspectOptions struct {
98
+	// Size controls whether to propagate the container's size fields.
99
+	Size bool
100
+}
101
+
95 102
 // ExecStartConfig holds the options to start container's exec.
96 103
 type ExecStartConfig struct {
97 104
 	Stdin       io.Reader
... ...
@@ -44,7 +44,7 @@ type Backend interface {
44 44
 	ActivateContainerServiceBinding(containerName string) error
45 45
 	DeactivateContainerServiceBinding(containerName string) error
46 46
 	UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error
47
-	ContainerInspectCurrent(ctx context.Context, name string, size bool) (*container.InspectResponse, error)
47
+	ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
48 48
 	ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
49 49
 	ContainerRm(name string, config *backend.ContainerRmConfig) error
50 50
 	ContainerKill(name string, sig string) error
... ...
@@ -372,7 +372,7 @@ func (c *containerAdapter) start(ctx context.Context) error {
372 372
 }
373 373
 
374 374
 func (c *containerAdapter) inspect(ctx context.Context) (containertypes.InspectResponse, error) {
375
-	cs, err := c.backend.ContainerInspectCurrent(ctx, c.container.name(), false)
375
+	cs, err := c.backend.ContainerInspect(ctx, c.container.name(), backend.ContainerInspectOptions{})
376 376
 	if ctx.Err() != nil {
377 377
 		return containertypes.InspectResponse{}, ctx.Err()
378 378
 	}
... ...
@@ -12,43 +12,17 @@ import (
12 12
 	"github.com/docker/docker/api/types/backend"
13 13
 	containertypes "github.com/docker/docker/api/types/container"
14 14
 	networktypes "github.com/docker/docker/api/types/network"
15
-	"github.com/docker/docker/api/types/versions"
16 15
 	"github.com/docker/docker/container"
17 16
 	"github.com/docker/docker/daemon/config"
18 17
 	"github.com/docker/docker/daemon/network"
19 18
 	"github.com/docker/docker/errdefs"
20
-	"github.com/docker/docker/internal/sliceutil"
21
-	"github.com/docker/docker/pkg/stringid"
22 19
 	"github.com/docker/go-connections/nat"
23 20
 )
24 21
 
25 22
 // ContainerInspect returns low-level information about a
26 23
 // container. Returns an error if the container cannot be found, or if
27 24
 // there is an error getting the data.
28
-func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error) {
29
-	switch {
30
-	case versions.LessThan(version, "1.45"):
31
-		ctr, err := daemon.ContainerInspectCurrent(ctx, name, size)
32
-		if err != nil {
33
-			return nil, err
34
-		}
35
-
36
-		shortCID := stringid.TruncateID(ctr.ID)
37
-		for nwName, ep := range ctr.NetworkSettings.Networks {
38
-			if containertypes.NetworkMode(nwName).IsUserDefined() {
39
-				ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
40
-			}
41
-		}
42
-
43
-		return ctr, nil
44
-	default:
45
-		return daemon.ContainerInspectCurrent(ctx, name, size)
46
-	}
47
-}
48
-
49
-// ContainerInspectCurrent returns low-level information about a
50
-// container in a most recent api version.
51
-func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string, size bool) (*containertypes.InspectResponse, error) {
25
+func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*containertypes.InspectResponse, error) {
52 26
 	ctr, err := daemon.GetContainer(name)
53 27
 	if err != nil {
54 28
 		return nil, err
... ...
@@ -94,7 +68,7 @@ func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string,
94 94
 
95 95
 	ctr.Unlock()
96 96
 
97
-	if size {
97
+	if options.Size {
98 98
 		sizeRw, sizeRootFs, err := daemon.imageService.GetContainerLayerSize(ctx, base.ID)
99 99
 		if err != nil {
100 100
 			return nil, err