Skip to content

Commit f4c1c04

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 dc27394 commit f4c1c04

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

internal/localapi/utils.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,45 @@ 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+
return nil, nil, err
59+
}
60+
61+
if state != define.Running {
62+
return nil, nil, fmt.Errorf("machine %s is not running but in state %s", mc.Name, state)
63+
}
64+
65+
return mc, machineProvider, nil
66+
}
6667
}
6768

6869
return nil, nil, fmt.Errorf("could not find a matching machine for connection %q", connectionURI)
@@ -203,7 +204,7 @@ func IsHyperVProvider(ctx context.Context) (bool, error) {
203204

204205
_, vmType, err := getMachineMountsAndVMType(conn.URI.String(), conn.URI)
205206
if err != nil {
206-
logrus.Debugf("Failed to get machine mounts: %v", err)
207+
logrus.Debugf("Failed to get machine hypervisor type: %v", err)
207208
return false, err
208209
}
209210

0 commit comments

Comments
 (0)