Skip to content

Commit eebd5f8

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

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

cmd/limactl/validate.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

33
import (
4+
"bufio"
45
"fmt"
6+
"io"
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,59 @@ 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+
return cidata.ValidateCloudConfig(b)
53+
}
54+
55+
func validateLimayaml(f string) error {
56+
_, err := store.LoadYAMLByFilePath(f)
57+
if err != nil {
58+
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
59+
}
60+
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
61+
return err
62+
}
63+
return nil
64+
}
65+
2466
func validateAction(_ *cobra.Command, args []string) error {
2567
for _, f := range args {
26-
_, err := store.LoadYAMLByFilePath(f)
68+
line, err := firstLine(f)
2769
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 {
3170
return err
3271
}
72+
if line == "#cloud-config" {
73+
if err := validateCloudConfig(f); err != nil {
74+
return err
75+
}
76+
} else {
77+
if err := validateLimayaml(f); err != nil {
78+
return err
79+
}
80+
}
3381
logrus.Infof("%q: OK", f)
3482
}
3583

pkg/cidata/schema.go

Lines changed: 1 addition & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 5 deletions
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)