Skip to content

api/t/network: represent MAC addrs as byte slices#51355

Merged
robmry merged 1 commit intomoby:masterfrom
corhere:api-macaddr-marshaltext
Oct 30, 2025
Merged

api/t/network: represent MAC addrs as byte slices#51355
robmry merged 1 commit intomoby:masterfrom
corhere:api-macaddr-marshaltext

Conversation

@corhere
Copy link
Contributor

@corhere corhere commented Oct 30, 2025

- What I did
Make invalid states unrepresentable by moving away from stringly-typed MAC address values in API structs.

- How I did it
As https://go.dev/issue/29678 has not yet been implemented, provide our own HardwareAddr byte-slice type which implements TextMarshaler and TextUnmarshaler to retain compatibility with the API wire format.

When stdlib's net.HardwareAddr type implements TextMarshaler and TextUnmarshaler and GODEBUG=netmarshal becomes the default, we should be able to make the type a straight alias for stdlib net.HardwareAddr as a non-breaking change.

- How to verify it
CI.

- Human readable description for the release notes

- Go-SDK: MAC address fields are represented as byte slices compatible with the standard library net.HardwareAddr type instead of strings.

- A picture of a cute animal (not mandatory but encouraged)

@corhere corhere added this to the 29.0.0 milestone Oct 30, 2025
@corhere corhere added area/api API area/networking Networking impact/api impact/changelog impact/go-sdk Noteworthy (compatibility changes) in the Go SDK release-blocker PRs we want to block a release on labels Oct 30, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors MAC address handling across the codebase by introducing a new network.HardwareAddr type that wraps net.HardwareAddr with custom marshaling/unmarshaling behavior. This change improves type safety and eliminates repetitive MAC address parsing/validation logic.

Key changes:

  • Introduced network.HardwareAddr type with custom text and JSON marshaling
  • Replaced string-based MAC address fields with network.HardwareAddr throughout the API types
  • Removed redundant MAC address validation code since parsing now happens during unmarshaling

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
api/types/network/hwaddr.go New type definition for HardwareAddr with marshaling support
api/types/network/hwaddr_test.go Comprehensive tests for HardwareAddr marshaling/unmarshaling
api/types/network/endpoint.go Updated MacAddress field to use HardwareAddr type
api/types/network/endpoint_resource.go Updated MacAddress field to use HardwareAddr type
api/swagger.yaml Added x-go-type annotations for MacAddress fields
daemon/server/router/container/backend.go Updated ContainerInspect signature to return HardwareAddr
daemon/server/router/container/container_routes.go Updated MAC address handling to use HardwareAddr type
daemon/server/router/container/container_routes_test.go Updated tests to use HardwareAddr instead of strings
daemon/server/router/container/inspect.go Updated MAC address comparison to use length check
daemon/cluster/executor/backend.go Updated ContainerInspect signature to return HardwareAddr
daemon/inspect.go Updated return type and nil handling for HardwareAddr
daemon/network.go Removed manual MAC parsing, using type conversion instead
daemon/network/settings.go Updated DesiredMacAddress to HardwareAddr type
daemon/container_operations.go Removed manual MAC address validation and updated nil assignment
integration/internal/container/ops.go Added MAC address parsing in WithMacAddress helper
integration/networking/mac_addr_test.go Updated MAC address comparison to use slices.Equal
integration/networking/bridge_linux_test.go Updated MAC address comparisons and removed redundant parsing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@corhere corhere added the kind/refactor PR's that refactor, or clean-up code label Oct 30, 2025
@corhere corhere force-pushed the api-macaddr-marshaltext branch from 7065c16 to 0b130b3 Compare October 30, 2025 20:40
@thaJeztah

This comment has been minimized.

Make invalid states unrepresentable by moving away from stringly-typed
MAC address values in API structs. As go.dev/issue/29678 has not yet
been implemented, provide our own HardwareAddr byte-slice type which
implements TextMarshaler and TextUnmarshaler to retain compatibility
with the API wire format.

When stdlib's net.HardwareAddr type implements TextMarshaler and
TextUnmarshaler and GODEBUG=netmarshal becomes the default, we should be
able to make the type a straight alias for stdlib net.HardwareAddr as a
non-breaking change.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere force-pushed the api-macaddr-marshaltext branch from 0b130b3 to 19f4c27 Compare October 30, 2025 21:11
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@robmry want to give this a peek as well?

@robmry robmry merged commit 43b48f1 into moby:master Oct 30, 2025
181 checks passed
@corhere corhere deleted the api-macaddr-marshaltext branch November 10, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api API area/daemon Core Engine area/networking Networking area/swarm impact/api impact/changelog impact/go-sdk Noteworthy (compatibility changes) in the Go SDK kind/refactor PR's that refactor, or clean-up code module/api release-blocker PRs we want to block a release on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

api: consider using net.HardwareAddr for MACAddress fields?

4 participants