Skip to content

Commit 6c0e64a

Browse files
committed
revamp vm-type resolution
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 7b6b1e6 commit 6c0e64a

File tree

4 files changed

+76
-63
lines changed

4 files changed

+76
-63
lines changed

pkg/driver/wsl2/wsl_driver_windows.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (l *LimaWslDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) erro
104104
}
105105

106106
func (l *LimaWslDriver) Validate() error {
107-
if *l.Instance.Config.MountType != limatype.WSLMount {
107+
if l.Instance.Config.MountType != nil && *l.Instance.Config.MountType != limatype.WSLMount {
108108
return fmt.Errorf("field `mountType` must be %q for WSL2 driver, got %q", limatype.WSLMount, *l.Instance.Config.MountType)
109109
}
110110
// TODO: revise this list for WSL2
@@ -116,33 +116,43 @@ func (l *LimaWslDriver) Validate() error {
116116
return fmt.Errorf("unsupported arch: %q", *l.Instance.Config.Arch)
117117
}
118118

119-
// TODO: real filetype checks
120-
tarFileRegex := regexp.MustCompile(`.*tar\.*`)
121-
for i, image := range l.Instance.Config.Images {
122-
if unknown := reflectutil.UnknownNonEmptyFields(image, "File"); len(unknown) > 0 {
123-
logrus.Warnf("Ignoring: vmType %s: images[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
119+
if l.Instance.Config.VMType != nil {
120+
if l.Instance.Config.Images != nil && l.Instance.Config.Arch != nil {
121+
// TODO: real filetype checks
122+
tarFileRegex := regexp.MustCompile(`.*tar\.*`)
123+
for i, image := range l.Instance.Config.Images {
124+
if unknown := reflectutil.UnknownNonEmptyFields(image, "File"); len(unknown) > 0 {
125+
logrus.Warnf("Ignoring: vmType %s: images[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
126+
}
127+
match := tarFileRegex.MatchString(image.Location)
128+
if image.Arch == *l.Instance.Config.Arch && !match {
129+
return fmt.Errorf("unsupported image type for vmType: %s, tarball root file system required: %q", *l.Instance.Config.VMType, image.Location)
130+
}
131+
}
124132
}
125-
match := tarFileRegex.MatchString(image.Location)
126-
if image.Arch == *l.Instance.Config.Arch && !match {
127-
return fmt.Errorf("unsupported image type for vmType: %s, tarball root file system required: %q", *l.Instance.Config.VMType, image.Location)
128-
}
129-
}
130133

131-
for i, mount := range l.Instance.Config.Mounts {
132-
if unknown := reflectutil.UnknownNonEmptyFields(mount); len(unknown) > 0 {
133-
logrus.Warnf("Ignoring: vmType %s: mounts[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
134+
if l.Instance.Config.Mounts != nil {
135+
for i, mount := range l.Instance.Config.Mounts {
136+
if unknown := reflectutil.UnknownNonEmptyFields(mount); len(unknown) > 0 {
137+
logrus.Warnf("Ignoring: vmType %s: mounts[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
138+
}
139+
}
134140
}
135-
}
136141

137-
for i, network := range l.Instance.Config.Networks {
138-
if unknown := reflectutil.UnknownNonEmptyFields(network); len(unknown) > 0 {
139-
logrus.Warnf("Ignoring: vmType %s: networks[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
142+
if l.Instance.Config.Networks != nil {
143+
for i, network := range l.Instance.Config.Networks {
144+
if unknown := reflectutil.UnknownNonEmptyFields(network); len(unknown) > 0 {
145+
logrus.Warnf("Ignoring: vmType %s: networks[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
146+
}
147+
}
140148
}
141-
}
142149

143-
audioDevice := *l.Instance.Config.Audio.Device
144-
if audioDevice != "" {
145-
logrus.Warnf("Ignoring: vmType %s: `audio.device`: %+v", *l.Instance.Config.VMType, audioDevice)
150+
if l.Instance.Config.Audio.Device != nil {
151+
audioDevice := *l.Instance.Config.Audio.Device
152+
if audioDevice != "" {
153+
logrus.Warnf("Ignoring: vmType %s: `audio.device`: %+v", *l.Instance.Config.VMType, audioDevice)
154+
}
155+
}
146156
}
147157

148158
return nil

pkg/driverutil/vm.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package driverutil
22

33
import (
44
"fmt"
5-
"sort"
65

76
"github.com/lima-vm/lima/v2/pkg/limatype"
87
"github.com/lima-vm/lima/v2/pkg/registry"
@@ -11,46 +10,37 @@ import (
1110

1211
func ResolveVMType(y *limatype.LimaYAML, filePath string) error {
1312
if y.VMType != nil && *y.VMType != "" {
14-
vmType := *y.VMType
15-
_, intDriver, exists := registry.Get(vmType)
16-
if !exists {
17-
return fmt.Errorf("specified vmType %q is not a registered driver", vmType)
13+
if err := validateConfigAgainstDriver(y, filePath, *y.VMType); err != nil {
14+
return err
1815
}
19-
if intDriver == nil {
20-
// For now we only support internal drivers.
21-
return fmt.Errorf("specified vmType %q is not an internal driver", vmType)
22-
}
23-
if err := intDriver.AcceptConfig(y, filePath); err != nil {
24-
return fmt.Errorf("vmType %q is not compatible with the configuration: %w", vmType, err)
25-
}
26-
if err := intDriver.FillConfig(y, filePath); err != nil {
27-
return fmt.Errorf("unable to fill config for vmType %q: %w", vmType, err)
28-
}
29-
logrus.Debugf("ResolveVMType: using explicitly specified VMType %q", vmType)
16+
logrus.Debugf("Using specified vmType %q for %q", *y.VMType, filePath)
3017
return nil
3118
}
3219

33-
// If VMType is not specified, we try to resolve it by checking config with all the registered drivers.
34-
candidates := registry.List()
35-
vmtypes := make([]string, 0, len(candidates))
36-
for vmtype := range candidates {
37-
vmtypes = append(vmtypes, vmtype)
20+
// If VMType is not specified, we go with the default platform driver.
21+
vmType := limatype.DefaultDriver()
22+
if err := validateConfigAgainstDriver(y, filePath, vmType); err == nil {
23+
return nil
24+
} else {
25+
return err
3826
}
39-
sort.Strings(vmtypes)
27+
}
4028

41-
for _, vmType := range vmtypes {
42-
// For now we only support internal drivers.
43-
if registry.CheckInternalOrExternal(vmType) == registry.Internal {
44-
_, intDriver, _ := registry.Get(vmType)
45-
if err := intDriver.AcceptConfig(y, filePath); err == nil {
46-
logrus.Debugf("ResolveVMType: resolved VMType %q", vmType)
47-
if err := intDriver.FillConfig(y, filePath); err != nil {
48-
return fmt.Errorf("unable to fill config for VMType %q: %w", vmType, err)
49-
}
50-
return nil
51-
}
52-
}
29+
func validateConfigAgainstDriver(y *limatype.LimaYAML, filePath, vmType string) error {
30+
_, intDriver, exists := registry.Get(vmType)
31+
if !exists {
32+
return fmt.Errorf("vmType %q is not a registered driver", vmType)
33+
}
34+
// For now we only support internal drivers.
35+
if intDriver == nil {
36+
return fmt.Errorf("vmType %q is not an internal driver", vmType)
37+
}
38+
if err := intDriver.AcceptConfig(y, filePath); err != nil {
39+
return err
40+
}
41+
if err := intDriver.FillConfig(y, filePath); err != nil {
42+
return err
5343
}
5444

55-
return fmt.Errorf("no VMType found for %q", filePath)
45+
return nil
5646
}

pkg/limatype/limayaml.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,14 @@ func NewVMType(driver string) VMType {
387387
return driver
388388
}
389389
}
390+
391+
func DefaultDriver() VMType {
392+
switch runtime.GOOS {
393+
case "darwin":
394+
return VZ
395+
case "windows":
396+
return WSL2
397+
default:
398+
return QEMU
399+
}
400+
}

pkg/limayaml/validate_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package limayaml
55

66
import (
7+
"fmt"
78
"testing"
89

910
"gotest.tools/v3/assert"
1011

12+
"github.com/lima-vm/lima/v2/pkg/limatype"
1113
"github.com/lima-vm/lima/v2/pkg/version"
1214
)
1315

@@ -346,37 +348,37 @@ func TestValidateAgainstLatestConfig(t *testing.T) {
346348
name: "Valid disk size unchanged",
347349
yNew: `disk: 100GiB`,
348350
yLatest: `disk: 100GiB`,
349-
wantErr: "failed to accept config for \"\": no VMType found for \"\"",
351+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver", limatype.DefaultDriver()),
350352
},
351353
{
352354
name: "Valid disk size increased",
353355
yNew: `disk: 200GiB`,
354356
yLatest: `disk: 100GiB`,
355-
wantErr: "failed to accept config for \"\": no VMType found for \"\"",
357+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver", limatype.DefaultDriver()),
356358
},
357359
{
358360
name: "No disk field in both YAMLs",
359361
yNew: ``,
360362
yLatest: ``,
361-
wantErr: "failed to accept config for \"\": no VMType found for \"\"",
363+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver", limatype.DefaultDriver()),
362364
},
363365
{
364366
name: "No disk field in new YAMLs",
365367
yNew: ``,
366368
yLatest: `disk: 100GiB`,
367-
wantErr: "failed to accept config for \"\": no VMType found for \"\"",
369+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver", limatype.DefaultDriver()),
368370
},
369371
{
370372
name: "No disk field in latest YAMLs",
371373
yNew: `disk: 100GiB`,
372374
yLatest: ``,
373-
wantErr: "failed to accept config for \"\": no VMType found for \"\"",
375+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver", limatype.DefaultDriver()),
374376
},
375377
{
376378
name: "Disk size shrunk",
377379
yNew: `disk: 50GiB`,
378380
yLatest: `disk: 100GiB`,
379-
wantErr: "failed to accept config for \"\": no VMType found for \"\"\n" +
381+
wantErr: fmt.Sprintf("failed to accept config for \"\": vmType %q is not a registered driver\n", limatype.DefaultDriver()) +
380382
"field `disk`: shrinking the disk (100GiB --> 50GiB) is not supported",
381383
},
382384
}

0 commit comments

Comments
 (0)