libnetwork/drivers/macvlan: parentHasSingleUser: don't create copy of networks

This function was calling driver.getNetworks, which copies the networks map
into a new slice. As we're not mutating the networks, we can just use the
networks map itself to check if there's any networks configured with the
same parent.

While changing;

- Also change the signature to accept the parent to compare to as a string
- Return early once we determined there's more than one user

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-19 15:20:42 +02:00
parent 65296cd0e7
commit a013147c40

View File

@@ -139,14 +139,20 @@ func (d *driver) createNetwork(config *configuration) (bool, error) {
return foundExisting, nil
}
func (d *driver) parentHasSingleUser(n *network) bool {
func (d *driver) parentHasSingleUser(parent string) bool {
d.mu.Lock()
defer d.mu.Unlock()
users := 0
networkList := d.getNetworks()
for _, testN := range networkList {
if n.config.Parent == testN.config.Parent {
for _, nw := range d.networks {
if nw.config.Parent == parent {
users++
}
if users > 1 {
return false
}
}
// TODO(thaJeztah): "zero users" should also return "true?" (this would be theoretical as we're checking a network to be the last remaining user)
return users == 1
}
@@ -157,7 +163,7 @@ func (d *driver) DeleteNetwork(nid string) error {
return fmt.Errorf("network id %s not found", nid)
}
// if the driver created the slave interface and this network is the last user, delete it, otherwise leave it
if n.config.CreatedSlaveLink && parentExists(n.config.Parent) && d.parentHasSingleUser(n) {
if n.config.CreatedSlaveLink && parentExists(n.config.Parent) && d.parentHasSingleUser(n.config.Parent) {
// only delete the link if it is named the net_id
if n.config.Parent == getDummyName(nid) {
err := delDummyLink(n.config.Parent)