Skip to content

Commit

Permalink
WIP, tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 committed Jan 23, 2024
1 parent 39c0cd1 commit 2c64297
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/oci"
"github.com/defenseunicorns/zarf/src/pkg/ocizarf"
"github.com/defenseunicorns/zarf/src/pkg/packager"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/utils"
Expand Down Expand Up @@ -142,7 +143,7 @@ func downloadInitPackage(cacheDirectory string) (string, error) {
if err != nil {
return "", err
}
source := sources.OCISource{OrasRemote: remote}
source := &sources.OCISource{ZarfOrasRemote: &ocizarf.ZarfOrasRemote{OrasRemote: remote}}
return source.Collect(cacheDirectory)
}
// Otherwise, exit and tell the user to manually download the init-package
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/oci"
"github.com/defenseunicorns/zarf/src/pkg/ocizarf"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/pki"
"github.com/defenseunicorns/zarf/src/types"
Expand Down Expand Up @@ -188,7 +189,7 @@ var downloadInitCmd = &cobra.Command{
message.Fatalf(err, lang.CmdToolsDownloadInitErr, err.Error())
}

source := &sources.OCISource{OrasRemote: remote}
source := &sources.OCISource{ZarfOrasRemote: &ocizarf.ZarfOrasRemote{OrasRemote: remote}}

_, err = source.Collect(outputDirectory)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/pkg/oci/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
type log func(string, ...any)

// OrasRemote is a wrapper around the Oras remote repository that includes a progress bar for interactive feedback.
// Do we want to start exporting fields in this struct? For example log may come in handy?
type OrasRemote struct {
repo *remote.Repository
root *OCIManifest
Expand Down
39 changes: 1 addition & 38 deletions src/pkg/oci/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"slices"

"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -31,11 +30,6 @@ var (
ZarfPackageImagesBlobsDir = filepath.Join("images", "blobs", "sha256")
)

var (
// PackageAlwaysPull is a list of paths that will always be pulled from the remote repository.
PackageAlwaysPull = []string{layout.ZarfYAML, layout.Checksums, layout.Signature}
)

// FileDescriptorExists returns true if the given file exists in the given directory with the expected SHA.
func (o *OrasRemote) FileDescriptorExists(desc ocispec.Descriptor, destinationDir string) bool {
rel := desc.Annotations[ocispec.AnnotationTitle]
Expand Down Expand Up @@ -69,29 +63,8 @@ func (o *OrasRemote) FileDescriptorExists(desc ocispec.Descriptor, destinationDi
//
// layersToPull is an optional parameter that allows the caller to specify which layers to pull.
//
// The following layers will ALWAYS be pulled if they exist:
// - zarf.yaml
// - checksums.txt
// - zarf.yaml.sig
// ?! Now that we are going to 100% going to call this function with parameters do we still want layerstopull to be optional?
func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersToPull ...ocispec.Descriptor) ([]ocispec.Descriptor, error) {
isPartialPull := len(layersToPull) > 0
o.log("Pulling", o.repo.Reference)

manifest, err := o.FetchRoot()
if err != nil {
return nil, err
}

if isPartialPull {
for _, path := range PackageAlwaysPull {
desc := manifest.Locate(path)
layersToPull = append(layersToPull, desc)
}
} else {
layersToPull = append(layersToPull, manifest.Layers...)
}
layersToPull = append(layersToPull, manifest.Config)

// de-duplicate layers
layersToPull = RemoveDuplicateDescriptors(layersToPull)

Expand Down Expand Up @@ -206,13 +179,3 @@ func (o *OrasRemote) PullPackagePaths(paths []string, destinationDir string) ([]
}
return layersPulled, nil
}

// PullPackageMetadata pulls the package metadata from the remote repository and saves it to `destinationDir`.
func (o *OrasRemote) PullPackageMetadata(destinationDir string) ([]ocispec.Descriptor, error) {
return o.PullPackagePaths(PackageAlwaysPull, destinationDir)
}

// PullPackageSBOM pulls the package's sboms.tar from the remote repository and saves it to `destinationDir`.
func (o *OrasRemote) PullPackageSBOM(destinationDir string) ([]ocispec.Descriptor, error) {
return o.PullPackagePaths([]string{layout.SBOMTar}, destinationDir)
}
2 changes: 1 addition & 1 deletion src/pkg/ocizarf/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
)

