mirror of
https://github.com/siderolabs/talos.git
synced 2026-05-05 12:26:21 +02:00
fix: correctly predict interface name on darwin
Old implementation didn't work if the interface to be created wasn't the biggest index. For example if interfaces `bridge100` and `bridge102` already existed, vmnet would create a `bridge 101`, but the old logic expected a `bridge103`. Signed-off-by: Orzelius <albert.kostusev@siderolabs.com>
This commit is contained in:
parent
cfcfad3c45
commit
3035744a80
@ -5,39 +5,20 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"sync"
|
||||
"slices"
|
||||
)
|
||||
|
||||
var vmnetInterfaceRegex = sync.OnceValue(func() *regexp.Regexp { return regexp.MustCompile(`\bbridge(1\d\d)\b`) })
|
||||
|
||||
// GetVmnetInterfaceName returns the name of the interface that will be assigned to the next vmnet interface.
|
||||
// The name is assigned incrementing by one, starting from "bridge100".
|
||||
func GetVmnetInterfaceName(allCurrentInterfaces []string) (string, error) {
|
||||
vmnetInterfaceFound := false
|
||||
largestVmnetIfIndex := 100
|
||||
|
||||
for _, iface := range allCurrentInterfaces {
|
||||
matches := vmnetInterfaceRegex().FindSubmatch([]byte(iface))
|
||||
if matches != nil {
|
||||
vmnetInterfaceFound = true
|
||||
|
||||
index, err := strconv.Atoi(string(matches[1]))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse interface name: %w", err)
|
||||
}
|
||||
|
||||
if index > largestVmnetIfIndex {
|
||||
largestVmnetIfIndex = index
|
||||
}
|
||||
for i := 100; i < 200; i++ {
|
||||
interfaceName := fmt.Sprintf("bridge%d", i)
|
||||
if slices.Index(allCurrentInterfaces, interfaceName) == -1 {
|
||||
return interfaceName, nil
|
||||
}
|
||||
}
|
||||
|
||||
if !vmnetInterfaceFound {
|
||||
return "bridge100", nil
|
||||
}
|
||||
|
||||
return "bridge" + strconv.Itoa(largestVmnetIfIndex+1), nil
|
||||
return "", errors.New("all interface names seem to be already in use")
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func TestGetVmnetInterfaceNameNoVmnetInterface(t *testing.T) {
|
||||
|
||||
func TestGetVmnetInterfaceNameWithExistingVmnetInterfaces(t *testing.T) {
|
||||
interfaces := []string{
|
||||
"bridge", "bridge1", "eth0", "utun1", "bridge001", "bridge1001", "bridge100", "bridge101",
|
||||
"bridge", "bridge1", "eth0", "utun1", "bridge001", "bridge1001", "bridge100", "bridge101", "bridge104",
|
||||
}
|
||||
result, err := vm.GetVmnetInterfaceName(interfaces)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user