Skip to content

Commit 80bf1d9

Browse files
committed
driver(internal): consolidate some functions to single GetInfo() function
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent ed4ecef commit 80bf1d9

File tree

7 files changed

+43
-56
lines changed

7 files changed

+43
-56
lines changed

pkg/driver/driver.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ type Lifecycle interface {
3838

3939
// GUI defines GUI-related operations.
4040
type GUI interface {
41-
// CanRunGUI returns bool to indicate if the hostagent need to run GUI synchronously
42-
CanRunGUI() bool
43-
4441
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
4542
// It returns error if there are any failures
4643
RunGUI() error
@@ -72,23 +69,23 @@ type GuestAgent interface {
7269
GuestAgentConn(_ context.Context) (net.Conn, error)
7370
}
7471

75-
type Plugin interface {
76-
// Name returns the name of the driver
77-
Name() string
78-
79-
// SetConfig sets the configuration for the instance.
80-
SetConfig(inst *store.Instance, sshLocalPort int)
81-
}
82-
8372
// Driver interface is used by hostagent for managing vm.
8473
type Driver interface {
8574
Lifecycle
8675
GUI
8776
SnapshotManager
8877
Registration
8978
GuestAgent
90-
Plugin
9179

92-
GetVSockPort() int
93-
GetVirtioPort() string
80+
GetInfo() Info
81+
82+
// SetConfig sets the configuration for the instance.
83+
SetConfig(inst *store.Instance, sshLocalPort int)
84+
}
85+
86+
type Info struct {
87+
DriverName string
88+
CanRunGUI bool
89+
VsockPort int
90+
VirtioPort string
9491
}

pkg/driver/qemu/qemu_driver.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ func (l *LimaQemuDriver) SetConfig(inst *store.Instance, sshLocalPort int) {
6969
l.SSHLocalPort = sshLocalPort
7070
}
7171

72-
func (l *LimaQemuDriver) GetVirtioPort() string {
73-
return l.VirtioPort
74-
}
75-
76-
func (l *LimaQemuDriver) GetVSockPort() int {
77-
return l.VSockPort
78-
}
79-
8072
func (l *LimaQemuDriver) Validate() error {
8173
if runtime.GOOS == "darwin" {
8274
if err := l.checkBinarySignature(); err != nil {
@@ -519,18 +511,19 @@ func (a *qArgTemplateApplier) applyTemplate(qArg string) (string, error) {
519511
return b.String(), nil
520512
}
521513

522-
func (l *LimaQemuDriver) Name() string {
523-
return "qemu"
514+
func (l *LimaQemuDriver) GetInfo() driver.Info {
515+
return driver.Info{
516+
DriverName: "qemu",
517+
CanRunGUI: false,
518+
VsockPort: l.VSockPort,
519+
VirtioPort: l.VirtioPort,
520+
}
524521
}
525522

526523
func (l *LimaQemuDriver) Initialize(_ context.Context) error {
527524
return nil
528525
}
529526

530-
func (l *LimaQemuDriver) CanRunGUI() bool {
531-
return false
532-
}
533-
534527
func (l *LimaQemuDriver) RunGUI() error {
535528
return nil
536529
}

pkg/driver/vz/vz_driver_darwin.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ func (l *LimaVzDriver) SetConfig(inst *store.Instance, sshLocalPort int) {
9292
l.SSHLocalPort = sshLocalPort
9393
}
9494

95-
func (l *LimaVzDriver) GetVirtioPort() string {
96-
return l.VirtioPort
97-
}
98-
99-
func (l *LimaVzDriver) GetVSockPort() int {
100-
return l.VSockPort
101-
}
102-
10395
func (l *LimaVzDriver) Validate() error {
10496
macOSProductVersion, err := osutil.ProductVersion()
10597
if err != nil {
@@ -208,7 +200,7 @@ func (l *LimaVzDriver) Start(ctx context.Context) (chan error, error) {
208200
return errCh, nil
209201
}
210202

211-
func (l *LimaVzDriver) CanRunGUI() bool {
203+
func (l *LimaVzDriver) canRunGUI() bool {
212204
switch *l.Instance.Config.Video.Display {
213205
case "vz", "default":
214206
return true
@@ -218,7 +210,7 @@ func (l *LimaVzDriver) CanRunGUI() bool {
218210
}
219211

220212
func (l *LimaVzDriver) RunGUI() error {
221-
if l.CanRunGUI() {
213+
if l.canRunGUI() {
222214
return l.machine.StartGraphicApplication(1920, 1200)
223215
}
224216
//nolint:revive // error-strings
@@ -265,8 +257,16 @@ func (l *LimaVzDriver) GuestAgentConn(_ context.Context) (net.Conn, error) {
265257
return nil, errors.New("unable to connect to guest agent via vsock port 2222")
266258
}
267259

268-
func (l *LimaVzDriver) Name() string {
269-
return "vz"
260+
func (l *LimaVzDriver) GetInfo() driver.Info {
261+
var info driver.Info
262+
if l.Instance != nil && l.Instance.Config != nil {
263+
info.CanRunGUI = l.canRunGUI()
264+
}
265+
266+
info.DriverName = "vz"
267+
info.VsockPort = l.VSockPort
268+
info.VirtioPort = l.VirtioPort
269+
return info
270270
}
271271

272272
func (l *LimaVzDriver) Register(_ context.Context) error {

pkg/driver/wsl2/wsl_driver_windows.go

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

71-
func (l *LimaWslDriver) GetVirtioPort() string {
72-
return l.VirtioPort
73-
}
74-
75-
func (l *LimaWslDriver) GetVSockPort() int {
76-
return l.VSockPort
77-
}
78-
7971
func (l *LimaWslDriver) SetConfig(inst *store.Instance, sshLocalPort int) {
8072
l.Instance = inst
8173
l.SSHLocalPort = sshLocalPort
@@ -173,7 +165,7 @@ func (l *LimaWslDriver) Start(ctx context.Context) (chan error, error) {
173165

174166
// Requires WSLg, which requires specific version of WSL2 to be installed.
175167
// TODO: Add check and add support for WSLg (instead of VNC) to hostagent.
176-
func (l *LimaWslDriver) CanRunGUI() bool {
168+
func (l *LimaWslDriver) canRunGUI() bool {
177169
// return *l.InstConfig.Video.Display == "wsl"
178170
return false
179171
}
@@ -222,8 +214,13 @@ func (l *LimaWslDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
222214
return winio.Dial(ctx, sockAddr)
223215
}
224216

225-
func (l *LimaWslDriver) Name() string {
226-
return "wsl2"
217+
func (l *LimaWslDriver) GetInfo() driver.Info {
218+
return driver.Info{
219+
DriverName: "wsl",
220+
CanRunGUI: l.canRunGUI(),
221+
VsockPort: l.VSockPort,
222+
VirtioPort: l.VirtioPort,
223+
}
227224
}
228225

229226
func (l *LimaWslDriver) Initialize(_ context.Context) error {

pkg/hostagent/hostagent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
136136
return nil, fmt.Errorf("failed to create driver instance: %w", err)
137137
}
138138

139-
vSockPort := limaDriver.GetVSockPort()
140-
virtioPort := limaDriver.GetVirtioPort()
139+
vSockPort := limaDriver.GetInfo().VsockPort
140+
virtioPort := limaDriver.GetInfo().VirtioPort
141141

142142
if err := cidata.GenerateCloudConfig(inst.Dir, instName, inst.Config); err != nil {
143143
return nil, err
@@ -364,7 +364,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
364364
logrus.Infof("VNC Password: `%s`", vncpwdfile)
365365
}
366366

367-
if a.driver.CanRunGUI() {
367+
if a.driver.GetInfo().CanRunGUI {
368368
go func() {
369369
err = a.startRoutinesAndWait(ctx, errCh)
370370
if err != nil {

pkg/instance/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func Start(ctx context.Context, inst *store.Instance, limactl string, launchHost
190190
"hostagent",
191191
"--pidfile", haPIDPath,
192192
"--socket", haSockPath)
193-
if prepared.Driver.CanRunGUI() {
193+
if prepared.Driver.GetInfo().CanRunGUI {
194194
args = append(args, "--run-gui")
195195
}
196196
if prepared.GuestAgent != "" {

pkg/registry/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func init() {
170170

171171
func Register(driver driver.Driver) {
172172
if DefaultRegistry != nil {
173-
name := driver.Name()
173+
name := driver.GetInfo().DriverName
174174
if _, exists := DefaultRegistry.drivers[name]; exists {
175175
return
176176
}

0 commit comments

Comments
 (0)