Skip to content

Commit eb2383f

Browse files
committed
move SSHAddress() to drivers and add driver features
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 2cee8d2 commit eb2383f

File tree

8 files changed

+56
-18
lines changed

8 files changed

+56
-18
lines changed

pkg/driver/driver.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,24 @@ type Driver interface {
8080

8181
AcceptConfig(cfg *limatype.LimaYAML, filepath string) error
8282
FillConfig(cfg *limatype.LimaYAML, filePath string) error
83+
84+
SSHAddress(ctx context.Context) (string, error)
8385
}
8486

8587
type ConfiguredDriver struct {
8688
Driver
8789
}
8890

8991
type Info struct {
90-
DriverName string `json:"driverName"`
91-
CanRunGUI bool `json:"canRunGui,omitempty"`
92-
VsockPort int `json:"vsockPort"`
93-
VirtioPort string `json:"virtioPort"`
94-
InstanceDir string `json:"instanceDir,omitempty"`
92+
DriverName string `json:"driverName"`
93+
CanRunGUI bool `json:"canRunGui,omitempty"`
94+
VsockPort int `json:"vsockPort"`
95+
VirtioPort string `json:"virtioPort"`
96+
InstanceDir string `json:"instanceDir,omitempty"`
97+
Features DriverFeatures `json:"features"`
98+
}
99+
100+
type DriverFeatures struct {
101+
DynamicSSHAddress bool `json:"dynamicSSHAddress"`
102+
SkipSocketForwarding bool `json:"skipSocketForwarding"`
95103
}

pkg/driver/external/client/methods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,7 @@ func (d *DriverClient) FillConfig(cfg *limatype.LimaYAML, filepath string) error
323323
func (d *DriverClient) InspectStatus(_ context.Context, _ string) string {
324324
return ""
325325
}
326+
327+
func (d *DriverClient) SSHAddress(ctx context.Context) (string, error) {
328+
return "", nil
329+
}

pkg/driver/qemu/qemu_driver.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,18 @@ func (l *LimaQemuDriver) Info() driver.Info {
638638
info.CanRunGUI = false
639639
info.VirtioPort = l.virtioPort
640640
info.VsockPort = l.vSockPort
641+
642+
info.Features = driver.DriverFeatures{
643+
DynamicSSHAddress: false,
644+
SkipSocketForwarding: false,
645+
}
641646
return info
642647
}
643648

649+
func (l *LimaQemuDriver) SSHAddress(ctx context.Context) (string, error) {
650+
return "127.0.0.1", nil
651+
}
652+
644653
func (l *LimaQemuDriver) InspectStatus(_ context.Context, instName string) string {
645654
return ""
646655
}

pkg/driver/vz/vz_driver_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,16 @@ func (l *LimaVzDriver) Info() driver.Info {
313313
if l.Instance != nil {
314314
info.InstanceDir = l.Instance.Dir
315315
}
316+
317+
info.Features = driver.DriverFeatures{
318+
DynamicSSHAddress: false,
319+
SkipSocketForwarding: false,
320+
}
316321
return info
317322
}
323+
func (l *LimaVzDriver) SSHAddress(ctx context.Context) (string, error) {
324+
return "127.0.0.1", nil
325+
}
318326

319327
func (l *LimaVzDriver) InspectStatus(_ context.Context, instName string) string {
320328
return ""

pkg/driver/wsl2/wsl_driver_windows.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/lima-vm/lima/v2/pkg/limayaml"
2020
"github.com/lima-vm/lima/v2/pkg/ptr"
2121
"github.com/lima-vm/lima/v2/pkg/reflectutil"
22-
"github.com/lima-vm/lima/v2/pkg/store"
2322
"github.com/lima-vm/lima/v2/pkg/windows"
2423
)
2524

@@ -170,7 +169,7 @@ func (l *LimaWslDriver) InspectStatus(ctx context.Context, instName string) stri
170169
l.Instance.SSHLocalPort = 22
171170

