Skip to content

api/types/swarm: PortConfig: add Compare method#52047

Merged
vvoland merged 1 commit intomoby:masterfrom
thaJeztah:swarm_portspec_sorting
Feb 27, 2026
Merged

api/types/swarm: PortConfig: add Compare method#52047
vvoland merged 1 commit intomoby:masterfrom
thaJeztah:swarm_portspec_sorting

Conversation

@thaJeztah
Copy link
Member

Add a compare function that can be used for slices.SortFunc to have a canonical definition of sorting.

- What I did

- How I did it

- How to verify it

- Human readable description for the release notes

api/types/swarm: PortConfig: add Compare method.

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

Add a compare function that can be used for slices.SortFunc to have
a canonical definition of sorting.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added this to the 29.3.0 milestone Feb 15, 2026
@thaJeztah thaJeztah added area/api API status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. impact/changelog area/swarm module/api labels Feb 15, 2026
Comment on lines +60 to +61
if n := cmp.Compare(p.Protocol, other.Protocol); n != 0 {
return n
Copy link
Member Author

Choose a reason for hiding this comment

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

The only thing I wasn't sure of here was if we should perform "normalisation" here as well, because we have the explicit "not set means tcp", so two PortConfig's one with Protocol="" and one with Protocol="tcp" may have to be considered "equal"?

Copy link
Member Author

Choose a reason for hiding this comment

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

cc @robmry in case you have thoughts

Copy link
Contributor

Choose a reason for hiding this comment

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

From a quick look - I think Protocol is always populated in this one, so it's probably ok?

Copy link
Member Author

@thaJeztah thaJeztah Feb 27, 2026

Choose a reason for hiding this comment

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

@robmry yeah, I was considering having this utility to also be "usable" elsewhere; so that I can replace this in the CLI; not 100% sure if it's always propagated there

https://github.com/docker/cli/blob/b35a2d0837aa1b78a37b08973272227adb1b143d/cli/compose/convert/service.go#L575-L597

Copy link
Contributor

Choose a reason for hiding this comment

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

I think swarm.PortConfig always comes from the daemon, which populates the field (it's never an API param, and I don't see where the default-TCP would be applied).

Maybe I'm missing something though. Assuming TCP in the ordering if it's not set couldn't really break anything.

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 adds a Compare method to the PortConfig type in the Swarm API to enable canonical sorting of port configurations using slices.SortFunc. The method provides lexical ordering based on port and protocol properties.

Changes:

  • Added Compare(other PortConfig) int method to PortConfig struct with ordering by PublishedPort, TargetPort, Protocol, and PublishMode
  • Added comprehensive test coverage in network_test.go demonstrating the sorting behavior
  • Added cmp import to support the comparison implementation

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
api/types/swarm/network.go Added Compare method to PortConfig with documentation of comparison priority order
vendor/github.com/moby/moby/api/types/swarm/network.go Mirror of the Compare method implementation in vendor directory
api/types/swarm/network_test.go New test file with TestPortConfigCompareSort verifying sorting behavior across different field combinations

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

Comment on lines +53 to +64
func (p PortConfig) Compare(other PortConfig) int {
if n := cmp.Compare(p.PublishedPort, other.PublishedPort); n != 0 {
return n
}
if n := cmp.Compare(p.TargetPort, other.TargetPort); n != 0 {
return n
}
if n := cmp.Compare(p.Protocol, other.Protocol); n != 0 {
return n
}
return cmp.Compare(p.PublishMode, other.PublishMode)
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

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

The Compare method does not include the Name field in the comparison. This means that two PortConfig instances with identical PublishedPort, TargetPort, Protocol, and PublishMode but different Name values would be considered equal by the comparison function, potentially resulting in non-deterministic sort order.

Consider adding Name as a fifth comparison criterion (after PublishMode) to ensure a fully deterministic ordering. This would make the comparison complete and prevent any ambiguity when sorting PortConfig slices.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

Name is not used currently (probably should be removed)

@vvoland vvoland merged commit 7adeea2 into moby:master Feb 27, 2026
283 of 287 checks passed
@thaJeztah thaJeztah deleted the swarm_portspec_sorting branch February 27, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api API area/swarm impact/changelog kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. module/api status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants