From 25f66976a062685071a86e596b839843d908a94d Mon Sep 17 00:00:00 2001 From: razzle Date: Wed, 25 Oct 2023 15:27:28 -0500 Subject: [PATCH] changes from review Signed-off-by: razzle --- src/pkg/packager/compose.go | 6 +- src/pkg/packager/composer/extensions.go | 2 - src/pkg/packager/composer/list.go | 130 --------------------- src/pkg/packager/composer/oci.go | 144 ++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 135 deletions(-) create mode 100644 src/pkg/packager/composer/oci.go diff --git a/src/pkg/packager/compose.go b/src/pkg/packager/compose.go index 67e008f757..3011a3167d 100644 --- a/src/pkg/packager/compose.go +++ b/src/pkg/packager/compose.go @@ -19,9 +19,9 @@ func (p *Packager) composeComponents() error { for _, component := range p.cfg.Pkg.Components { arch := p.arch - // filter by architecture if set, otherwise use system architecture - if component.Only.Cluster.Architecture != "" { - arch = component.Only.Cluster.Architecture + // filter by architecture + if component.Only.Cluster.Architecture != "" && component.Only.Cluster.Architecture != arch { + continue } // build the import chain diff --git a/src/pkg/packager/composer/extensions.go b/src/pkg/packager/composer/extensions.go index 1fc6c35a33..9ef977ac21 100644 --- a/src/pkg/packager/composer/extensions.go +++ b/src/pkg/packager/composer/extensions.go @@ -6,7 +6,6 @@ package composer import ( "github.com/defenseunicorns/zarf/src/extensions/bigbang" - "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/types" ) @@ -24,7 +23,6 @@ func composeExtensions(c *types.ZarfComponent, override types.ZarfComponent, rel } else { if override.Extensions.BigBang.ValuesFiles != nil { c.Extensions.BigBang.ValuesFiles = append(c.Extensions.BigBang.ValuesFiles, override.Extensions.BigBang.ValuesFiles...) - message.Warnf("%s", c.Extensions.BigBang.ValuesFiles) } if override.Extensions.BigBang.FluxPatchFiles != nil { c.Extensions.BigBang.FluxPatchFiles = append(c.Extensions.BigBang.FluxPatchFiles, override.Extensions.BigBang.FluxPatchFiles...) diff --git a/src/pkg/packager/composer/list.go b/src/pkg/packager/composer/list.go index dba19c1ebb..436825feca 100644 --- a/src/pkg/packager/composer/list.go +++ b/src/pkg/packager/composer/list.go @@ -5,26 +5,17 @@ package composer import ( - "context" - "crypto/sha256" "fmt" - "os" "path/filepath" "strings" - "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/internal/packager/validate" "github.com/defenseunicorns/zarf/src/pkg/layout" - "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/oci" "github.com/defenseunicorns/zarf/src/pkg/packager/deprecated" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/pkg/utils/helpers" "github.com/defenseunicorns/zarf/src/types" - "github.com/mholt/archiver/v3" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "oras.land/oras-go/v2/content" - ocistore "oras.land/oras-go/v2/content/oci" ) // Node is a node in the import chain @@ -224,127 +215,6 @@ func (ic *ImportChain) Migrate(build types.ZarfBuildData) (warnings []string) { return warnings } -func (ic *ImportChain) getRemote(url string) (*oci.OrasRemote, error) { - if ic.remote != nil { - return ic.remote, nil - } - var err error - ic.remote, err = oci.NewOrasRemote(url) - if err != nil { - return nil, err - } - return ic.remote, nil -} - -// ContainsOCIImport returns true if the import chain contains a remote import -func (ic *ImportChain) ContainsOCIImport() bool { - // only the 2nd to last node may have a remote import - return ic.tail.prev != nil && ic.tail.prev.Import.URL != "" -} - -// OCIImportDefinition returns the url and name of the remote import -func (ic *ImportChain) OCIImportDefinition() (string, string) { - if !ic.ContainsOCIImport() { - return "", "" - } - return ic.tail.prev.Import.URL, ic.tail.prev.ImportName() -} - -func (ic *ImportChain) fetchOCISkeleton() error { - if !ic.ContainsOCIImport() { - return nil - } - node := ic.tail.prev - remote, err := ic.getRemote(node.Import.URL) - if err != nil { - return err - } - - manifest, err := remote.FetchRoot() - if err != nil { - return err - } - - name := node.ImportName() - - componentDesc := manifest.Locate(filepath.Join(layout.ComponentsDir, fmt.Sprintf("%s.tar", name))) - - cache := filepath.Join(config.GetAbsCachePath(), "oci") - if err := utils.CreateDirectory(cache, 0700); err != nil { - return err - } - - var tb, dir string - - // if there is not a tarball to fetch, create a directory named based upon - // the import url and the component name - if oci.IsEmptyDescriptor(componentDesc) { - h := sha256.New() - h.Write([]byte(node.Import.URL + name)) - id := fmt.Sprintf("%x", h.Sum(nil)) - - dir = filepath.Join(cache, "dirs", id) - - message.Debug("creating empty directory for remote component:", filepath.Join("", "oci", "dirs", id)) - } else { - tb = filepath.Join(cache, "blobs", "sha256", componentDesc.Digest.Encoded()) - dir = filepath.Join(cache, "dirs", componentDesc.Digest.Encoded()) - - store, err := ocistore.New(cache) - if err != nil { - return err - } - - ctx := context.TODO() - // ensure the tarball is in the cache - exists, err := store.Exists(ctx, componentDesc) - if err != nil { - return err - } else if !exists { - copyOpts := remote.CopyOpts - // TODO: investigate why the default FindSuccessors in CopyWithProgress is not working - copyOpts.FindSuccessors = content.Successors - if err := remote.CopyWithProgress([]ocispec.Descriptor{componentDesc}, store, copyOpts, cache); err != nil { - return err - } - exists, err := store.Exists(ctx, componentDesc) - if err != nil { - return err - } else if !exists { - return fmt.Errorf("failed to fetch remote component: %+v", componentDesc) - } - } - } - - if err := utils.CreateDirectory(dir, 0700); err != nil { - return err - } - - cwd, err := os.Getwd() - if err != nil { - return err - } - rel, err := filepath.Rel(cwd, dir) - if err != nil { - return err - } - // the tail node is the only node whose relativeToHead is based solely upon cwd<->cache - // contrary to the other nodes, which are based upon the previous node - ic.tail.relativeToHead = rel - - if oci.IsEmptyDescriptor(componentDesc) { - // nothing was fetched, nothing to extract - return nil - } - - tu := archiver.Tar{ - OverwriteExisting: true, - // removes // from the paths - StripComponents: 1, - } - return tu.Unarchive(tb, dir) -} - // Compose merges the import chain into a single component // fixing paths, overriding metadata, etc func (ic *ImportChain) Compose() (composed types.ZarfComponent, err error) { diff --git a/src/pkg/packager/composer/oci.go b/src/pkg/packager/composer/oci.go new file mode 100644 index 0000000000..afa8d2af9b --- /dev/null +++ b/src/pkg/packager/composer/oci.go @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package composer contains functions for composing components within Zarf packages. +package composer + +import ( + "context" + "crypto/sha256" + "fmt" + "os" + "path/filepath" + + "github.com/defenseunicorns/zarf/src/config" + "github.com/defenseunicorns/zarf/src/pkg/layout" + "github.com/defenseunicorns/zarf/src/pkg/message" + "github.com/defenseunicorns/zarf/src/pkg/oci" + "github.com/defenseunicorns/zarf/src/pkg/utils" + "github.com/mholt/archiver/v3" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "oras.land/oras-go/v2/content" + ocistore "oras.land/oras-go/v2/content/oci" +) + +func (ic *ImportChain) getRemote(url string) (*oci.OrasRemote, error) { + if ic.remote != nil { + return ic.remote, nil + } + var err error + ic.remote, err = oci.NewOrasRemote(url) + if err != nil { + return nil, err + } + return ic.remote, nil +} + +// ContainsOCIImport returns true if the import chain contains a remote import +func (ic *ImportChain) ContainsOCIImport() bool { + // only the 2nd to last node may have a remote import + return ic.tail.prev != nil && ic.tail.prev.Import.URL != "" +} + +// OCIImportDefinition returns the url and name of the remote import +func (ic *ImportChain) OCIImportDefinition() (string, string) { + if !ic.ContainsOCIImport() { + return "", "" + } + return ic.tail.prev.Import.URL, ic.tail.prev.ImportName() +} + +func (ic *ImportChain) fetchOCISkeleton() error { + if !ic.ContainsOCIImport() { + return nil + } + node := ic.tail.prev + remote, err := ic.getRemote(node.Import.URL) + if err != nil { + return err + } + + manifest, err := remote.FetchRoot() + if err != nil { + return err + } + + name := node.ImportName() + + componentDesc := manifest.Locate(filepath.Join(layout.ComponentsDir, fmt.Sprintf("%s.tar", name))) + + cache := filepath.Join(config.GetAbsCachePath(), "oci") + if err := utils.CreateDirectory(cache, 0700); err != nil { + return err + } + + var tb, dir string + + // if there is not a tarball to fetch, create a directory named based upon + // the import url and the component name + if oci.IsEmptyDescriptor(componentDesc) { + h := sha256.New() + h.Write([]byte(node.Import.URL + name)) + id := fmt.Sprintf("%x", h.Sum(nil)) + + dir = filepath.Join(cache, "dirs", id) + + message.Debug("creating empty directory for remote component:", filepath.Join("", "oci", "dirs", id)) + } else { + tb = filepath.Join(cache, "blobs", "sha256", componentDesc.Digest.Encoded()) + dir = filepath.Join(cache, "dirs", componentDesc.Digest.Encoded()) + + store, err := ocistore.New(cache) + if err != nil { + return err + } + + ctx := context.TODO() + // ensure the tarball is in the cache + exists, err := store.Exists(ctx, componentDesc) + if err != nil { + return err + } else if !exists { + copyOpts := remote.CopyOpts + // TODO: investigate why the default FindSuccessors in CopyWithProgress is not working + copyOpts.FindSuccessors = content.Successors + if err := remote.CopyWithProgress([]ocispec.Descriptor{componentDesc}, store, copyOpts, cache); err != nil { + return err + } + exists, err := store.Exists(ctx, componentDesc) + if err != nil { + return err + } else if !exists { + return fmt.Errorf("failed to fetch remote component: %+v", componentDesc) + } + } + } + + if err := utils.CreateDirectory(dir, 0700); err != nil { + return err + } + + cwd, err := os.Getwd() + if err != nil { + return err + } + rel, err := filepath.Rel(cwd, dir) + if err != nil { + return err + } + // the tail node is the only node whose relativeToHead is based solely upon cwd<->cache + // contrary to the other nodes, which are based upon the previous node + ic.tail.relativeToHead = rel + + if oci.IsEmptyDescriptor(componentDesc) { + // nothing was fetched, nothing to extract + return nil + } + + tu := archiver.Tar{ + OverwriteExisting: true, + // removes // from the paths + StripComponents: 1, + } + return tu.Unarchive(tb, dir) +}