Skip to content

Conversation

corhere
Copy link
Contributor

@corhere corhere commented May 27, 2025

- What I did
- How I did it
The netip types are really useful for tracking state in the overlay driver as they are hashable, unlike net.IP and friends, making them directly useable as map keys. Converting between netip and net types is fairly trivial, but fewer conversions is more ergonomic.

The NetworkDB entries for the overlay peer table encode the IP addresses as strings. We need to parse them to some representation before processing them further. Parse directly into netip types and pass those values around to cut down on the number of conversions needed.

The peerDB needs to marshal the keys and entries to structs of hashable values to be able to insert them into the SetMatrix. Use netip.Addr in peerEntry so that peerEntry values can be directly inserted into the SetMatrix without conversions. Use a hashable struct type as the SetMatrix key to avoid having to marshal the whole struct to a string and parse it back out.

Use netip.Addr as the map key for the driver's encryption map so the values do not need to be converted to and from strings. Change the encryption configuration methods to take netip types so the peerDB code can pass netip values directly.

- How to verify it

  1. Create a multi-node Swarm cluster.
  2. Create a user-defined encrypted overlay network.
  3. docker service create --mode global --network my-overlay -p 80:8080 nginxdemos/hello:plain-text
  4. Exec into one of the service containers and verify that the other containers can be curl'ed by the IP address of its user-defined overlay network endpoint. Repeat with the other replicas.
  5. curl port 8080 on each Swarm node. Verify that the service is reachable. Repeat to verify that the requests are load-balanced among the replicas.

- Human readable description for the release notes

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

Make the SetMatrix key's type generic so that e.g. netip.Addr values can
be used as matrix keys.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere added area/networking Networking kind/refactor PR's that refactor, or clean-up code area/networking/d/overlay Networking labels May 27, 2025
The netip types are really useful for tracking state in the overlay
driver as they are hashable, unlike net.IP and friends, making them
directly useable as map keys. Converting between netip and net types is
fairly trivial, but fewer conversions is more ergonomic.

The NetworkDB entries for the overlay peer table encode the IP addresses
as strings. We need to parse them to some representation before
processing them further. Parse directly into netip types and pass those
values around to cut down on the number of conversions needed.

The peerDB needs to marshal the keys and entries to structs of hashable
values to be able to insert them into the SetMatrix. Use netip.Addr in
peerEntry so that peerEntry values can be directly inserted into the
SetMatrix without conversions. Use a hashable struct type as the
SetMatrix key to avoid having to marshal the whole struct to a string
and parse it back out.

Use netip.Addr as the map key for the driver's encryption map so the
values do not need to be converted to and from strings. Change the
encryption configuration methods to take netip types so the peerDB code
can pass netip values directly.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere force-pushed the libn/overlay-netip branch from 6715ce4 to d188df0 Compare May 27, 2025 17:47
Copy link

@Copilot 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@corhere corhere requested a review from Copilot May 27, 2025 17:51
Copy link

@Copilot 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 various parts of the overlay driver code to use netip types instead of net types, improving hashability and reducing conversions. Key changes include updating SetMatrix generic parameters, modifying peerDB methods to work with netip types, and revising encryption and network functions to operate on the new types.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

File Description
libnetwork/service.go Update of SetMatrix type parameters in the service struct
libnetwork/network.go Changes in setmatrix usage and conversions to netip types
libnetwork/internal/setmatrix/setmatrix*.go Update of SetMatrix generics across insertion, deletion, etc.
libnetwork/drivers/overlay/*.go Comprehensive conversion of net and IP-related logic to netip
Comments suppressed due to low confidence (4)

libnetwork/service.go:60

  • Ensure that updating the generic parameters for SetMatrix in the service struct aligns with its usage elsewhere in the module, and that this change does not break any implicit assumptions about key and value types.
ipToEndpoint setmatrix.SetMatrix[string, string]

libnetwork/drivers/overlay/peerdb.go:129

  • Confirm that the ipmac type used as a key correctly implements equality and hashing semantics to prevent unexpected behavior in map operations.
b, i := pMap.mp.Insert(pKey, pEntry)

libnetwork/drivers/overlay/ov_network.go:616

  • Review the logic in getSubnetforIP to ensure that comparing prefix mask lengths using Bits() is sufficient for matching subnets and that the Contains method is applied correctly.
func (n *network) getSubnetforIP(ip netip.Prefix) *subnet {

libnetwork/drivers/overlay/encryption.go:175

  • Review the conversion of netip.Addr to slices via AsSlice(), ensuring compatibility for both IPv4 and IPv6 addresses during SPI computation.
spis := &spi{buildSPI(advIP.AsSlice(), remoteIP.AsSlice(), k.tag), buildSPI(remoteIP.AsSlice(), advIP.AsSlice(), k.tag)}

@thaJeztah
Copy link
Member

Looked from my phone, but this looks great!

@vvoland vvoland modified the milestones: 28.2.2, 28.3.0 May 29, 2025
@thompson-shaun thompson-shaun moved this from Open to Complete in 🔦 Maintainer spotlight May 29, 2025
@corhere corhere merged commit f144264 into moby:master May 29, 2025
166 checks passed
@corhere corhere deleted the libn/overlay-netip branch May 29, 2025 18:12
@vvoland vvoland modified the milestones: 28.3.0, 28.2.2 May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants