Skip to content

Commit 006b8ab

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

File tree

10 files changed

+137
-64
lines changed

10 files changed

+137
-64
lines changed

pkg/driver/driver.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package driver
55

66
import (
77
"context"
8+
"encoding/json"
9+
"errors"
810
"net"
911

1012
"github.com/lima-vm/lima/v2/pkg/store"
@@ -79,18 +81,78 @@ type Driver interface {
7981

8082
Info() Info
8183

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

8688
type ConfiguredDriver struct {
8789
Driver
8890
}
8991

92+
type ConfigOption func(*Options)
93+
94+
type Options struct {
95+
Instance *store.Instance `json:"instance"`
96+
SSHLocalPort int `json:"sshLocalPort,omitempty"`
97+
}
98+
99+
func WithSSHLocalPort(port int) ConfigOption {
100+
return func(opts *Options) {
101+
opts.SSHLocalPort = port
102+
}
103+
}
104+
105+
func WithInstance(instance *store.Instance) ConfigOption {
106+
return func(opts *Options) {
107+
opts.Instance = instance
108+
}
109+
}
110+
111+
func WithOptions(options Options) ConfigOption {
112+
return func(opts *Options) {
113+
*opts = options
114+
}
115+
}
116+
90117
type Info struct {
91118
DriverName string `json:"driverName"`
92119
CanRunGUI bool `json:"canRunGui,omitempty"`
93120
VsockPort int `json:"vsockPort"`
94121
VirtioPort string `json:"virtioPort"`
95122
InstanceDir string `json:"instanceDir,omitempty"`
96123
}
124+
125+
func (o *Options) MarshalJSON() ([]byte, error) {
126+
type Alias Options
127+
errorsAsStrings := make([]string, len(o.Instance.Errors))
128+
for i, err := range o.Instance.Errors {
129+
if err != nil {
130+
errorsAsStrings[i] = err.Error()
131+
}
132+
}
133+
return json.Marshal(&struct {
134+
*Alias
135+
Errors []string `json:"errors,omitempty"`
136+
}{
137+
Alias: (*Alias)(o),
138+
Errors: errorsAsStrings,
139+
})
140+
}
141+
142+
func (o *Options) UnmarshalJSON(data []byte) error {
143+
type Alias Options
144+
aux := &struct {
145+
*Alias
146+
Errors []string `json:"errors,omitempty"`
147+
}{
148+
Alias: (*Alias)(o),
149+
}
150+
if err := json.Unmarshal(data, &aux); err != nil {
151+
return err
152+
}
153+
o.Instance.Errors = nil
154+
for _, msg := range aux.Errors {
155+
o.Instance.Errors = append(o.Instance.Errors, errors.New(msg))
156+
}
157+
return nil
158+
}

pkg/driver/external/client/methods.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/lima-vm/lima/v2/pkg/driver"
1616
pb "github.com/lima-vm/lima/v2/pkg/driver/external"
17-
"github.com/lima-vm/lima/v2/pkg/store"
1817
)
1918

2019
func (d *DriverClient) Validate() error {
@@ -281,28 +280,32 @@ func (d *DriverClient) Info() driver.Info {
281280
return info
282281
}
283282

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)
283+
func (d *DriverClient) Configure(opts ...driver.ConfigOption) *driver.ConfiguredDriver {
284+
options := driver.Options{}
285+
for _, o := range opts {
286+
o(&options)
287+
}
288+
289+
d.logger.Debugf("Setting config for instance %s with options %v", options.Instance.Name, options)
286290

287-
instJSON, err := inst.MarshalJSON()
291+
configOptsJSON, err := options.MarshalJSON()
288292
if err != nil {
289-
d.logger.Errorf("Failed to marshal instance config: %v", err)
293+
d.logger.Errorf("Failed to marshal config options: %v", err)
290294
return nil
291295
}
292296

293297
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
294298
defer cancel()
295299

296300
_, err = d.DriverSvc.SetConfig(ctx, &pb.SetConfigRequest{
297-
InstanceConfigJson: instJSON,
298-
SshLocalPort: int64(sshLocalPort),
301+
ConfigOpts: configOptsJSON,
299302
})
300303
if err != nil {
301304
d.logger.Errorf("Failed to set config: %v", err)
302305
return nil
303306
}
304307

305-
d.logger.Debugf("Config set successfully for instance %s", inst.Name)
308+
d.logger.Debugf("Config set successfully for instance %s", options.Instance.Name)
306309
return &driver.ConfiguredDriver{
307310
Driver: d,
308311
}

pkg/driver/external/driver.pb.go

Lines changed: 10 additions & 18 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ message StartResponse {
4141
}
4242

4343
message SetConfigRequest {
44-
bytes instance_config_json = 1;
45-
int64 ssh_local_port = 3;
44+
bytes config_opts = 1;
4645
}
4746

4847
message ChangeDisplayPasswordRequest {

pkg/driver/external/server/methods.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ 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"
19-
"github.com/lima-vm/lima/v2/pkg/store"
2020
"github.com/lima-vm/lima/v2/pkg/store/filenames"
2121
)
2222

@@ -55,14 +55,14 @@ func (s *DriverServer) Start(_ *emptypb.Empty, stream pb.Driver_StartServer) err
5555

5656
func (s *DriverServer) SetConfig(_ context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
5757
s.logger.Debugf("Received SetConfig request")
58-
var inst store.Instance
58+
var configOpts driver.Options
5959

60-
if err := inst.UnmarshalJSON(req.InstanceConfigJson); err != nil {
60+
if err := configOpts.UnmarshalJSON(req.ConfigOpts); err != nil {
6161
s.logger.Errorf("Failed to unmarshal InstanceConfigJson: %v", err)
6262
return &emptypb.Empty{}, err
6363
}
6464

65-
_ = s.driver.Configure(&inst, int(req.SshLocalPort))
65+
_ = s.driver.Configure(driver.WithOptions(configOpts))
6666

6767
return &emptypb.Empty{}, nil
6868
}

pkg/driver/qemu/qemu_driver.go

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

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

7175
return &driver.ConfiguredDriver{
7276
Driver: l,

pkg/driver/vz/vz_driver_darwin.go

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

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

9498
return &driver.ConfiguredDriver{
9599
Driver: l,

pkg/driver/wsl2/wsl_driver_windows.go

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

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

7579
return &driver.ConfiguredDriver{
7680
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+
driver.WithInstance(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+
driver.WithInstance(inst),
47+
driver.WithSSHLocalPort(sshLocalPort),
48+
), nil
4349
}

pkg/registry/registry_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"gotest.tools/v3/assert"
1515

1616
"github.com/lima-vm/lima/v2/pkg/driver"
17-
"github.com/lima-vm/lima/v2/pkg/store"
1817
)
1918

2019
type mockDriver struct {
@@ -27,24 +26,24 @@ func newMockDriver(name string) *mockDriver {
2726

2827
var _ driver.Driver = (*mockDriver)(nil)
2928

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

4948
func TestRegister(t *testing.T) {
5049
BackupRegistry(t)

0 commit comments

Comments
 (0)