Skip to content

Commit 3f2e9da

Browse files
committed
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 <[email protected]>
1 parent 0c595fe commit 3f2e9da

6 files changed

Lines changed: 31 additions & 36 deletions

File tree

api/server/router/container/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type stateBackend interface {
4747
// monitorBackend includes functions to implement to provide containers monitoring functionality.
4848
type monitorBackend interface {
4949
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)
5151
ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
5252
ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
5353
ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error)

api/server/router/container/inspect.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@ import (
55
"net/http"
66

77
"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"
813
)
914

1015
// getContainersByName inspects container's configuration and serializes it as json.
1116
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+
})
1620
if err != nil {
1721
return err
1822
}
1923

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)
2135
}

api/types/backend/backend.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ type ContainerStatsConfig struct {
9292
OutStream func() io.Writer
9393
}
9494

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+
95102
// ExecStartConfig holds the options to start container's exec.
96103
type ExecStartConfig struct {
97104
Stdin io.Reader

daemon/cluster/executor/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Backend interface {
4444
ActivateContainerServiceBinding(containerName string) error
4545
DeactivateContainerServiceBinding(containerName string) error
4646
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)
4848
ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
4949
ContainerRm(name string, config *backend.ContainerRmConfig) error
5050
ContainerKill(name string, sig string) error

daemon/cluster/executor/container/adapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func (c *containerAdapter) start(ctx context.Context) error {
372372
}
373373

374374
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{})
376376
if ctx.Err() != nil {
377377
return containertypes.InspectResponse{}, ctx.Err()
378378
}

daemon/inspect.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,17 @@ import (
1212
"github.com/docker/docker/api/types/backend"
1313
containertypes "github.com/docker/docker/api/types/container"
1414
networktypes "github.com/docker/docker/api/types/network"
15-
"github.com/docker/docker/api/types/versions"
1615
"github.com/docker/docker/container"
1716
"github.com/docker/docker/daemon/config"
1817
"github.com/docker/docker/daemon/network"
1918
"github.com/docker/docker/errdefs"
20-
"github.com/docker/docker/internal/sliceutil"
21-
"github.com/docker/docker/pkg/stringid"
2219
"github.com/docker/go-connections/nat"
2320
)
2421

2522
// ContainerInspect returns low-level information about a
2623
// container. Returns an error if the container cannot be found, or if
2724
// 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) {
5226
ctr, err := daemon.GetContainer(name)
5327
if err != nil {
5428
return nil, err
@@ -94,7 +68,7 @@ func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string,
9468

9569
ctr.Unlock()
9670

97-
if size {
71+
if options.Size {
9872
sizeRw, sizeRootFs, err := daemon.imageService.GetContainerLayerSize(ctx, base.ID)
9973
if err != nil {
10074
return nil, err

0 commit comments

Comments
 (0)