From 0d737635e7aac209393c48c9e156058a490e725b Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Fri, 9 Jun 2023 10:53:20 -0400 Subject: [PATCH] Make OCI provider default to current arch This was previously fixed by PR 152, but that PR introduced another bug. Re-implement platform defaulting after the revert of PR 152. Signed-off-by: Will Murphy --- client.go | 11 +++++++++++ test/integration/platform_test.go | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/client.go b/client.go index 778a93ea..ee3588fb 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,7 @@ package stereoscope import ( "context" "fmt" + "runtime" "github.com/wagoodman/go-partybus" @@ -139,6 +140,7 @@ func selectImageProvider(imgStr string, source image.Source, cfg config) (image. } provider = oci.NewProviderFromTarball(imgStr, tempDirGenerator) case image.OciRegistrySource: + defaultPlatformIfNil(&cfg) provider = oci.NewProviderFromRegistry(imgStr, tempDirGenerator, cfg.Registry, cfg.Platform) case image.SingularitySource: if cfg.Platform != nil { @@ -151,6 +153,15 @@ func selectImageProvider(imgStr string, source image.Source, cfg config) (image. return provider, nil } +func defaultPlatformIfNil(cfg *config) { + if cfg.Platform == nil { + p, err := image.NewPlatform(fmt.Sprintf("linux/%s", runtime.GOARCH)) + if err == nil { + cfg.Platform = p + } + } +} + // GetImage parses the user provided image string and provides an image object; // note: the source where the image should be referenced from is automatically inferred. func GetImage(ctx context.Context, userStr string, options ...Option) (*image.Image, error) { diff --git a/test/integration/platform_test.go b/test/integration/platform_test.go index e4f28d50..c4e74c7c 100644 --- a/test/integration/platform_test.go +++ b/test/integration/platform_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "runtime" "strings" "testing" @@ -133,6 +134,12 @@ func TestDigestThatNarrowsToOnePlatform(t *testing.T) { } } +func TestDefaultPlatformWithOciRegistry(t *testing.T) { + img, err := stereoscope.GetImageFromSource(context.TODO(), "busybox:1.31", image.OciRegistrySource) + require.NoError(t, err) + assertArchAndOs(t, img, runtime.GOARCH, "linux") +} + func assertArchAndOs(t *testing.T, img *image.Image, architecture string, os string) { type platform struct { Architecture string `json:"architecture"`