Skip to content

Commit

Permalink
changes from review
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <[email protected]>
  • Loading branch information
Noxsios committed Oct 25, 2023
1 parent 5701fc7 commit 25f6697
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 135 deletions.
6 changes: 3 additions & 3 deletions src/pkg/packager/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/packager/composer/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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...)
Expand Down
130 changes: 0 additions & 130 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("<zarf-cache>", "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 /<name>/ 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) {
Expand Down
144 changes: 144 additions & 0 deletions src/pkg/packager/composer/oci.go
Original file line number Diff line number Diff line change
@@ -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("<zarf-cache>", "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 /<component-name>/ from the paths
StripComponents: 1,
}
return tu.Unarchive(tb, dir)
}

0 comments on commit 25f6697

Please sign in to comment.