Skip to content

Commit

Permalink
reverting the previous code that I deleted, and adding a retry logic …
Browse files Browse the repository at this point in the history
…when pulling an image and the platform doesn't match

Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Nov 9, 2023
1 parent b12c9b3 commit 66da647
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/image/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewFetcher(logger logging.Logger, docker DockerClient, opts ...FetcherOptio
}

var ErrNotFound = errors.New("not found")
var ErrPlatformNotMatch = errors.New("does not match")

func (f *Fetcher) Fetch(ctx context.Context, name string, options FetchOptions) (imgutil.Image, error) {
name, err := pname.TranslateRegistry(name, f.registryMirrors, f.logger)
Expand Down Expand Up @@ -109,7 +110,17 @@ func (f *Fetcher) Fetch(ctx context.Context, name string, options FetchOptions)
}

f.logger.Debugf("Pulling image %s", style.Symbol(name))
err = f.pullImage(ctx, name, options.Platform)
platform := options.Platform
for i := 0; i <= 1; i++ {
err = f.pullImage(ctx, name, platform)
if err != nil && errors.Is(err, ErrPlatformNotMatch) {
f.logger.Warnf("%s, retrying without platform", err.Error())
err = nil
platform = ""
continue
}
break
}
if err != nil && !errors.Is(err, ErrNotFound) {
return nil, err
}
Expand Down Expand Up @@ -192,6 +203,9 @@ func (f *Fetcher) pullImage(ctx context.Context, imageID string, platform string

err = jsonmessage.DisplayJSONMessagesStream(rc, &colorizedWriter{writer}, termFd, isTerm, nil)
if err != nil {
if strings.Contains(err.Error(), "does not match the specified platform") {
return errors.Wrapf(ErrPlatformNotMatch, "image with reference %s was found but platform %s", style.Symbol(imageID), platform)
}
return err
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/image/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/buildpacks/imgutil"

"github.com/buildpacks/imgutil/local"
"github.com/buildpacks/imgutil/remote"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -213,6 +216,20 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
_, err := imageFetcher.Fetch(context.TODO(), repoName, image.FetchOptions{Daemon: true, PullPolicy: image.PullAlways, Platform: "some-unsupported-platform"})
h.AssertError(t, err, "unknown operating system or architecture")
})

when("remote platform does not match", func() {
it.Before(func() {
h.SkipIf(t, runtime.GOOS != "linux", "Skipped on non-linux")
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.WithDefaultPlatform(imgutil.Platform{OS: "linux", Architecture: ""}))
h.AssertNil(t, err)
h.AssertNil(t, img.Save())
})

it("retry without setting platform", func() {
_, err := imageFetcher.Fetch(context.TODO(), repoName, image.FetchOptions{Daemon: true, PullPolicy: image.PullAlways, Platform: "linux/amd64"})
h.AssertNil(t, err)
})
})
})
})

Expand Down

0 comments on commit 66da647

Please sign in to comment.