Skip to content

Commit 2730600

Browse files
committed
WIP: option 1
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 57483c2 commit 2730600

6 files changed

Lines changed: 29 additions & 38 deletions

File tree

daemon/cluster/executor/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Backend interface {
4343
ActivateContainerServiceBinding(containerName string) error
4444
DeactivateContainerServiceBinding(containerName string) error
4545
UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error
46-
ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
46+
ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (_ *container.InspectResponse, desiredMACAddress string, _ error)
4747
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan containerpkg.StateStatus, error)
4848
ContainerRm(name string, config *backend.ContainerRmConfig) error
4949
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
@@ -375,7 +375,7 @@ func (c *containerAdapter) start(ctx context.Context) error {
375375
}
376376

377377
func (c *containerAdapter) inspect(ctx context.Context) (containertypes.InspectResponse, error) {
378-
cs, err := c.backend.ContainerInspect(ctx, c.container.name(), backend.ContainerInspectOptions{})
378+
cs, _, err := c.backend.ContainerInspect(ctx, c.container.name(), backend.ContainerInspectOptions{})
379379
if ctx.Err() != nil {
380380
return containertypes.InspectResponse{}, ctx.Err()
381381
}

daemon/inspect.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ import (
1919
// ContainerInspect returns low-level information about a
2020
// container. Returns an error if the container cannot be found, or if
2121
// there is an error getting the data.
22-
func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*containertypes.InspectResponse, error) {
22+
func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (_ *containertypes.InspectResponse, desiredMACAddress string, _ error) {
2323
ctr, err := daemon.GetContainer(name)
2424
if err != nil {
25-
return nil, err
25+
return nil, "", err
2626
}
2727

2828
ctr.Lock()
2929

30-
base, err := daemon.getInspectData(&daemon.config().Config, ctr)
30+
base, desiredMACAddress, err := daemon.getInspectData(&daemon.config().Config, ctr)
3131
if err != nil {
3232
ctr.Unlock()
33-
return nil, err
33+
return nil, "", err
3434
}
3535

3636
// TODO(thaJeztah): do we need a deep copy here? Otherwise we could use maps.Clone (see https://github.com/moby/moby/commit/7917a36cc787ada58987320e67cc6d96858f3b55)
@@ -61,7 +61,7 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options
6161
if options.Size {
6262
sizeRw, sizeRootFs, err := daemon.imageService.GetContainerLayerSize(ctx, base.ID)
6363
if err != nil {
64-
return nil, err
64+
return nil, "", err
6565
}
6666
base.SizeRw = &sizeRw
6767
base.SizeRootFs = &sizeRootFs
@@ -80,10 +80,10 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options
8080
base.NetworkSettings = networkSettings
8181
base.ImageManifestDescriptor = imageManifest
8282

83-
return base, nil
83+
return base, desiredMACAddress, nil
8484
}
8585

86-
func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Container) (*containertypes.InspectResponse, error) {
86+
func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Container) (_ *containertypes.InspectResponse, desiredMACAddress string, _ error) {
8787
// make a copy to play with
8888
hostConfig := *ctr.HostConfig
8989

@@ -101,13 +101,14 @@ func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Co
101101
// Config.MacAddress field for older API versions (< 1.44). We set it here
102102
// unconditionally, to keep backward compatibility with clients that use
103103
// unversioned API endpoints.
104-
// if ctr.Config != nil {
105-
// if nwm := hostConfig.NetworkMode; nwm.IsBridge() || nwm.IsUserDefined() {
106-
// if epConf, ok := ctr.NetworkSettings.Networks[nwm.NetworkName()]; ok {
107-
// ctr.Config.MacAddress = epConf.DesiredMacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
108-
// }
109-
// }
110-
// }
104+
var macAddress string
105+
if ctr.Config != nil {
106+
if nwm := hostConfig.NetworkMode; nwm.IsBridge() || nwm.IsUserDefined() {
107+
if epConf, ok := ctr.NetworkSettings.Networks[nwm.NetworkName()]; ok {
108+
macAddress = epConf.DesiredMacAddress
109+
}
110+
}
111+
}
111112

112113
var containerHealth *containertypes.Health
113114
if ctr.State.Health != nil {
@@ -163,31 +164,31 @@ func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Co
163164
}
164165

165166
// Additional information only applies to graphDrivers, so we're done.
166-
return inspectResponse, nil
167+
return inspectResponse, macAddress, nil
167168
}
168169

169170
inspectResponse.GraphDriver = &storage.DriverData{
170171
Name: ctr.Driver,
171172
}
172173
if ctr.RWLayer == nil {
173174
if ctr.State.Dead {
174-
return inspectResponse, nil
175+
return inspectResponse, "", nil
175176
}
176-
return nil, errdefs.System(errors.New("RWLayer of container " + ctr.ID + " is unexpectedly nil"))
177+
return nil, "", errdefs.System(errors.New("RWLayer of container " + ctr.ID + " is unexpectedly nil"))
177178
}
178179

179180
graphDriverData, err := ctr.RWLayer.Metadata()
180181
if err != nil {
181182
if ctr.State.Dead {
182183
// container is marked as Dead, and its graphDriver metadata may
183184
// have been removed; we can ignore errors.
184-
return inspectResponse, nil
185+
return inspectResponse, "", nil
185186
}
186-
return nil, errdefs.System(err)
187+
return nil, "", errdefs.System(err)
187188
}
188189

189190
inspectResponse.GraphDriver.Data = graphDriverData
190-
return inspectResponse, nil
191+
return inspectResponse, macAddress, nil
191192
}
192193

193194
// ContainerExecInspect returns low-level information about the exec

daemon/inspect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func TestGetInspectData(t *testing.T) {
2626
cfg := &configStore{}
2727
d.configStore.Store(cfg)
2828

29-
_, err := d.getInspectData(&cfg.Config, c)
29+
_, _, err := d.getInspectData(&cfg.Config, c)
3030
assert.Check(t, is.ErrorContains(err, "RWLayer of container inspect-me is unexpectedly nil"))
3131

3232
c.State.Dead = true
33-
_, err = d.getInspectData(&cfg.Config, c)
33+
_, _, err = d.getInspectData(&cfg.Config, c)
3434
assert.Check(t, err)
3535
}

daemon/server/router/container/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type stateBackend interface {
4848
// monitorBackend includes functions to implement to provide containers monitoring functionality.
4949
type monitorBackend interface {
5050
ContainerChanges(ctx context.Context, name string) ([]archive.Change, error)
51-
ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
51+
ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (_ *container.InspectResponse, desiredMACAddress string, _ error)
5252
ContainerLogs(ctx context.Context, name string, config *backend.ContainerLogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
5353
ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
5454
ContainerTop(name string, psArgs string) (*container.TopResponse, error)

daemon/server/router/container/inspect.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// getContainersByName inspects container's configuration and serializes it as json.
1717
func (c *containerRouter) getContainersByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
18-
ctr, err := c.backend.ContainerInspect(ctx, vars["name"], backend.ContainerInspectOptions{
18+
ctr, desiredMACAddress, err := c.backend.ContainerInspect(ctx, vars["name"], backend.ContainerInspectOptions{
1919
Size: httputils.BoolValue(r, "size"),
2020
})
2121
if err != nil {
@@ -62,20 +62,10 @@ func (c *containerRouter) getContainersByName(ctx context.Context, w http.Respon
6262
//
6363
// This was deprecated in API v1.44, but kept in place until
6464
// API v1.52, which removed this entirely.
65-
var macAddress string
66-
if ctr.HostConfig != nil {
67-
mainNetwork := "bridge"
68-
if ctr.HostConfig.NetworkMode.IsUserDefined() {
69-
mainNetwork = ctr.HostConfig.NetworkMode.NetworkName()
70-
}
71-
if v := ctr.NetworkSettings.Networks[mainNetwork]; v != nil && v.MacAddress != "" {
72-
macAddress = v.MacAddress
73-
}
74-
}
75-
if macAddress != "" {
65+
if desiredMACAddress != "" {
7666
wrapOpts = append(wrapOpts, compat.WithExtraFields(map[string]any{
7767
"Config": map[string]any{
78-
"MacAddress": macAddress,
68+
"MacAddress": desiredMACAddress,
7969
},
8070
}))
8171
}

0 commit comments

Comments
 (0)