Skip to content

Commit 9bb96ef

Browse files
authored
Merge pull request #1216 from balajiv113/validation
support for validating unknown fields
2 parents fc3f2a9 + e421cb2 commit 9bb96ef

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

pkg/limayaml/defaults.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,28 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
121121
}
122122
}
123123
}
124+
var overrideCPUType bool
124125
for k, v := range d.CPUType {
125126
if len(v) > 0 {
127+
overrideCPUType = true
126128
cpuType[k] = v
127129
}
128130
}
129131
for k, v := range y.CPUType {
130132
if len(v) > 0 {
133+
overrideCPUType = true
131134
cpuType[k] = v
132135
}
133136
}
134137
for k, v := range o.CPUType {
135138
if len(v) > 0 {
139+
overrideCPUType = true
136140
cpuType[k] = v
137141
}
138142
}
139-
y.CPUType = cpuType
143+
if *y.VMType == QEMU || overrideCPUType {
144+
y.CPUType = cpuType
145+
}
140146

141147
if y.CPUs == nil {
142148
y.CPUs = d.CPUs
@@ -176,7 +182,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
176182
if o.Video.Display != nil {
177183
y.Video.Display = o.Video.Display
178184
}
179-
if y.Video.Display == nil || *y.Video.Display == "" {
185+
if (y.Video.Display == nil || *y.Video.Display == "") && *y.VMType == QEMU {
180186
y.Video.Display = pointer.String("none")
181187
}
182188

pkg/reflectutil/reflectutil.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This file has been adapted from https://github.com/containerd/nerdctl/blob/v1.0.0/pkg/reflectutil/reflectutil.go
2+
/*
3+
Copyright The containerd Authors.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package reflectutil
16+
17+
import (
18+
"fmt"
19+
"reflect"
20+
)
21+
22+
func UnknownNonEmptyFields(structOrStructPtr interface{}, knownNames ...string) []string {
23+
var unknown []string
24+
knownNamesMap := make(map[string]struct{}, len(knownNames))
25+
for _, name := range knownNames {
26+
knownNamesMap[name] = struct{}{}
27+
}
28+
origVal := reflect.ValueOf(structOrStructPtr)
29+
var val reflect.Value
30+
switch kind := origVal.Kind(); kind {
31+
case reflect.Ptr:
32+
val = origVal.Elem()
33+
case reflect.Struct:
34+
val = origVal
35+
default:
36+
panic(fmt.Errorf("expected Ptr or Struct, got %+v", kind))
37+
}
38+
for i := 0; i < val.NumField(); i++ {
39+
iField := val.Field(i)
40+
if isEmpty(iField) {
41+
continue
42+
}
43+
iName := val.Type().Field(i).Name
44+
if _, ok := knownNamesMap[iName]; !ok {
45+
unknown = append(unknown, iName)
46+
}
47+
}
48+
return unknown
49+
}
50+
51+
func isEmpty(v reflect.Value) bool {
52+
// NOTE: IsZero returns false for zero-length map and slice
53+
if v.IsZero() {
54+
return true
55+
}
56+
switch v.Kind() {
57+
case reflect.Map, reflect.Slice:
58+
return v.Len() == 0
59+
}
60+
return false
61+
}

pkg/vz/vz_driver_darwin.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"fmt"
1010
"path/filepath"
1111

12+
"github.com/lima-vm/lima/pkg/reflectutil"
13+
1214
"github.com/Code-Hex/vz/v3"
1315

1416
"github.com/lima-vm/lima/pkg/store/filenames"
@@ -37,6 +39,57 @@ func (l *LimaVzDriver) Validate() error {
3739
if *l.Yaml.Firmware.LegacyBIOS {
3840
return fmt.Errorf("`firmware.legacyBIOS` configuration is not supported for VZ driver")
3941
}
42+
if unknown := reflectutil.UnknownNonEmptyFields(l.Yaml, "VMType",
43+
"Arch",
44+
"Images",
45+
"CPUs",
46+
"Memory",
47+
"Disk",
48+
"Mounts",
49+
"MountType",
50+
"SSH",
51+
"Firmware",
52+
"Provision",
53+
"Containerd",
54+
"Probes",
55+
"PortForwards",
56+
"Message",
57+
"Networks",
58+
"Env",
59+
"DNS",
60+
"HostResolver",
61+
"PropagateProxyEnv",
62+
"CACertificates",
63+
"Rosetta",
64+
); len(unknown) > 0 {
65+
logrus.Warnf("Ignoring: vmType %s: %+v", *l.Yaml.VMType, unknown)
66+
}
67+
68+
for i, image := range l.Yaml.Images {
69+
if unknown := reflectutil.UnknownNonEmptyFields(image, "File"); len(unknown) > 0 {
70+
logrus.Warnf("Ignoring: vmType %s: images[%d]: %+v", *l.Yaml.VMType, i, unknown)
71+
}
72+
}
73+
74+
for i, mount := range l.Yaml.Mounts {
75+
if unknown := reflectutil.UnknownNonEmptyFields(mount, "Location",
76+
"MountPoint",
77+
"Writable",
78+
"SSHFS",
79+
"NineP",
80+
); len(unknown) > 0 {
81+
logrus.Warnf("Ignoring: vmType %s: mounts[%d]: %+v", *l.Yaml.VMType, i, unknown)
82+
}
83+
}
84+
85+
for i, network := range l.Yaml.Networks {
86+
if unknown := reflectutil.UnknownNonEmptyFields(network, "VZNAT",
87+
"MACAddress",
88+
"Interface",
89+
); len(unknown) > 0 {
90+
logrus.Warnf("Ignoring: vmType %s: networks[%d]: %+v", *l.Yaml.VMType, i, unknown)
91+
}
92+
}
4093
return nil
4194
}
4295

0 commit comments

Comments
 (0)