func()

in cmd/core_plugin/network/wicked/wicked_linux.go [195:252]


func (sn *serviceWicked) cleanupVlanInterfaces(ctx context.Context, keepMe []string) error {
	galog.Debugf("Cleaning up old wicked interfaces.")

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

	configExp := `(?P<prefix>ifcfg)-(?P<parent>.*)\.(?P<vlan>.*)`
	configRegex := regexp.MustCompile(configExp)

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

		fileName := file.Name()
		filePath := path.Join(sn.configDir, fileName)
		groups := regex.GroupsMap(configRegex, fileName)

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

		// If it's not a vlan interface skip it.
		vlan, foundVlan := groups["vlan"]
		if !foundVlan {
			galog.Debugf("Skipping non-vlan interface ifcfg file: %s", filePath)
			continue
		}

		galog.V(2).Debugf("Vlan interface's ID: %s", vlan)
		iface := fmt.Sprintf("%s.%s", parent, vlan)

		// Don't remove the interface if it's in the list of interfaces to keep.
		if slices.Contains(keepMe, iface) {
			continue
		}

		removed, err := sn.removeInterface(ctx, filePath)
		if err != nil {
			return fmt.Errorf("failed to remove vlan interface: %+v", err)
		}

		if !removed {
			continue
		}

		opt := run.Options{OutputType: run.OutputNone, Name: "wicked", Args: []string{"ifdown", iface}}
		if _, err := run.WithContext(ctx, opt); err != nil {
			return fmt.Errorf("error disabling interfaces: %w", err)
		}
	}

	return nil
}