daemon/server/router/container: remove uses of deprecated MacAddress

Rewrite the router to use a local struct to unmarshal the deprecated
field for requests that send it, and adjust the adoption code.

There also appeared to be duplication between daemon.getInspectData,
and the containerRouter.postContainersCreate methods, as both were
back-filling the field.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-15 16:32:29 +02:00
parent 0030ba21e3
commit 91ce33d4b0
2 changed files with 21 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
package container
import (
"bytes"
"context"
"encoding/json"
"fmt"
@@ -497,8 +498,12 @@ func (c *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
name := r.Form.Get("name")
// Use a tee-reader to allow reading the body for legacy fields.
var requestBody bytes.Buffer
rdr := io.TeeReader(r.Body, &requestBody)
// TODO(thaJeztah): do we prefer [backend.ContainerCreateConfig] here?
req, err := runconfig.DecodeCreateRequest(r.Body, c.backend.RawSysInfo())
req, err := runconfig.DecodeCreateRequest(rdr, c.backend.RawSysInfo())
if err != nil {
return err
}
@@ -662,10 +667,19 @@ func (c *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
if warn := handleVolumeDriverBC(version, hostConfig); warn != "" {
warnings = append(warnings, warn)
}
if warn, err := handleMACAddressBC(config, hostConfig, networkingConfig, version); err != nil {
return err
} else if warn != "" {
warnings = append(warnings, warn)
if versions.LessThan(version, "1.52") {
var legacyConfig struct {
// Mac Address of the container.
//
// MacAddress field is deprecated since API v1.44. Use EndpointSettings.MacAddress instead.
MacAddress string `json:",omitempty"`
}
_ = json.Unmarshal(requestBody.Bytes(), &legacyConfig)
if warn, err := handleMACAddressBC(hostConfig, networkingConfig, version, legacyConfig.MacAddress); err != nil {
return err
} else if warn != "" {
warnings = append(warnings, warn)
}
}
if warn, err := handleSysctlBC(hostConfig, networkingConfig, version); err != nil {
@@ -731,9 +745,7 @@ func handleVolumeDriverBC(version string, hostConfig *container.HostConfig) (war
// handleMACAddressBC takes care of backward-compatibility for the container-wide MAC address by mutating the
// networkingConfig to set the endpoint-specific MACAddress field introduced in API v1.44. It returns a warning message
// or an error if the container-wide field was specified for API >= v1.44.
func handleMACAddressBC(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, version string) (string, error) {
deprecatedMacAddress := config.MacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
func handleMACAddressBC(hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, version string, deprecatedMacAddress string) (string, error) {
// For older versions of the API, migrate the container-wide MAC address to EndpointsConfig.
if versions.LessThan(version, "1.44") {
if deprecatedMacAddress == "" {
@@ -780,7 +792,6 @@ func handleMACAddressBC(config *container.Config, hostConfig *container.HostConf
}
}
warning = "The container-wide MacAddress field is now deprecated. It should be specified in EndpointsConfig instead."
config.MacAddress = "" //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
return warning, nil
}

View File

@@ -120,9 +120,6 @@ func TestHandleMACAddressBC(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
cfg := &container.Config{
MacAddress: tc.ctrWideMAC, //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
}
hostCfg := &container.HostConfig{
NetworkMode: tc.networkMode,
}
@@ -135,7 +132,7 @@ func TestHandleMACAddressBC(t *testing.T) {
EndpointsConfig: epConfig,
}
warning, err := handleMACAddressBC(cfg, hostCfg, netCfg, tc.apiVersion)
warning, err := handleMACAddressBC(hostCfg, netCfg, tc.apiVersion, tc.ctrWideMAC)
if tc.expError == "" {
assert.Check(t, err)
@@ -155,8 +152,6 @@ func TestHandleMACAddressBC(t *testing.T) {
got := netCfg.EndpointsConfig[tc.expEpWithNoMAC].MacAddress
assert.Check(t, is.Equal(got, ""))
}
gotCtrWideMAC := cfg.MacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
assert.Check(t, is.Equal(gotCtrWideMAC, tc.expCtrWideMAC))
})
}
}