Skip to content

Commit fd71fa0

Browse files
author
Leandro Dorileo
authored
network: force NetworkManager to connect to primary nic (#461)
If we are cleaning up/removing NetworkManager's for primary nic we should force it to connect - hence creating the default configuration for the interface.
1 parent e861d8e commit fd71fa0

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

google_guest_agent/network/manager/network_manager_linux.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,22 @@ func (n *networkManager) rollbackConfigs(ctx context.Context, nics *Interfaces,
350350
return fmt.Errorf("getting interfaces: %v", err)
351351
}
352352

353-
for _, iface := range ifaces {
354-
if err := n.removeInterface(iface); err != nil {
353+
reconnectPrimaryNic := false
354+
355+
for i, iface := range ifaces {
356+
removed, err := n.removeInterface(iface)
357+
if err != nil {
355358
logger.Errorf("Failed to remove %q interface with error: %v", iface, err)
356359
}
360+
if i == 0 && removed {
361+
reconnectPrimaryNic = true
362+
}
357363
}
358364

359365
if removeVlan {
360366
for _, vnic := range nics.VlanInterfaces {
361367
iface := n.vlanInterfaceName(vnic.ParentInterfaceID, vnic.Vlan)
362-
if err := n.removeInterface(iface); err != nil {
368+
if _, err := n.removeInterface(iface); err != nil {
363369
logger.Errorf("Failed to remove %q interface with error: %v", iface, err)
364370
}
365371
}
@@ -369,6 +375,15 @@ func (n *networkManager) rollbackConfigs(ctx context.Context, nics *Interfaces,
369375
return fmt.Errorf("error reloading NetworkManager config cache: %v", err)
370376
}
371377

378+
// NetworkManager will not create a default connection if we are removing the one
379+
// we manage, in that case we need to force it to connect and then with that create
380+
// a default connection.
381+
if reconnectPrimaryNic {
382+
if err := run.Quiet(ctx, "nmcli", "device", "connect", ifaces[0]); err != nil {
383+
return fmt.Errorf("error connecting NetworkManager's managed interface: %s, %v", ifaces[0], err)
384+
}
385+
}
386+
372387
return nil
373388
}
374389

@@ -385,29 +400,30 @@ func (n *networkManager) RollbackNics(ctx context.Context, nics *Interfaces) err
385400
}
386401

387402
// removeInterface verifies .nmconnection is managed by Guest Agent and removes it.
388-
func (n *networkManager) removeInterface(iface string) error {
403+
// It returns true if the configuration removal succeeds and false otherwise.
404+
func (n *networkManager) removeInterface(iface string) (bool, error) {
389405
configFilePath := n.networkManagerConfigFilePath(iface)
390406

391407
_, err := os.Stat(configFilePath)
392408
if err != nil {
393409
if !os.IsNotExist(err) {
394-
return fmt.Errorf("unable to remove %q interface, stat failed on %q: %w", iface, configFilePath, err)
410+
return false, fmt.Errorf("unable to remove %q interface, stat failed on %q: %w", iface, configFilePath, err)
395411
}
396412
logger.Debugf("NetworkManager's configuration file %q doesn't exist, ignoring.", configFilePath)
397-
return nil
413+
return false, nil
398414
}
399415

400416
config := new(nmConfig)
401417
if err := readIniFile(configFilePath, config); err != nil {
402-
return fmt.Errorf("failed to load NetworkManager %q file: %v", configFilePath, err)
418+
return false, fmt.Errorf("failed to load NetworkManager %q file: %v", configFilePath, err)
403419
}
404420

405421
if config.GuestAgent.ManagedByGuestAgent {
406422
logger.Debugf("Attempting to remove NetworkManager configuration %s", configFilePath)
407423

408424
if err = os.Remove(configFilePath); err != nil {
409-
return fmt.Errorf("error deleting config file for %s: %v", iface, err)
425+
return false, fmt.Errorf("error deleting config file for %s: %v", iface, err)
410426
}
411427
}
412-
return nil
428+
return true, nil
413429
}

0 commit comments

Comments
 (0)