Browse code

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>

Sebastiaan van Stijn authored on 2025/10/19 22:20:42
Showing 1 changed files
... ...
@@ -139,14 +139,20 @@ func (d *driver) createNetwork(config *configuration) (bool, error) {
139 139
 	return foundExisting, nil
140 140
 }
141 141
 
142
-func (d *driver) parentHasSingleUser(n *network) bool {
142
+func (d *driver) parentHasSingleUser(parent string) bool {
143
+	d.mu.Lock()
144
+	defer d.mu.Unlock()
145
+
143 146
 	users := 0
144
-	networkList := d.getNetworks()
145
-	for _, testN := range networkList {
146
-		if n.config.Parent == testN.config.Parent {
147
+	for _, nw := range d.networks {
148
+		if nw.config.Parent == parent {
147 149
 			users++
148 150
 		}
151
+		if users > 1 {
152
+			return false
153
+		}
149 154
 	}
155
+	// TODO(thaJeztah): "zero users" should also return "true?" (this would be theoretical as we're checking a network to be the last remaining user)
150 156
 	return users == 1
151 157
 }
152 158
 
... ...
@@ -157,7 +163,7 @@ func (d *driver) DeleteNetwork(nid string) error {
157 157
 		return fmt.Errorf("network id %s not found", nid)
158 158
 	}
159 159
 	// if the driver created the slave interface and this network is the last user, delete it, otherwise leave it
160
-	if n.config.CreatedSlaveLink && parentExists(n.config.Parent) && d.parentHasSingleUser(n) {
160
+	if n.config.CreatedSlaveLink && parentExists(n.config.Parent) && d.parentHasSingleUser(n.config.Parent) {
161 161
 		// only delete the link if it is named the net_id
162 162
 		if n.config.Parent == getDummyName(nid) {
163 163
 			err := delDummyLink(n.config.Parent)