Skip to content

Commit

Permalink
Make OCI provider default to current arch
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
willmurphyscode committed Jun 9, 2023
1 parent d894581 commit 0d73763
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stereoscope
import (
"context"
"fmt"
"runtime"

"github.com/wagoodman/go-partybus"

Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 0d73763

Please sign in to comment.