Skip to content

Commit

Permalink
Avoid writing configuration files when they already exist on wicked a…
Browse files Browse the repository at this point in the history
…nd (#410)

NetworkManager, nits in netplan
  • Loading branch information
drewhli authored Aug 8, 2024
1 parent 659fce6 commit 12ec8a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions google_guest_agent/network/manager/netplan_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (n netplan) IsManaging(ctx context.Context, iface string) (bool, error) {
if errors.Is(err, exec.ErrNotFound) {
return false, nil
}
return false, fmt.Errorf("error looking up dhclient path: %v", err)
return false, fmt.Errorf("error looking up netplan path: %v", err)
}
return true, nil
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (n netplan) SetupEthernetInterface(ctx context.Context, config *cfg.Section
return fmt.Errorf("error reloading systemd-networkd network configs: %v", err)
}

// Avoid restarting systemd-networkd.
// Avoid restarting netplan.
if err := run.Quiet(ctx, "netplan", "apply"); err != nil {
return fmt.Errorf("error applying netplan changes: %w", err)
}
Expand Down
17 changes: 10 additions & 7 deletions google_guest_agent/network/manager/network_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,17 @@ func (n networkManager) writeNetworkManagerConfigs(ifaces []string) ([]string, e
return []string{}, fmt.Errorf("error updating permissions for %s connection config: %v", iface, err)
}

// Clean up the files written by the old agent. Make sure they're managed
// by the agent before deleting them.
ifcfgFilePath := n.ifcfgFilePath(iface)
_, err := os.Stat(ifcfgFilePath)
if err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to stat ifcfg file(%s): %v", ifcfgFilePath, err)
}
} else {
if err := os.Remove(ifcfgFilePath); err != nil {
contents, err := os.ReadFile(ifcfgFilePath)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to read ifcfg file(%s): %v", ifcfgFilePath, err)
}

// Check for the google comment.
if strings.Contains(string(contents), "# Added by Google Compute Engine OS Login.") {
if err = os.Remove(ifcfgFilePath); err != nil {
return nil, fmt.Errorf("failed to remove previously managed ifcfg file(%s): %v", ifcfgFilePath, err)
}
}
Expand Down
23 changes: 14 additions & 9 deletions google_guest_agent/network/manager/wicked_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,28 @@ func (n wicked) writeEthernetConfigs(ifaces []string) error {
// Write the config for all the non-primary network interfaces.
for _, iface := range ifaces {
logger.Debugf("write enabling ifcfg-%s config", iface)

var ifcfg *os.File

ifcfg, err := os.Create(n.ifcfgFilePath(iface))
if err != nil {
return err
ifcfg := n.ifcfgFilePath(iface)

// Avoid writing the configuration file if the configuration already exists.
if _, err := os.Stat(ifcfg); err != nil {
if os.IsNotExist(err) {
logger.Debugf("%s already exists, skipping", ifcfg)
continue
}
return fmt.Errorf("error getting config file %s: %v", ifcfg, err)
}
defer ifcfg.Close()

contents := []string{
googleComment,
"STARTMODE=hotplug",
// NOTE: 'dhcp' is the dhcp4+dhcp6 option.
"BOOTPROTO=dhcp",
fmt.Sprintf("DHCLIENT_ROUTE_PRIORITY=%d", priority),
}
_, err = ifcfg.WriteString(strings.Join(contents, "\n"))
if err != nil {
contentBytes := []byte(strings.Join(contents, "\n"))

// Write the file.
if err := os.WriteFile(ifcfg, contentBytes, 0644); err != nil {
return fmt.Errorf("error writing config file for %s: %v", iface, err)
}
priority += 100
Expand Down

0 comments on commit 12ec8a6

Please sign in to comment.