Skip to content

Commit 452e94c

Browse files
committed
Move NerdctlArchiveCache function to cacheutil
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent 595b2c3 commit 452e94c

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

pkg/cacheutil/cacheutil.go

Lines changed: 50 additions & 0 deletions
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/v2/pkg/downloader"
11+
"github.com/lima-vm/lima/v2/pkg/fileutils"
12+
"github.com/lima-vm/lima/v2/pkg/limatype"
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 *limatype.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+
}

pkg/instance/start.go

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/lima-vm/go-qcow2reader"
2020
"github.com/sirupsen/logrus"
2121

22-
"github.com/lima-vm/lima/v2/pkg/downloader"
22+
"github.com/lima-vm/lima/v2/pkg/cacheutil"
2323
"github.com/lima-vm/lima/v2/pkg/driver"
2424
"github.com/lima-vm/lima/v2/pkg/driverutil"
2525
"github.com/lima-vm/lima/v2/pkg/executil"
@@ -37,41 +37,6 @@ import (
3737
// to be running before timing out.
3838
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
3939

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

157-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
122+
nerdctlArchiveCache, err := cacheutil.EnsureNerdctlArchiveCache(ctx, inst.Config, created)
158123
if err != nil {
159124
return nil, err
160125
}

0 commit comments

Comments
 (0)