Skip to content

Commit 2adaf1d

Browse files
authored
Merge pull request #3453 from jandubois/image-templates
Refactor images lists and default mounts into base templates
2 parents 95173c1 + 238a141 commit 2adaf1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+865
-893
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ jobs:
196196
- name: Validate jsonschema
197197
run: make schema-limayaml.json
198198
- name: Validate templates
199-
run: find -L templates -name '*.yaml' | xargs limactl validate
199+
# Can't validate base templates in `_default` because they have no images
200+
run: find -L templates -name '*.yaml' ! -path '*/_default/*' | xargs limactl validate
200201
- name: Install test dependencies
201202
# QEMU: required by Lima itself
202203
# bash: required by test-templates.sh (OS version of bash is too old)
@@ -208,7 +209,7 @@ jobs:
208209
run: echo "LIMACTL_CREATE_ARGS=${LIMACTL_CREATE_ARGS} --vm-type=qemu" >>$GITHUB_ENV
209210
- name: "Inject `no_timer_check` to kernel cmdline"
210211
# workaround to https://github.com/lima-vm/lima/issues/84
211-
run: ./hack/inject-cmdline-to-template.sh templates/default.yaml no_timer_check
212+
run: ./hack/inject-cmdline-to-template.sh templates/_images/ubuntu.yaml no_timer_check
212213
- name: Cache image used by default.yaml
213214
uses: ./.github/actions/setup_cache_for_template
214215
with:
@@ -353,7 +354,7 @@ jobs:
353354
steps:
354355
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
355356
with:
356-
fetch-depth: 1
357+
fetch-depth: 0
357358
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0
358359
with:
359360
go-version: 1.24.x
@@ -365,7 +366,7 @@ jobs:
365366
run: echo "LIMACTL_CREATE_ARGS=${LIMACTL_CREATE_ARGS} --vm-type=qemu --network=lima:shared" >>$GITHUB_ENV
366367
- name: "Inject `no_timer_check` to kernel cmdline"
367368
# workaround to https://github.com/lima-vm/lima/issues/84
368-
run: ./hack/inject-cmdline-to-template.sh templates/default.yaml no_timer_check
369+
run: ./hack/inject-cmdline-to-template.sh templates/_images/ubuntu.yaml no_timer_check
369370
- name: Cache image used by default .yaml
370371
uses: ./.github/actions/setup_cache_for_template
371372
with:
@@ -408,6 +409,7 @@ jobs:
408409
steps:
409410
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
410411
with:
412+
# To avoid "failed to load YAML file \"templates/experimental/riscv64.yaml\": can't parse builtin Lima version \"3f3a6f6\": 3f3a6f6 is not in dotted-tri format"
411413
fetch-depth: 0
412414
- name: Fetch homebrew-core commit messages
413415
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -452,7 +454,7 @@ jobs:
452454
steps:
453455
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
454456
with:
455-
fetch-depth: 1
457+
fetch-depth: 0
456458
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0
457459
with:
458460
go-version: 1.24.x
@@ -487,7 +489,7 @@ jobs:
487489
steps:
488490
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
489491
with:
490-
fetch-depth: 1
492+
fetch-depth: 0
491493
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0
492494
with:
493495
go-version: 1.24.x

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
_output/
22
_artifacts/
33
lima.REJECTED.yaml
4+
default-template.yaml
45
schema-limayaml.json
56
.config

.ls-lint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ ls:
1919
docs:
2020
.md: kebab-case
2121

22+
templates:
23+
# _default and _images have leading underscores
24+
.dir: lowercase
25+
2226
website/content:
2327
.dir: lowercase
2428

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,19 @@ ifeq ($(native_compiling),true)
419419
endif
420420

421421
################################################################################
422-
schema-limayaml.json: _output/bin/limactl$(exe)
422+
default-template.yaml: _output/bin/limactl$(exe)
423423
ifeq ($(native_compiling),true)
424-
$< generate-jsonschema --schemafile $@ templates/default.yaml
424+
$< tmpl copy --embed-all templates/default.yaml $@
425+
endif
426+
427+
schema-limayaml.json: _output/bin/limactl$(exe) default-template.yaml
428+
ifeq ($(native_compiling),true)
429+
$< generate-jsonschema --schemafile $@ default-template.yaml
425430
endif
426431

427432
.PHONY: check-jsonschema
428-
check-jsonschema: schema-limayaml.json
429-
check-jsonschema --schemafile $< templates/default.yaml
433+
check-jsonschema: schema-limayaml.json default-template.yaml
434+
check-jsonschema --schemafile schema-limayaml.json default-template.yaml
430435

431436
################################################################################
432437
.PHONY: diagrams

