Skip to content

Commit a6ca564

Browse files
committed
Move NerdctlArchiveCache function to cacheutil
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 6a2fd5a commit a6ca564

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

Diff for: pkg/cacheutil/cacheutil.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cacheutil
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/lima-vm/lima/pkg/downloader"
11+
"github.com/lima-vm/lima/pkg/fileutils"
12+
"github.com/lima-vm/lima/pkg/limayaml"
13+
)
14+
15+
// EnsureNerdctlArchiveCache prefetches the archive into the cache.
16+
//
17+
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
18+
// into the cache before launching the hostagent process, so that we can show the progress in tty.
19+
// https://github.com/lima-vm/lima/issues/326
20+
func EnsureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, created bool) (string, error) {
21+
if !*y.Containerd.System && !*y.Containerd.User {
22+
// nerdctl archive is not needed
23+
return "", nil
24+
}
25+
26+
errs := make([]error, len(y.Containerd.Archives))
27+
for i, f := range y.Containerd.Archives {
28+
// Skip downloading again if the file is already in the cache
29+
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
30+
path, err := fileutils.CachedFile(f)
31+
if err == nil {
32+
return path, nil
33+
}
34+
}
35+
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
36+
if err != nil {
37+
errs[i] = err
38+
continue
39+
}
40+
if path == "" {
41+
if downloader.IsLocal(f.Location) {
42+
return f.Location, nil
43+
}
44+
return "", fmt.Errorf("cache did not contain %q", f.Location)
45+
}
46+
return path, nil
47+
}
48+
49+
return "", fileutils.Errors(errs)
50+
}

Diff for: pkg/instance/start.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/coreos/go-semver/semver"
21+
"github.com/lima-vm/lima/pkg/cacheutil"
2122
"github.com/lima-vm/lima/pkg/driver"
2223
"github.com/lima-vm/lima/pkg/driverutil"
2324
"github.com/lima-vm/lima/pkg/executil"
@@ -26,8 +27,6 @@ import (
2627
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
2728
"github.com/mattn/go-isatty"
2829

29-
"github.com/lima-vm/lima/pkg/downloader"
30-
"github.com/lima-vm/lima/pkg/fileutils"
3130
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
3231
"github.com/lima-vm/lima/pkg/limayaml"
3332
"github.com/lima-vm/lima/pkg/store"
@@ -39,41 +38,6 @@ import (
3938
// to be running before timing out.
4039
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
4140

42-
// ensureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
43-
// into the cache before launching the hostagent process, so that we can show the progress in tty.
44-
// https://github.com/lima-vm/lima/issues/326
45-
func ensureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, created bool) (string, error) {
46-
if !*y.Containerd.System && !*y.Containerd.User {
47-
// nerdctl archive is not needed
48-
return "", nil
49-
}
50-
51-
errs := make([]error, len(y.Containerd.Archives))
52-
for i, f := range y.Containerd.Archives {
53-
// Skip downloading again if the file is already in the cache
54-
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
55-
path, err := fileutils.CachedFile(f)
56-
if err == nil {
57-
return path, nil
58-
}
59-
}
60-
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
61-
if err != nil {
62-
errs[i] = err
63-
continue
64-
}
65-
if path == "" {
66-
if downloader.IsLocal(f.Location) {
67-
return f.Location, nil
68-
}
69-
return "", fmt.Errorf("cache did not contain %q", f.Location)
70-
}
71-
return path, nil
72-
}
73-
74-
return "", fileutils.Errors(errs)
75-
}
76-
7741
type Prepared struct {
7842
Driver driver.Driver
7943
NerdctlArchiveCache string
@@ -101,7 +65,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
10165
if err := limaDriver.CreateDisk(ctx); err != nil {
10266
return nil, err
10367
}
104-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
68+
nerdctlArchiveCache, err := cacheutil.EnsureNerdctlArchiveCache(ctx, inst.Config, created)
10569
if err != nil {
10670
return nil, err
10771
}

0 commit comments

Comments
 (0)