Skip to content

Commit 1656c90

Browse files
committed
Iterate through all machine providers in FindMachineByPort
Replace single provider.Get() with provider.GetAll() loop to search across all available machine providers when finding machine by port. Signed-off-by: Jan Rodák <[email protected]>
1 parent a1e7e9a commit 1656c90

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

internal/localapi/utils.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,46 @@ import (
2626
// FindMachineByPort finds a running machine that matches the given connection port.
2727
// It returns the machine configuration and provider, or an error if not found.
2828
func FindMachineByPort(connectionURI string, parsedConnection *url.URL) (*vmconfigs.MachineConfig, vmconfigs.VMProvider, error) {
29-
machineProvider, err := provider.Get()
30-
if err != nil {
31-
return nil, nil, fmt.Errorf("getting machine provider: %w", err)
32-
}
33-
34-
dirs, err := env.GetMachineDirs(machineProvider.VMType())
35-
if err != nil {
36-
return nil, nil, err
37-
}
38-
39-
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
40-
if err != nil {
41-
return nil, nil, fmt.Errorf("listing machines: %w", err)
42-
}
43-
44-
// Now we know that the connection points to a machine and we
45-
// can find the machine by looking for the one with the
46-
// matching port.
47-
connectionPort, err := strconv.Atoi(parsedConnection.Port())
48-
if err != nil {
49-
return nil, nil, fmt.Errorf("parsing connection port: %w", err)
50-
}
51-
52-
for _, mc := range machineList {
53-
if connectionPort != mc.SSH.Port {
29+
for _, machineProvider := range provider.GetAll() {
30+
logrus.Debugf("Checking provider: %s", machineProvider.VMType())
31+
dirs, err := env.GetMachineDirs(machineProvider.VMType())
32+
if err != nil {
33+
logrus.Debugf("Failed to get machine dirs for provider %s: %v", machineProvider.VMType(), err)
5434
continue
5535
}
5636

57-
state, err := machineProvider.State(mc, false)
37+
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
5838
if err != nil {
59-
return nil, nil, err
39+
logrus.Debugf("Failed to list machines: %v", err)
40+
continue
6041
}
6142

62-
if state != define.Running {
63-
return nil, nil, fmt.Errorf("machine %s is not running but in state %s", mc.Name, state)
43+
// Now we know that the connection points to a machine and we
44+
// can find the machine by looking for the one with the
45+
// matching port.
46+
connectionPort, err := strconv.Atoi(parsedConnection.Port())
47+
if err != nil {
48+
logrus.Debugf("Failed to parse connection port: %v", err)
49+
continue
6450
}
6551

66-
return mc, machineProvider, nil
52+
for _, mc := range machineList {
53+
if connectionPort != mc.SSH.Port {
54+
continue
55+
}
56+
57+
state, err := machineProvider.State(mc, false)
58+
if err != nil {
59+
logrus.Debugf("Failed to get machine state for %s: %v", mc.Name, err)
60+
continue
61+
}
62+
63+
if state != define.Running {
64+
return nil, nil, fmt.Errorf("machine %s is not running but in state %s", mc.Name, state)
65+
}
66+
67+
return mc, machineProvider, nil
68+
}
6769
}
6870

6971
return nil, nil, fmt.Errorf("could not find a matching machine for connection %q", connectionURI)
@@ -244,13 +246,13 @@ func IsHyperVProvider(ctx context.Context) (bool, error) {
244246
return false, err
245247
}
246248

247-
_, vmType, err := getMachineMountsAndVMType(conn.URI.String(), conn.URI)
249+
_, vmProvider, err := FindMachineByPort(conn.URI.String(), conn.URI)
248250
if err != nil {
249-
logrus.Debugf("Failed to get machine mounts: %v", err)
251+
logrus.Debugf("Failed to get machine hypervisor type: %v", err)
250252
return false, err
251253
}
252254

253-
return vmType == define.HyperVVirt, nil
255+
return vmProvider.VMType() == define.HyperVVirt, nil
254256
}
255257

256258
// ValidatePathForLocalAPI checks if the provided path satisfies requirements for local API usage.

0 commit comments

Comments
 (0)