cmd/limactl/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ func createStartActionCommon(cmd *cobra.Command, _ []string) (exit bool, err err
374374
if templates, err := templatestore.Templates(); err == nil {
375375
w := cmd.OutOrStdout()
376376
for _, f := range templates {
377-
_, _ = fmt.Fprintln(w, f.Name)
377+
// Don't show internal base templates like `_default/*` and `_images/*`.
378+
if !strings.HasPrefix(f.Name, "_") {
379+
_, _ = fmt.Fprintln(w, f.Name)
380+
}
378381
}
379382
return true, nil
380383
}

hack/common.inc.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ _IPERF3=iperf3
2929
# https://github.com/lima-vm/socket_vmnet/issues/85
3030
[ "$(uname -s)" = "Darwin" ] && _IPERF3="iperf3-darwin"
3131
: "${IPERF3:=$_IPERF3}"
32+
33+
# Setup LIMA_TEMPLATES_PATH because the templates are not installed, but reference base templates
34+
# via template://_images/* and template://_default/*.
35+
templates_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../templates" && pwd)"
36+
: "${LIMA_TEMPLATES_PATH:-$templates_dir}"
37+
export LIMA_TEMPLATES_PATH

hack/test-templates.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ FILE="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
2222
NAME="$(basename -s .yaml "$FILE")"
2323
OS_HOST="$(uname -o)"
2424

25-
# On Windows $HOME of the bash runner, %USERPROFILE% of the host machine and mpunting point in the guest machine
26-
# are all different folders. This will handle path differences, when values are expilictly set.
25+
# On Windows $HOME of the bash runner, %USERPROFILE% of the host machine and mounting point in the guest machine
26+
# are all different folders. This will handle path differences, when values are explicitly set.
2727
HOME_HOST=${HOME_HOST:-$HOME}
2828
HOME_GUEST=${HOME_GUEST:-$HOME}
2929
FILE_HOST=$FILE

pkg/limayaml/limayaml_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func TestDefaultYAML(t *testing.T) {
3737
assert.NilError(t, err)
3838
y.Images = nil // remove default images
3939
y.Mounts = nil // remove default mounts
40+
y.Base = nil // remove default base templates
41+
y.MinimumLimaVersion = nil // remove minimum Lima version
4042
y.MountTypesUnsupported = nil // remove default workaround for kernel 6.9-6.11
4143
t.Log(dumpJSON(t, y))
4244
b, err := Marshal(&y, false)

pkg/limayaml/validate_test.go

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

66
import (
7-
"os"
8-
"runtime"
97
"testing"
108

119
"gotest.tools/v3/assert"
@@ -18,22 +16,6 @@ func TestValidateEmpty(t *testing.T) {
1816
assert.Error(t, err, "field `images` must be set")
1917
}
2018

21-
// Note: can't embed symbolic links, use "os"
22-
23-
func TestValidateDefault(t *testing.T) {
24-
if runtime.GOOS == "windows" {
25-
// FIXME: `assertion failed: error is not nil: field `mounts[1].location` must be an absolute path, got "/tmp/lima"`
26-
t.Skip("Skipping on windows")
27-
}
28-
29-
bytes, err := os.ReadFile("default.yaml")
30-
assert.NilError(t, err)
31-
y, err := Load(bytes, "default.yaml")
32-
assert.NilError(t, err)
33-
err = Validate(y, true)
34-
assert.NilError(t, err)
35-
}
36-
3719
func TestValidateProbes(t *testing.T) {
3820
images := `images: [{"location": "/"}]`
3921
validProbe := `probes: [{"script": "#!foo"}]`

pkg/templatestore/templatestore.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package templatestore
55

66
import (
7+
"errors"
8+
"fmt"
79
"io/fs"
810
"os"
911
"path/filepath"
@@ -20,21 +22,32 @@ type Template struct {
2022
}
2123

2224
func Read(name string) ([]byte, error) {
23-
dir, err := usrlocalsharelima.Dir()
24-
if err != nil {
25-
return nil, err
25+
var pathList []string
26+
if tmplPath := os.Getenv("LIMA_TEMPLATES_PATH"); tmplPath != "" {
27+
pathList = strings.Split(tmplPath, string(filepath.ListSeparator))
28+
} else {
29+
dir, err := usrlocalsharelima.Dir()
30+
if err != nil {
31+
return nil, err
32+
}
33+
pathList = []string{filepath.Join(dir, "templates")}
2634
}
2735
ext := filepath.Ext(name)
2836
// Append .yaml extension if name doesn't have an extension, or if it starts with a digit.
2937
// So "docker.sh" would remain unchanged but "ubuntu-24.04" becomes "ubuntu-24.04.yaml".
3038
if len(ext) < 2 || unicode.IsDigit(rune(ext[1])) {
3139
name += ".yaml"
3240
}
33-
filePath, err := securejoin.SecureJoin(filepath.Join(dir, "templates"), name)
34-
if err != nil {
35-
return nil, err
41+
for _, path := range pathList {
42+
filePath, err := securejoin.SecureJoin(path, name)
43+
if err != nil {
44+
return nil, err
45+
}
46+
if b, err := os.ReadFile(filePath); !errors.Is(err, os.ErrNotExist) {
47+
return b, err
48+
}
3649
}
37-
return os.ReadFile(filePath)
50+
return nil, fmt.Errorf("template %q not found", name)
3851
}
3952

4053
const Default = "default"

0 commit comments

Comments
 (0)