Skip to content

Commit

Permalink
Run cloud-config schema validation on-demand
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed May 2, 2024
1 parent 48f6a4a commit aa35fe4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
59 changes: 55 additions & 4 deletions cmd/limactl/validate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"bufio"

Check failure on line 4 in cmd/limactl/validate.go

View workflow job for this annotation

GitHub Actions / Lints

File is not `gofumpt`-ed (gofumpt)
"io"

Check failure on line 5 in cmd/limactl/validate.go

View workflow job for this annotation

GitHub Actions / Lints

File is not `gofmt`-ed with `-s` (gofmt)
"fmt"
"os"

"github.com/lima-vm/lima/cmd/limactl/guessarg"
"github.com/lima-vm/lima/pkg/cidata"
"github.com/lima-vm/lima/pkg/store"
"github.com/spf13/cobra"

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

func firstLine(f string) (string, error) {
file, err := os.Open(f)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
if err := scanner.Err(); err != nil {
return "", err
}
return scanner.Text(), nil
}

func validateCloudConfig(f string) error {
file, err := os.Open(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
defer file.Close()
b, err := io.ReadAll(file)
if err != nil {
return err
}
if err = cidata.ValidateCloudConfig(b); err != nil {
return err
}
return nil
}

func validateLimayaml(f string) error {
_, err := store.LoadYAMLByFilePath(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
return err
}
return nil
}

func validateAction(_ *cobra.Command, args []string) error {
for _, f := range args {
_, err := store.LoadYAMLByFilePath(f)
line, err := firstLine(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
return err
}
if line == "#cloud-config" {
if err := validateCloudConfig(f); err != nil {
return err
}
} else {
if err := validateLimayaml(f); err != nil {
return err
}
}
logrus.Infof("%q: OK", f)
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/cidata/schema.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cidata

import (
"bytes"
_ "embed"
"fmt"
"strings"

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

func validateCloudConfig(userData []byte) error {
if !bytes.HasPrefix(userData, []byte("#cloud-config")) {
return fmt.Errorf("missing #cloud-config")
}
func ValidateCloudConfig(userData []byte) error {
var m interface{}
err := yaml.Unmarshal(userData, &m)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cidata/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func TestValidate(t *testing.T) {
users:
- default
`
err := validateCloudConfig([]byte(config))
err := ValidateCloudConfig([]byte(config))
assert.NilError(t, err)
}
5 changes: 0 additions & 5 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ func ExecuteTemplate(args TemplateArgs) ([]iso9660util.Entry, error) {
if err != nil {
return err
}
if d.Name() == "user-data" {
if err := validateCloudConfig(b); err != nil {
return err
}
}
layout = append(layout, iso9660util.Entry{
Path: path,
Reader: bytes.NewReader(b),
Expand Down

0 comments on commit aa35fe4

Please sign in to comment.