172171
if l.Instance.Status == limatype.StatusRunning {
173-
sshAddr, err := store.GetSSHAddress(instName)
172+
sshAddr, err := l.SSHAddress(ctx)
174173
if err == nil {
175174
l.Instance.SSHAddress = sshAddr
176175
} else {
@@ -284,9 +283,18 @@ func (l *LimaWslDriver) Info() driver.Info {
284283
info.CanRunGUI = l.canRunGUI()
285284
info.VirtioPort = l.virtioPort
286285
info.VsockPort = l.vSockPort
286+
287+
info.Features = driver.DriverFeatures{
288+
DynamicSSHAddress: true,
289+
SkipSocketForwarding: true,
290+
}
287291
return info
288292
}
289293

294+
func (l *LimaWslDriver) SSHAddress(_ context.Context) (string, error) {
295+
return "127.0.0.1", nil
296+
}
297+
290298
func (l *LimaWslDriver) Create(_ context.Context) error {
291299
return nil
292300
}

pkg/hostagent/hostagent.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
116116
if err != nil {
117117
return nil, err
118118
}
119-
if *inst.Config.VMType == limatype.WSL2 {
120-
sshLocalPort = inst.SSHLocalPort
121-
}
122119

123120
var udpDNSLocalPort, tcpDNSLocalPort int
124121
if *inst.Config.HostResolver.Enabled {
@@ -137,6 +134,14 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
137134
return nil, fmt.Errorf("failed to create driver instance: %w", err)
138135
}
139136

137+
if limaDriver.Info().Features.DynamicSSHAddress {
138+
sshLocalPort = inst.SSHLocalPort
139+
limaDriver, err = driverutil.CreateConfiguredDriver(inst, sshLocalPort)
140+
if err != nil {
141+
return nil, fmt.Errorf("failed to recreate driver instance: %w", err)
142+
}
143+
}
144+
140145
vSockPort := limaDriver.Info().VsockPort
141146
virtioPort := limaDriver.Info().VirtioPort
142147

@@ -309,9 +314,8 @@ func (a *HostAgent) Run(ctx context.Context) error {
309314
return err
310315
}
311316

312-
// WSL instance SSH address isn't known until after VM start
313-
if *a.instConfig.VMType == limatype.WSL2 {
314-
sshAddr, err := store.GetSSHAddress(a.instName)
317+
if a.driver.Info().Features.DynamicSSHAddress {
318+
sshAddr, err := a.driver.SSHAddress(ctx)
315319
if err != nil {
316320
return err
317321
}
@@ -534,7 +538,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
534538
// TODO: use vSock (when QEMU for macOS gets support for vSock)
535539

536540
// Setup all socket forwards and defer their teardown
537-
if *a.instConfig.VMType != limatype.WSL2 {
541+
if !a.driver.Info().Features.SkipSocketForwarding {
538542
logrus.Debugf("Forwarding unix sockets")
539543
for _, rule := range a.instConfig.PortForwards {
540544
if rule.GuestSocket != "" {

pkg/registry/registry_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (m *mockDriver) Configure(_ *limatype.Instance) *driver.ConfiguredDriver
4949
func (m *mockDriver) AcceptConfig(_ *limatype.LimaYAML, _ string) error { return nil }
5050
func (m *mockDriver) FillConfig(_ *limatype.LimaYAML, _ string) error { return nil }
5151
func (m *mockDriver) InspectStatus(_ context.Context, _ string) string { return "" }
52+
func (m *mockDriver) SSHAddress(_ context.Context) (string, error) { return "", nil }
5253

5354
func TestRegister(t *testing.T) {
5455
BackupRegistry(t)

pkg/store/instance.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ func inspectStatus(instDir string, inst *limatype.Instance, y *limatype.LimaYAML
165165
inst.Status = status
166166
}
167167

168-
func GetSSHAddress(_ string) (string, error) {
169-
return "127.0.0.1", nil
170-
}
171-
172168
func inspectStatusWithPIDFiles(instDir string, inst *limatype.Instance, y *limatype.LimaYAML) {
173169
var err error
174170
inst.DriverPID, err = ReadPIDFile(filepath.Join(instDir, filenames.PIDFile(*y.VMType)))

0 commit comments

Comments
 (0)