Skip to content

Commit aa35fe4

Browse files
committed
Run cloud-config schema validation on-demand
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 48f6a4a commit aa35fe4

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

cmd/limactl/validate.go

+55-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

33
import (
4+
"bufio"
5+
"io"
46
"fmt"
7+
"os"
58

69
"github.com/lima-vm/lima/cmd/limactl/guessarg"
10+
"github.com/lima-vm/lima/pkg/cidata"
711
"github.com/lima-vm/lima/pkg/store"
812
"github.com/spf13/cobra"
913

@@ -21,15 +25,62 @@ func newValidateCommand() *cobra.Command {
2125
return validateCommand
2226
}
2327

28+
func firstLine(f string) (string, error) {
29+
file, err := os.Open(f)
30+
if err != nil {
31+
return "", err
32+
}
33+
defer file.Close()
34+
scanner := bufio.NewScanner(file)
35+
scanner.Scan()
36+
if err := scanner.Err(); err != nil {
37+
return "", err
38+
}
39+
return scanner.Text(), nil
40+
}
41+
42+
func validateCloudConfig(f string) error {
43+
file, err := os.Open(f)
44+
if err != nil {
45+
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
46+
}
47+
defer file.Close()
48+
b, err := io.ReadAll(file)
49+
if err != nil {
50+
return err
51+
}
52+
if err = cidata.ValidateCloudConfig(b); err != nil {
53+
return err
54+
}
55+
return nil
56+
}
57+
58+
func validateLimayaml(f string) error {
59+
_, err := store.LoadYAMLByFilePath(f)
60+
if err != nil {
61+
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
62+
}
63+
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
64+
return err
65+
}
66+
return nil
67+
}
68+
2469
func validateAction(_ *cobra.Command, args []string) error {
2570
for _, f := range args {
26-
_, err := store.LoadYAMLByFilePath(f)
71+
line, err := firstLine(f)
2772
if err != nil {
28-
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
29-
}
30-
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
3173
return err
3274
}
75+
if line == "#cloud-config" {
76+
if err := validateCloudConfig(f); err != nil {
77+
return err
78+
}
79+
} else {
80+
if err := validateLimayaml(f); err != nil {
81+
return err
82+
}
83+
}
3384
logrus.Infof("%q: OK", f)
3485
}
3586

pkg/cidata/schema.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package cidata
22

33
import (
4-
"bytes"
54
_ "embed"
6-
"fmt"
75
"strings"
86

97
"github.com/santhosh-tekuri/jsonschema/v5"
@@ -16,10 +14,7 @@ const schemaURL = "https://raw.githubusercontent.com/canonical/cloud-init/main/c
1614
//go:embed schemas/schema-cloud-config-v1.json
1715
var schemaText string
1816

19-
func validateCloudConfig(userData []byte) error {
20-
if !bytes.HasPrefix(userData, []byte("#cloud-config")) {
21-
return fmt.Errorf("missing #cloud-config")
22-
}
17+
func ValidateCloudConfig(userData []byte) error {
2318
var m interface{}
2419
err := yaml.Unmarshal(userData, &m)
2520
if err != nil {

pkg/cidata/schema_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ func TestValidate(t *testing.T) {
1111
users:
1212
- default
1313
`
14-
err := validateCloudConfig([]byte(config))
14+
err := ValidateCloudConfig([]byte(config))
1515
assert.NilError(t, err)
1616
}

pkg/cidata/template.go

-5
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ func ExecuteTemplate(args TemplateArgs) ([]iso9660util.Entry, error) {
147147
if err != nil {
148148
return err
149149
}
150-
if d.Name() == "user-data" {
151-
if err := validateCloudConfig(b); err != nil {
152-
return err
153-
}
154-
}
155150
layout = append(layout, iso9660util.Entry{
156151
Path: path,
157152
Reader: bytes.NewReader(b),

0 commit comments

Comments
 (0)