From f039affe91b664f601b274a5e66d29bf030ca75f Mon Sep 17 00:00:00 2001 From: decleaver <85503726+decleaver@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:04:48 -0700 Subject: [PATCH] chore: update NewOrasRemote to take ocispec.Platform as an argument (#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 --- src/cmd/initialize.go | 2 +- src/cmd/tools/zarf.go | 2 +- src/pkg/oci/common.go | 26 +++++++++------------- src/pkg/packager/composer/oci.go | 2 +- src/pkg/packager/create_stages.go | 4 ++-- src/pkg/packager/publish.go | 4 ++-- src/pkg/packager/sources/new.go | 2 +- src/test/e2e/50_oci_publish_deploy_test.go | 4 ++-- 8 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 799896b066..84e93fa7bf 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -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 } diff --git a/src/cmd/tools/zarf.go b/src/cmd/tools/zarf.go index d835674ed6..27cc36da6a 100644 --- a/src/cmd/tools/zarf.go +++ b/src/cmd/tools/zarf.go @@ -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()) } diff --git a/src/pkg/oci/common.go b/src/pkg/oci/common.go index c228ec0675..873d5e2846 100644 --- a/src/pkg/oci/common.go +++ b/src/pkg/oci/common.go @@ -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 diff --git a/src/pkg/packager/composer/oci.go b/src/pkg/packager/composer/oci.go index 0477d04b97..56418eadfe 100644 --- a/src/pkg/packager/composer/oci.go +++ b/src/pkg/packager/composer/oci.go @@ -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 } diff --git a/src/pkg/packager/create_stages.go b/src/pkg/packager/create_stages.go index 2ae7f06ad9..07d5116b68 100644 --- a/src/pkg/packager/create_stages.go +++ b/src/pkg/packager/create_stages.go @@ -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 } @@ -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 } diff --git a/src/pkg/packager/publish.go b/src/pkg/packager/publish.go index 6022eeada2..c55b06602d 100644 --- a/src/pkg/packager/publish.go +++ b/src/pkg/packager/publish.go @@ -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 } @@ -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 } diff --git a/src/pkg/packager/sources/new.go b/src/pkg/packager/sources/new.go index 9cd8c07c1a..2ac083f129 100644 --- a/src/pkg/packager/sources/new.go +++ b/src/pkg/packager/sources/new.go @@ -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 } diff --git a/src/test/e2e/50_oci_publish_deploy_test.go b/src/test/e2e/50_oci_publish_deploy_test.go index 55a038715b..d4011ee951 100644 --- a/src/test/e2e/50_oci_publish_deploy_test.go +++ b/src/test/e2e/50_oci_publish_deploy_test.go @@ -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])