Skip to content

Commit

Permalink
chore: update NewOrasRemote to take ocispec.Platform as an argument (#…
Browse files Browse the repository at this point in the history
…2241)

## Description

makes the ocispec's Platform a required arg to NewOrasRemote. With the
Platform as an arg, Zarf lib users no longer have to remember to pass in
the WithArch mod because the compiler will catch the error

## Related Issue

Relates to #2240 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed

---------

Co-authored-by: Wayne Starr <[email protected]>
  • Loading branch information
decleaver and Racer159 authored Jan 23, 2024
1 parent 8da4fda commit f039aff
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func downloadInitPackage(cacheDirectory string) (string, error) {

// If the user wants to download the init-package, download it
if confirmDownload {
remote, err := oci.NewOrasRemote(url, oci.WithArch(config.GetArch()))
remote, err := oci.NewOrasRemote(url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ var downloadInitCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
url := oci.GetInitPackageURL(config.CLIVersion)

remote, err := oci.NewOrasRemote(url, oci.WithArch(config.GetArch()))
remote, err := oci.NewOrasRemote(url, oci.PlatformForArch(config.GetArch()))
if err != nil {
message.Fatalf(err, lang.CmdToolsDownloadInitErr, err.Error())
}
Expand Down
26 changes: 10 additions & 16 deletions src/pkg/oci/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,32 @@ func WithInsecureSkipVerify(insecure bool) Modifier {
}
}

// WithTargetPlatform sets the target platform for the remote
func WithTargetPlatform(platform *ocispec.Platform) Modifier {
return func(o *OrasRemote) {
o.targetPlatform = platform
}
}

// WithSkeletonArch sets the target architecture for the remote to skeleton
func WithSkeletonArch() Modifier {
return WithTargetPlatform(&ocispec.Platform{
// PlatformForSkeleton sets the target architecture for the remote to skeleton
func PlatformForSkeleton() ocispec.Platform {
return ocispec.Platform{
OS: MultiOS,
Architecture: SkeletonArch,
})
}
}

// WithArch sets the target architecture for the remote
func WithArch(arch string) Modifier {
return WithTargetPlatform(&ocispec.Platform{
// PlatformForArch sets the target architecture for the remote
func PlatformForArch(arch string) ocispec.Platform {
return ocispec.Platform{
OS: MultiOS,
Architecture: arch,
})
}
}

// NewOrasRemote returns an oras remote repository client and context for the given url.
//
// Registry auth is handled by the Docker CLI's credential store and checked before returning the client
func NewOrasRemote(url string, mods ...Modifier) (*OrasRemote, error) {
func NewOrasRemote(url string, platform ocispec.Platform, mods ...Modifier) (*OrasRemote, error) {
ref, err := registry.ParseReference(strings.TrimPrefix(url, helpers.OCIURLPrefix))
if err != nil {
return nil, fmt.Errorf("failed to parse OCI reference %q: %w", url, err)
}
o := &OrasRemote{}
o.targetPlatform = &platform

if err := o.setRepository(ref); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/composer/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ic *ImportChain) getRemote(url string) (*oci.OrasRemote, error) {
return ic.remote, nil
}
var err error
ic.remote, err = oci.NewOrasRemote(url, oci.WithSkeletonArch())
ic.remote, err = oci.NewOrasRemote(url, oci.PlatformForSkeleton())
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/create_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (p *Packager) output() error {
if err != nil {
return err
}
remote, err := oci.NewOrasRemote(ref)
remote, err := oci.NewOrasRemote(ref, oci.PlatformForArch(config.GetArch()))
if err != nil {
return err
}
Expand Down Expand Up @@ -652,7 +652,7 @@ func (p *Packager) loadDifferentialData() error {

// Load the package spec of the package we're using as a 'reference' for the differential build
if helpers.IsOCIURL(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath) {
remote, err := oci.NewOrasRemote(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath)
remote, err := oci.NewOrasRemote(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath, oci.PlatformForArch(config.GetArch()))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (p *Packager) Publish() (err error) {
p.cfg.PublishOpts.PackageDestination = p.cfg.PublishOpts.PackageDestination + "/" + packageName

arch := config.GetArch()
dstRemote, err := oci.NewOrasRemote(p.cfg.PublishOpts.PackageDestination, oci.WithArch(arch))
dstRemote, err := oci.NewOrasRemote(p.cfg.PublishOpts.PackageDestination, oci.PlatformForArch(arch))
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (p *Packager) Publish() (err error) {
return err
}

remote, err := oci.NewOrasRemote(ref)
remote, err := oci.NewOrasRemote(ref, oci.PlatformForArch(config.GetArch()))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/sources/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func New(pkgOpts *types.ZarfPackageOptions) (PackageSource, error) {
pkgSrc = fmt.Sprintf("%s@sha256:%s", pkgSrc, pkgOpts.Shasum)
}
arch := config.GetArch()
remote, err := oci.NewOrasRemote(pkgSrc, oci.WithArch(arch))
remote, err := oci.NewOrasRemote(pkgSrc, oci.PlatformForArch(arch))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/50_oci_publish_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func (suite *PublishDeploySuiteTestSuite) Test_3_Copy() {
e2e.SetupDockerRegistry(t, dstRegistryPort)
defer e2e.TeardownRegistry(t, dstRegistryPort)

src, err := oci.NewOrasRemote(ref, oci.WithPlainHTTP(true), oci.WithArch(e2e.Arch))
src, err := oci.NewOrasRemote(ref, oci.PlatformForArch(e2e.Arch), oci.WithPlainHTTP(true))
suite.NoError(err)

dst, err := oci.NewOrasRemote(dstRef, oci.WithPlainHTTP(true), oci.WithArch(e2e.Arch))
dst, err := oci.NewOrasRemote(dstRef, oci.PlatformForArch(e2e.Arch), oci.WithPlainHTTP(true))
suite.NoError(err)

reg, err := remote.NewRegistry(strings.Split(dstRef, "/")[0])
Expand Down

0 comments on commit f039aff

Please sign in to comment.