Skip to content

Commit 31245bf

Browse files
committed
Update example, switch to extraArchives and reuse nerdctl download function
Signed-off-by: Justin Alvarez <[email protected]>
1 parent ef37bf1 commit 31245bf

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

examples/default.yaml

+9-4
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,17 @@ containerd:
196196
# vim was not installed in the guest. Make sure the package system is working correctly.
197197
# Also see "/var/log/cloud-init-output.log" in the guest.
198198

199-
# Adds an additional archive to the CIDATA image which is mounted on boot
200-
# Useful for provisioning scripts that run before mounts
201-
# extraArchive:
202-
# location: "~/extra.tar.gz"
199+
# Adds an additional archive which matches the system architecture to the CIDATA image which is mounted on boot.
200+
# Useful for provisioning scripts that run before mounts (like `mode: dependency`) in case they need any file resources.
201+
# See pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh for an example of how to use an archive
202+
# from a provisioning script.
203+
# extraArchives:
204+
# - location: "~/extra.amd64.tar.gz"
203205
# arch: "x86_64"
204206
# digest: "sha256:..."
207+
# - location: "~/extra.aarch64.tar.gz"
208+
# arch: "aarch64"
209+
# digest: "sha256:..."
205210

206211
# ===================================================================== #
207212
# FURTHER ADVANCED CONFIGURATION

pkg/limayaml/limayaml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type LimaYAML struct {
3131
UseHostResolver *bool `yaml:"useHostResolver,omitempty" json:"useHostResolver,omitempty"` // DEPRECATED, use `HostResolver.Enabled` instead
3232
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
3333
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
34-
ExtraArchive *File `yaml:"extraArchive,omitempty" json:"extraArchive,omitempty"`
34+
ExtraArchives []File `yaml:"extraArchives,omitempty" json:"extraArchives,omitempty"`
3535
}
3636

3737
type Arch = string

pkg/start/start.go

+27-32
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,35 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
4343
return "", nil
4444
}
4545

46-
errs := make([]error, len(y.Containerd.Archives))
47-
for i := range y.Containerd.Archives {
48-
f := &y.Containerd.Archives[i]
49-
if f.Arch != *y.Arch {
46+
location, errs := downloadAndCacheArchiveForArch(y.Containerd.Archives, *y.Arch, "nerdctl")
47+
if location == "" {
48+
return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v",
49+
len(y.Containerd.Archives), errs)
50+
}
51+
52+
return location, nil
53+
}
54+
55+
// downloadAndCacheArchiveForArch iterates through a slice of File and tries to download the provided
56+
// file which matches the supplied arch.
57+
// The downloader caches remote downloads to disk, and then returns a file path to the archive.
58+
func downloadAndCacheArchiveForArch(files []limayaml.File, arch limayaml.Arch, archiveType string) (string, []error) {
59+
errs := make([]error, len(files))
60+
for i := range files {
61+
f := &files[i]
62+
if f.Arch != arch {
5063
errs[i] = fmt.Errorf("unsupported arch: %q", f.Arch)
5164
continue
5265
}
53-
logrus.WithField("digest", f.Digest).Infof("Attempting to download the nerdctl archive from %q", f.Location)
66+
logrus.WithField("digest", f.Digest).Infof("Attempting to download the %s archive from %q", archiveType, f.Location)
5467
res, err := downloader.Download("", f.Location, downloader.WithCache(), downloader.WithExpectedDigest(f.Digest))
5568
if err != nil {
5669
errs[i] = fmt.Errorf("failed to download %q: %w", f.Location, err)
5770
continue
5871
}
5972
switch res.Status {
6073
case downloader.StatusDownloaded:
61-
logrus.Infof("Downloaded the nerdctl archive from %q", f.Location)
74+
logrus.Infof("Downloaded the %s archive from %q", archiveType, f.Location)
6275
case downloader.StatusUsedCache:
6376
logrus.Infof("Using cache %q", res.CachePath)
6477
default:
@@ -68,13 +81,11 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
6881
if downloader.IsLocal(f.Location) {
6982
return f.Location, nil
7083
}
71-
return "", fmt.Errorf("cache did not contain %q", f.Location)
84+
errs[i] = fmt.Errorf("cache did not contain %q", f.Location)
7285
}
7386
return res.CachePath, nil
7487
}
75-
76-
return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v",
77-
len(y.Containerd.Archives), errs)
88+
return "", errs
7889
}
7990

8091
func Start(ctx context.Context, inst *store.Instance) error {
@@ -132,29 +143,13 @@ func Start(ctx context.Context, inst *store.Instance) error {
132143
if nerdctlArchiveCache != "" {
133144
args = append(args, "--nerdctl-archive", nerdctlArchiveCache)
134145
}
135-
if y.ExtraArchive != nil {
136-
if y.ExtraArchive.Digest.String() != "" {
137-
if !y.ExtraArchive.Digest.Algorithm().Available() {
138-
return fmt.Errorf("expected digest algorithm %q is not available", y.ExtraArchive.Digest.Algorithm())
139-
}
140-
if err := y.ExtraArchive.Digest.Validate(); err != nil {
141-
return err
142-
}
143-
eaBuf, err := os.ReadFile(y.ExtraArchive.Location)
144-
if err != nil {
145-
return err
146-
}
147-
verifier := y.ExtraArchive.Digest.Verifier()
148-
if _, err := verifier.Write(eaBuf); err != nil {
149-
return err
150-
}
151-
if !verifier.Verified() {
152-
return errors.New("digest mismatch between supplied digest and calculated digest for extra archive")
153-
}
154-
}
155-
156-
args = append(args, "--extra-archive", y.ExtraArchive.Location)
146+
location, errs := downloadAndCacheArchiveForArch(y.ExtraArchives, *y.Arch, "extrArchive")
147+
if location == "" {
148+
return fmt.Errorf("failed to download the extraArchive archive, attempted %d candidates, errors=%v",
149+
len(y.ExtraArchives), errs)
157150
}
151+
args = append(args, "--extra-archive", location)
152+
158153
args = append(args, inst.Name)
159154
haCmd := exec.CommandContext(ctx, self, args...)
160155

0 commit comments

Comments
 (0)