From cbd691d7accff590267ab08fcc9eed3aba32c4a5 Mon Sep 17 00:00:00 2001 From: Justin Alvarez Date: Thu, 6 Oct 2022 17:06:51 -0400 Subject: [PATCH] add "additionalArchives" config option Signed-off-by: Justin Alvarez --- cmd/limactl/hostagent.go | 8 +++++++ examples/default.yaml | 13 ++++++++++ pkg/cidata/cidata.go | 15 +++++++++++- pkg/hostagent/hostagent.go | 12 ++++++++-- pkg/limayaml/limayaml.go | 49 +++++++++++++++++++------------------- pkg/start/start.go | 40 +++++++++++++++++++++++-------- 6 files changed, 100 insertions(+), 37 deletions(-) diff --git a/cmd/limactl/hostagent.go b/cmd/limactl/hostagent.go index 1c7173dbbd9..c67253235e9 100644 --- a/cmd/limactl/hostagent.go +++ b/cmd/limactl/hostagent.go @@ -28,6 +28,7 @@ func newHostagentCommand() *cobra.Command { hostagentCommand.Flags().StringP("pidfile", "p", "", "write pid to file") hostagentCommand.Flags().String("socket", "", "hostagent socket") hostagentCommand.Flags().String("nerdctl-archive", "", "local file path (not URL) of nerdctl-full-VERSION-linux-GOARCH.tar.gz") + hostagentCommand.Flags().String("additional-archive", "", "local file path (not URL) of an arbitrary archive to add to CIDATA") return hostagentCommand } @@ -70,6 +71,13 @@ func hostagentAction(cmd *cobra.Command, args []string) error { if nerdctlArchive != "" { opts = append(opts, hostagent.WithNerdctlArchive(nerdctlArchive)) } + additionalArchive, err := cmd.Flags().GetString("additional-archive") + if err != nil { + return err + } + if additionalArchive != "" { + opts = append(opts, hostagent.WithAdditionalArchive(additionalArchive)) + } ha, err := hostagent.New(instName, stdout, sigintCh, opts...) if err != nil { return err diff --git a/examples/default.yaml b/examples/default.yaml index 990280d01db..5169f45935e 100644 --- a/examples/default.yaml +++ b/examples/default.yaml @@ -196,6 +196,19 @@ containerd: # vim was not installed in the guest. Make sure the package system is working correctly. # Also see "/var/log/cloud-init-output.log" in the guest. +# Adds an additional archive which matches the system architecture to the CIDATA image which is mounted on boot. +# The additional archive will be available on the CIDATA disk at /additional.tgz. +# Useful for provisioning scripts that run before mounts (like `mode: dependency`) in case they need any file resources. +# See pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh for an example of how to use an archive +# from a provisioning script. +# additionalArchives: +# - location: "~/additional.amd64.tar.gz" +# arch: "x86_64" +# digest: "sha256:..." +# - location: "~/additional.aarch64.tar.gz" +# arch: "aarch64" +# digest: "sha256:..." + # ===================================================================== # # FURTHER ADVANCED CONFIGURATION # ===================================================================== # diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index b1b3d41c5a3..d41828d0ab0 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -104,7 +104,7 @@ func setupEnv(y *limayaml.LimaYAML) (map[string]string, error) { return env, nil } -func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string) error { +func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive, additionalArchive string) error { if err := limayaml.Validate(*y, false); err != nil { return err } @@ -287,6 +287,19 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort }) } + if additionalArchive != "" { + additionaltgzR, err := os.Open(additionalArchive) + if err != nil { + return err + } + defer additionaltgzR.Close() + layout = append(layout, iso9660util.Entry{ + // ISO9660 requires len(Path) <= 30 + Path: "additional.tgz", + Reader: additionaltgzR, + }) + } + return iso9660util.Write(filepath.Join(instDir, filenames.CIDataISO), "cidata", layout) } diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 50bbb42f1bd..731d2bbe338 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -57,7 +57,8 @@ type HostAgent struct { } type options struct { - nerdctlArchive string // local path, not URL + nerdctlArchive string // local path, not URL + additionalArchive string // local path, not URL } type Opt func(*options) error @@ -69,6 +70,13 @@ func WithNerdctlArchive(s string) Opt { } } +func WithAdditionalArchive(s string) Opt { + return func(o *options) error { + o.additionalArchive = s + return nil + } +} + // New creates the HostAgent. // // stdout is for emitting JSON lines of Events. @@ -107,7 +115,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt } } - if err := cidata.GenerateISO9660(inst.Dir, instName, y, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive); err != nil { + if err := cidata.GenerateISO9660(inst.Dir, instName, y, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, o.additionalArchive); err != nil { return nil, err } diff --git a/pkg/limayaml/limayaml.go b/pkg/limayaml/limayaml.go index bb7622fc3eb..d75f496d95d 100644 --- a/pkg/limayaml/limayaml.go +++ b/pkg/limayaml/limayaml.go @@ -7,30 +7,31 @@ import ( ) type LimaYAML struct { - Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"` - Images []Image `yaml:"images" json:"images"` // REQUIRED - CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"` - CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"` - Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes - Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes - Mounts []Mount `yaml:"mounts,omitempty" json:"mounts,omitempty"` - MountType *MountType `yaml:"mountType,omitempty" json:"mountType,omitempty"` - SSH SSH `yaml:"ssh,omitempty" json:"ssh,omitempty"` // REQUIRED (FIXME) - Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"` - Video Video `yaml:"video,omitempty" json:"video,omitempty"` - Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"` - Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"` - Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"` - PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"` - Message string `yaml:"message,omitempty" json:"message,omitempty"` - Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"` - Network NetworkDeprecated `yaml:"network,omitempty" json:"network,omitempty"` // DEPRECATED, use `networks` instead - Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"` - DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"` - HostResolver HostResolver `yaml:"hostResolver,omitempty" json:"hostResolver,omitempty"` - UseHostResolver *bool `yaml:"useHostResolver,omitempty" json:"useHostResolver,omitempty"` // DEPRECATED, use `HostResolver.Enabled` instead - PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"` - CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"` + Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"` + Images []Image `yaml:"images" json:"images"` // REQUIRED + CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"` + CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"` + Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes + Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes + Mounts []Mount `yaml:"mounts,omitempty" json:"mounts,omitempty"` + MountType *MountType `yaml:"mountType,omitempty" json:"mountType,omitempty"` + SSH SSH `yaml:"ssh,omitempty" json:"ssh,omitempty"` // REQUIRED (FIXME) + Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"` + Video Video `yaml:"video,omitempty" json:"video,omitempty"` + Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"` + Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"` + Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"` + PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"` + Message string `yaml:"message,omitempty" json:"message,omitempty"` + Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"` + Network NetworkDeprecated `yaml:"network,omitempty" json:"network,omitempty"` // DEPRECATED, use `networks` instead + Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"` + DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"` + HostResolver HostResolver `yaml:"hostResolver,omitempty" json:"hostResolver,omitempty"` + UseHostResolver *bool `yaml:"useHostResolver,omitempty" json:"useHostResolver,omitempty"` // DEPRECATED, use `HostResolver.Enabled` instead + PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"` + CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"` + AdditionalArchives []File `yaml:"additionalArchives,omitempty" json:"additionalArchives,omitempty"` } type Arch = string diff --git a/pkg/start/start.go b/pkg/start/start.go index f87e267c5e7..e724af1a3aa 100644 --- a/pkg/start/start.go +++ b/pkg/start/start.go @@ -43,14 +43,27 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) { return "", nil } - errs := make([]error, len(y.Containerd.Archives)) - for i := range y.Containerd.Archives { - f := &y.Containerd.Archives[i] - if f.Arch != *y.Arch { + location, errs := downloadAndCacheArchiveForArch(y.Containerd.Archives, *y.Arch, "nerdctl") + if location == "" { + return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v", + len(y.Containerd.Archives), errs) + } + + return location, nil +} + +// downloadAndCacheArchiveForArch iterates through a slice of File and tries to download the provided +// file which matches the supplied arch. +// The downloader caches remote downloads to disk, and then returns a file path to the archive. +func downloadAndCacheArchiveForArch(files []limayaml.File, arch limayaml.Arch, archiveType string) (string, []error) { + errs := make([]error, len(files)) + for i := range files { + f := &files[i] + if f.Arch != arch { errs[i] = fmt.Errorf("unsupported arch: %q", f.Arch) continue } - logrus.WithField("digest", f.Digest).Infof("Attempting to download the nerdctl archive from %q", f.Location) + logrus.WithField("digest", f.Digest).Infof("Attempting to download the %s archive from %q", archiveType, f.Location) res, err := downloader.Download("", f.Location, downloader.WithCache(), downloader.WithExpectedDigest(f.Digest)) if err != nil { errs[i] = fmt.Errorf("failed to download %q: %w", f.Location, err) @@ -58,7 +71,7 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) { } switch res.Status { case downloader.StatusDownloaded: - logrus.Infof("Downloaded the nerdctl archive from %q", f.Location) + logrus.Infof("Downloaded the %s archive from %q", archiveType, f.Location) case downloader.StatusUsedCache: logrus.Infof("Using cache %q", res.CachePath) default: @@ -68,13 +81,11 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) { if downloader.IsLocal(f.Location) { return f.Location, nil } - return "", fmt.Errorf("cache did not contain %q", f.Location) + errs[i] = fmt.Errorf("cache did not contain %q", f.Location) } return res.CachePath, nil } - - return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v", - len(y.Containerd.Archives), errs) + return "", errs } func Start(ctx context.Context, inst *store.Instance) error { @@ -132,6 +143,15 @@ func Start(ctx context.Context, inst *store.Instance) error { if nerdctlArchiveCache != "" { args = append(args, "--nerdctl-archive", nerdctlArchiveCache) } + if len(y.AdditionalArchives) > 0 { + location, errs := downloadAndCacheArchiveForArch(y.AdditionalArchives, *y.Arch, "additionalArchive") + if location == "" { + return fmt.Errorf("failed to download the additionalArchive archive, attempted %d candidates, errors=%v", + len(y.AdditionalArchives), errs) + } + args = append(args, "--additional-archive", location) + } + args = append(args, inst.Name) haCmd := exec.CommandContext(ctx, self, args...)