func()

in cmd/core_plugin/network/networkd/networkd_linux.go [272:325]


func (sn *Module) cleanupVlanConfigs(keepMe []string) (bool, error) {
	galog.Debugf("Cleaning up systemd-networkd vlan interfaces.")

	if !file.Exists(sn.configDir, file.TypeDir) {
		galog.V(2).Debugf("No systemd-networkd configuration directory found: %s.", sn.configDir)
		return false, nil
	}

	files, err := os.ReadDir(sn.configDir)
	if err != nil {
		return false, fmt.Errorf("failed to read content from %s: %w", sn.configDir, err)
	}

	configExp := `(?P<priority>[0-9]+)-(?P<interface>.*\.[0-9]+)-(?P<suffix>.*)\.(?P<extension>network|netdev)`
	configRegex := regexp.MustCompile(configExp)
	requiresRestart := false

	for _, file := range files {
		// Skip directories.
		if file.IsDir() {
			continue
		}

		fileName := file.Name()
		groups := regex.GroupsMap(configRegex, fileName)

		galog.V(2).Debugf("Vlan file(%q) name extracted groups: %v.", fileName, groups)

		// If we don't have a matching interface skip it.
		currIface, ok := groups["interface"]
		if !ok {
			continue
		}

		// If suffix is not google-guest-agent that means it's not a vlan interface
		// we created.
		if suffix, ok := groups["suffix"]; !ok || suffix != "google-guest-agent" {
			continue
		}

		// If this is an interface still present skip it.
		if slices.Contains(keepMe, currIface) {
			continue
		}

		if err := os.Remove(filepath.Join(sn.configDir, fileName)); err != nil {
			return requiresRestart, fmt.Errorf("failed to remove vlan interface config(%s): %w", fileName, err)
		}

		requiresRestart = true
	}

	return requiresRestart, nil
}