Skip to content

Commit 5a5d58c

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 cfa7a00 commit 5a5d58c

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
@@ -25,44 +25,46 @@ import (
2525
// FindMachineByPort finds a running machine that matches the given connection port.
2626
// It returns the machine configuration and provider, or an error if not found.
2727
func FindMachineByPort(connectionURI string, parsedConnection *url.URL) (*vmconfigs.MachineConfig, vmconfigs.VMProvider, error) {
28-
machineProvider, err := provider.Get()
29-
if err != nil {
30-
return nil, nil, fmt.Errorf("getting machine provider: %w", err)
31-
}
32-
33-
dirs, err := env.GetMachineDirs(machineProvider.VMType())
34-
if err != nil {
35-
return nil, nil, err
36-
}
37-
38-
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
39-
if err != nil {
40-
return nil, nil, fmt.Errorf("listing machines: %w", err)
41-
}
42-
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-
return nil, nil, fmt.Errorf("parsing connection port: %w", err)
49-
}
50-
51-
for _, mc := range machineList {
52-
if connectionPort != mc.SSH.Port {
28+
for _, machineProvider := range provider.GetAll() {
29+
logrus.Debugf("Checking provider: %s", machineProvider.VMType())
30+
dirs, err := env.GetMachineDirs(machineProvider.VMType())
31+
if err != nil {
32+
logrus.Debugf("Failed to get machine dirs for provider %s: %v", machineProvider.VMType(), err)
5333
continue
5434
}
5535

56-
state, err := machineProvider.State(mc, false)
36+
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
5737
if err != nil {
58-
return nil, nil, err
38+
logrus.Debugf("Failed to list machines: %v", err)
39+
continue
5940
}
6041

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

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

6870
return nil, nil, fmt.Errorf("could not find a matching machine for connection %q", connectionURI)
@@ -201,13 +203,13 @@ func IsHyperVProvider(ctx context.Context) (bool, error) {
201203
return false, err
202204
}
203205

204-
_, vmType, err := getMachineMountsAndVMType(conn.URI.String(), conn.URI)
206+
_, vmProvider, err := FindMachineByPort(conn.URI.String(), conn.URI)
205207
if err != nil {
206-
logrus.Debugf("Failed to get machine mounts: %v", err)
208+
logrus.Debugf("Failed to get machine hypervisor type: %v", err)
207209
return false, err
208210
}
209211

210-
return vmType == define.HyperVVirt, nil
212+
return vmProvider.VMType() == define.HyperVVirt, nil
211213
}
212214

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

0 commit comments

Comments
 (0)