type ZarfOrasRemote struct {

Check warning on line 19 in src/pkg/ocizarf/common.go

View workflow job for this annotation

GitHub Actions / validate

exported type ZarfOrasRemote should have comment or be unexported
oci.OrasRemote
*oci.OrasRemote
}

// log is a function that logs a message.
Expand Down
46 changes: 46 additions & 0 deletions src/pkg/ocizarf/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,49 @@ import (
"slices"

"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/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

var (
// PackageAlwaysPull is a list of paths that will always be pulled from the remote repository.
PackageAlwaysPull = []string{layout.ZarfYAML, layout.Checksums, layout.Signature}
)

// PullPackage pulls the package from the remote repository and saves it to the given path.
//
// layersToPull is an optional parameter that allows the caller to specify which layers to pull.
//
// The following layers will ALWAYS be pulled if they exist:
// - zarf.yaml
// - checksums.txt
// - zarf.yaml.sig
func (o *ZarfOrasRemote) PullPackage(destinationDir string, concurrency int, layersToPull ...ocispec.Descriptor) ([]ocispec.Descriptor, error) {
isPartialPull := len(layersToPull) > 0
message.Infof("Pulling", o.Repo())

manifest, err := o.FetchRoot()
if err != nil {
return nil, err
}

if isPartialPull {
for _, path := range PackageAlwaysPull {
desc := manifest.Locate(path)
layersToPull = append(layersToPull, desc)
}
} else {
layersToPull = append(layersToPull, manifest.Layers...)
}
layersToPull = append(layersToPull, manifest.Config)

return o.OrasRemote.PullPackage(destinationDir, concurrency, layersToPull...)
}

// LayersFromRequestedComponents returns the descriptors for the given components from the root manifest.
// It also retrieves the descriptors for all image layers that are required by the components.
//
Expand Down Expand Up @@ -98,3 +134,13 @@ func LayersFromRequestedComponents(o *oci.OrasRemote, requestedComponents []stri
}
return layers, nil
}

// PullPackageMetadata pulls the package metadata from the remote repository and saves it to `destinationDir`.
func (o *ZarfOrasRemote) PullPackageMetadata(destinationDir string) ([]ocispec.Descriptor, error) {
return o.PullPackagePaths(PackageAlwaysPull, destinationDir)
}

// PullPackageSBOM pulls the package's sboms.tar from the remote repository and saves it to `destinationDir`.
func (o *ZarfOrasRemote) PullPackageSBOM(destinationDir string) ([]ocispec.Descriptor, error) {
return o.PullPackagePaths([]string{layout.SBOMTar}, destinationDir)
}
3 changes: 2 additions & 1 deletion src/pkg/packager/sources/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"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/ocizarf"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func New(pkgOpts *types.ZarfPackageOptions) (PackageSource, error) {
if err != nil {
return nil, err
}
source = &OCISource{pkgOpts, remote}
source = &OCISource{pkgOpts, &ocizarf.ZarfOrasRemote{remote}}
case "tarball":
source = &TarballSource{pkgOpts}
case "http", "https", "sget":
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
// OCISource is a package source for OCI registries.
type OCISource struct {
*types.ZarfPackageOptions
*oci.OrasRemote
*ocizarf.ZarfOrasRemote
}

// LoadPackage loads a package from an OCI registry.
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *OCISource) LoadPackage(dst *layout.PackagePaths, unarchiveAll bool) (er
func (s *OCISource) LoadPackageMetadata(dst *layout.PackagePaths, wantSBOM bool, skipValidation bool) (err error) {
var pkg types.ZarfPackage

toPull := oci.PackageAlwaysPull
toPull := ocizarf.PackageAlwaysPull
if wantSBOM {
toPull = append(toPull, layout.SBOMTar)
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"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/ocizarf"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
"github.com/mholt/archiver/v3"
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *TarballSource) LoadPackageMetadata(dst *layout.PackagePaths, wantSBOM b
}
}

toExtract := oci.PackageAlwaysPull
toExtract := ocizarf.PackageAlwaysPull
if wantSBOM {
toExtract = append(toExtract, layout.SBOMTar)
}
Expand Down

0 comments on commit 2c64297

Please sign in to comment.