Skip to content

Commit b60a4c2

Browse files
committed
pass sshlocalport as an option to configure driver
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent d4056f9 commit b60a4c2

File tree

10 files changed

+102
-43
lines changed

10 files changed

+102
-43
lines changed

pkg/driver/driver.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,32 @@ type Driver interface {
7979

8080
Info() Info
8181

82-
// SetConfig sets the configuration for the instance.
83-
Configure(inst *store.Instance, sshLocalPort int) *ConfiguredDriver
82+
// Configure sets the configuration for the instance.
83+
Configure(inst *store.Instance, opts ...ConfigOption) *ConfiguredDriver
8484
}
8585

8686
type ConfiguredDriver struct {
8787
Driver
8888
}
8989

90+
type ConfigOption func(*Options)
91+
92+
type Options struct {
93+
SSHLocalPort int `json:"sshLocalPort,omitempty"`
94+
}
95+
96+
func WithSSHLocalPort(port int) ConfigOption {
97+
return func(opts *Options) {
98+
opts.SSHLocalPort = port
99+
}
100+
}
101+
102+
func WithOptions(options Options) ConfigOption {
103+
return func(opts *Options) {
104+
*opts = options
105+
}
106+
}
107+
90108
type Info struct {
91109
DriverName string `json:"driverName"`
92110
CanRunGUI bool `json:"canRunGui,omitempty"`

pkg/driver/external/client/methods.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,23 @@ func (d *DriverClient) Info() driver.Info {
281281
return info
282282
}
283283

284-
func (d *DriverClient) Configure(inst *store.Instance, sshLocalPort int) *driver.ConfiguredDriver {
285-
d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, sshLocalPort)
284+
func (d *DriverClient) Configure(inst *store.Instance, opts ...driver.ConfigOption) *driver.ConfiguredDriver {
285+
options := driver.Options{}
286+
for _, o := range opts {
287+
o(&options)
288+
}
289+
290+
d.logger.Debugf("Setting config for instance %s with options %v", inst.Name, options)
286291

287292
instJSON, err := inst.MarshalJSON()
288293
if err != nil {
289-
d.logger.Errorf("Failed to marshal instance config: %v", err)
294+
d.logger.Errorf("Failed to marshal instance: %v", err)
295+
return nil
296+
}
297+
298+
configOptsJSON, err := json.Marshal(options)
299+
if err != nil {
300+
d.logger.Errorf("Failed to marshal config options: %v", err)
290301
return nil
291302
}
292303

@@ -295,7 +306,7 @@ func (d *DriverClient) Configure(inst *store.Instance, sshLocalPort int) *driver
295306

296307
_, err = d.DriverSvc.SetConfig(ctx, &pb.SetConfigRequest{
297308
InstanceConfigJson: instJSON,
298-
SshLocalPort: int64(sshLocalPort),
309+
ConfigOpts: configOptsJSON,
299310
})
300311
if err != nil {
301312
d.logger.Errorf("Failed to set config: %v", err)

pkg/driver/external/driver.pb.go

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/driver/external/driver.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ message StartResponse {
4242

4343
message SetConfigRequest {
4444
bytes instance_config_json = 1;
45-
int64 ssh_local_port = 3;
45+
bytes config_opts = 2;
4646
}
4747

4848
message ChangeDisplayPasswordRequest {

pkg/driver/external/server/methods.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"google.golang.org/protobuf/types/known/emptypb"
1616

1717
"github.com/lima-vm/lima/v2/pkg/bicopy"
18+
"github.com/lima-vm/lima/v2/pkg/driver"
1819
pb "github.com/lima-vm/lima/v2/pkg/driver/external"
1920
"github.com/lima-vm/lima/v2/pkg/store"
2021
"github.com/lima-vm/lima/v2/pkg/store/filenames"
@@ -55,14 +56,19 @@ func (s *DriverServer) Start(_ *emptypb.Empty, stream pb.Driver_StartServer) err
5556

5657
func (s *DriverServer) SetConfig(_ context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
5758
s.logger.Debugf("Received SetConfig request")
59+
var configOpts driver.Options
5860
var inst store.Instance
59-
6061
if err := inst.UnmarshalJSON(req.InstanceConfigJson); err != nil {
6162
s.logger.Errorf("Failed to unmarshal InstanceConfigJson: %v", err)
6263
return &emptypb.Empty{}, err
6364
}
6465

65-
_ = s.driver.Configure(&inst, int(req.SshLocalPort))
66+
if err := json.Unmarshal(req.ConfigOpts, &configOpts); err != nil {
67+
s.logger.Errorf("Failed to unmarshal InstanceConfigJson: %v", err)
68+
return &emptypb.Empty{}, err
69+
}
70+
71+
_ = s.driver.Configure(&inst, driver.WithOptions(configOpts))
6672

6773
return &emptypb.Empty{}, nil
6874
}

pkg/driver/qemu/qemu_driver.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ func New() *LimaQemuDriver {
6464
}
6565
}
6666

67-
func (l *LimaQemuDriver) Configure(inst *store.Instance, sshLocalPort int) *driver.ConfiguredDriver {
67+
func (l *LimaQemuDriver) Configure(inst *store.Instance, opts ...driver.ConfigOption) *driver.ConfiguredDriver {
6868
l.Instance = inst
69-
l.SSHLocalPort = sshLocalPort
69+
70+
options := driver.Options{}
71+
for _, o := range opts {
72+
o(&options)
73+
}
74+
l.SSHLocalPort = options.SSHLocalPort
7075

7176
return &driver.ConfiguredDriver{
7277
Driver: l,

pkg/driver/vz/vz_driver_darwin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ func New() *LimaVzDriver {
8787
}
8888
}
8989

90-
func (l *LimaVzDriver) Configure(inst *store.Instance, sshLocalPort int) *driver.ConfiguredDriver {
90+
func (l *LimaVzDriver) Configure(inst *store.Instance, opts ...driver.ConfigOption) *driver.ConfiguredDriver {
9191
l.Instance = inst
92-
l.SSHLocalPort = sshLocalPort
92+
93+
options := driver.Options{}
94+
for _, o := range opts {
95+
o(&options)
96+
}
97+
l.SSHLocalPort = options.SSHLocalPort
9398

9499
return &driver.ConfiguredDriver{
95100
Driver: l,

pkg/driver/wsl2/wsl_driver_windows.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ func New() *LimaWslDriver {
6868
}
6969
}
7070

71-
func (l *LimaWslDriver) Configure(inst *store.Instance, sshLocalPort int) *driver.ConfiguredDriver {
71+
func (l *LimaWslDriver) Configure(inst *store.Instance, opts ...driver.ConfigOption) *driver.ConfiguredDriver {
7272
l.Instance = inst
73-
l.SSHLocalPort = sshLocalPort
73+
74+
options := driver.Options{}
75+
for _, o := range opts {
76+
o(&options)
77+
}
78+
l.SSHLocalPort = options.SSHLocalPort
7479

7580
return &driver.ConfiguredDriver{
7681
Driver: l,

pkg/driverutil/instance.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ func CreateConfiguredDriver(inst *store.Instance, sshLocalPort int) (*driver.Con
3535
extDriver.InstanceName = inst.Name
3636
}
3737

38-
return extDriver.Client.Configure(inst, sshLocalPort), nil
38+
return extDriver.Client.Configure(
39+
inst,
40+
driver.WithSSHLocalPort(sshLocalPort),
41+
), nil
3942
}
4043

4144
logrus.Debugf("Using internal driver %q", intDriver.Info().DriverName)
42-
return intDriver.Configure(inst, sshLocalPort), nil
45+
return intDriver.Configure(
46+
inst,
47+
driver.WithSSHLocalPort(sshLocalPort),
48+
), nil
4349
}

pkg/registry/registry_test.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@ func newMockDriver(name string) *mockDriver {
2727

2828
var _ driver.Driver = (*mockDriver)(nil)
2929

30-
func (m *mockDriver) Validate() error { return nil }
31-
func (m *mockDriver) Initialize(_ context.Context) error { return nil }
32-
func (m *mockDriver) CreateDisk(_ context.Context) error { return nil }
33-
func (m *mockDriver) Start(_ context.Context) (chan error, error) { return nil, nil }
34-
func (m *mockDriver) Stop(_ context.Context) error { return nil }
35-
func (m *mockDriver) RunGUI() error { return nil }
36-
func (m *mockDriver) ChangeDisplayPassword(_ context.Context, _ string) error { return nil }
37-
func (m *mockDriver) DisplayConnection(_ context.Context) (string, error) { return "", nil }
38-
func (m *mockDriver) CreateSnapshot(_ context.Context, _ string) error { return nil }
39-
func (m *mockDriver) ApplySnapshot(_ context.Context, _ string) error { return nil }
40-
func (m *mockDriver) DeleteSnapshot(_ context.Context, _ string) error { return nil }
41-
func (m *mockDriver) ListSnapshots(_ context.Context) (string, error) { return "", nil }
42-
func (m *mockDriver) Register(_ context.Context) error { return nil }
43-
func (m *mockDriver) Unregister(_ context.Context) error { return nil }
44-
func (m *mockDriver) ForwardGuestAgent() bool { return false }
45-
func (m *mockDriver) GuestAgentConn(_ context.Context) (net.Conn, string, error) { return nil, "", nil }
46-
func (m *mockDriver) Info() driver.Info { return driver.Info{DriverName: m.Name} }
47-
func (m *mockDriver) Configure(_ *store.Instance, _ int) *driver.ConfiguredDriver { return nil }
30+
func (m *mockDriver) Validate() error { return nil }
31+
func (m *mockDriver) Initialize(_ context.Context) error { return nil }
32+
func (m *mockDriver) CreateDisk(_ context.Context) error { return nil }
33+
func (m *mockDriver) Start(_ context.Context) (chan error, error) { return nil, nil }
34+
func (m *mockDriver) Stop(_ context.Context) error { return nil }
35+
func (m *mockDriver) RunGUI() error { return nil }
36+
func (m *mockDriver) ChangeDisplayPassword(_ context.Context, _ string) error { return nil }
37+
func (m *mockDriver) DisplayConnection(_ context.Context) (string, error) { return "", nil }
38+
func (m *mockDriver) CreateSnapshot(_ context.Context, _ string) error { return nil }
39+
func (m *mockDriver) ApplySnapshot(_ context.Context, _ string) error { return nil }
40+
func (m *mockDriver) DeleteSnapshot(_ context.Context, _ string) error { return nil }
41+
func (m *mockDriver) ListSnapshots(_ context.Context) (string, error) { return "", nil }
42+
func (m *mockDriver) Register(_ context.Context) error { return nil }
43+
func (m *mockDriver) Unregister(_ context.Context) error { return nil }
44+
func (m *mockDriver) ForwardGuestAgent() bool { return false }
45+
func (m *mockDriver) GuestAgentConn(_ context.Context) (net.Conn, string, error) { return nil, "", nil }
46+
func (m *mockDriver) Info() driver.Info { return driver.Info{DriverName: m.Name} }
47+
func (m *mockDriver) Configure(_ *store.Instance, _ ...driver.ConfigOption) *driver.ConfiguredDriver {
48+
return nil
49+
}
4850

4951
func TestRegister(t *testing.T) {
5052
BackupRegistry(t)

0 commit comments

Comments
 (0)