Skip to content

Commit 23d565c

Browse files
committed
c8d: Improve error message for platform not found
Return a similar error as the graphdrivers implementation when an image was found, but the requested platform is not present locally or in the image. The message doesn't include the "actual" platform, as it doesn't make sense with the multi-platform images. With graphdrivers all images were single platform. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent e73c2a0 commit 23d565c

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

daemon/containerd/image.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,46 @@ import (
2525

2626
var errInconsistentData error = errors.New("consistency error: data changed during operation, retry")
2727

28+
type errPlatformNotFound struct {
29+
wanted ocispec.Platform
30+
imageRef string
31+
}
32+
33+
func (e *errPlatformNotFound) NotFound() {}
34+
func (e *errPlatformNotFound) Error() string {
35+
msg := "image with reference " + e.imageRef + " was found but does not match the specified platform"
36+
if e.wanted.OS != "" {
37+
msg += ": wanted " + platforms.Format(e.wanted)
38+
}
39+
return msg
40+
}
41+
2842
// GetImage returns an image corresponding to the image referred to by refOrID.
2943
func (i *ImageService) GetImage(ctx context.Context, refOrID string, options backend.GetImageOpts) (*image.Image, error) {
3044
img, err := i.resolveImage(ctx, refOrID)
3145
if err != nil {
3246
return nil, err
3347
}
48+
3449
pm := matchAllWithPreference(platforms.Default())
3550
if options.Platform != nil {
3651
pm = platforms.OnlyStrict(*options.Platform)
3752
}
38-
return i.getImageV1(ctx, img, pm)
53+
54+
imgV1, err := i.getImageV1(ctx, img, pm)
55+
if err != nil {
56+
var e *errPlatformNotFound
57+
if errors.As(err, &e) {
58+
if options.Platform != nil {
59+
e.wanted = *options.Platform
60+
} else {
61+
e.wanted = platforms.DefaultSpec()
62+
}
63+
}
64+
return nil, err
65+
}
66+
67+
return imgV1, nil
3968
}
4069

4170
// getImageV1 gets the containerd image as a docker v1 image struct.
@@ -97,7 +126,9 @@ func (i *ImageService) presentImages(ctx context.Context, img containerdimages.I
97126
return nil, err
98127
}
99128
if len(presentImages) == 0 {
100-
return nil, errdefs.NotFound(errors.New("no matching image found"))
129+
return nil, &errPlatformNotFound{
130+
imageRef: img.Name,
131+
}
101132
}
102133

103134
sort.SliceStable(presentImages, func(i, j int) bool {

daemon/containerd/image_history.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"github.com/containerd/platforms"
1010
"github.com/distribution/reference"
1111
imagetype "github.com/docker/docker/api/types/image"
12-
"github.com/docker/docker/daemon/images"
1312
dimages "github.com/docker/docker/daemon/images"
14-
"github.com/docker/docker/errdefs"
1513
"github.com/opencontainers/go-digest"
1614
"github.com/opencontainers/image-spec/identity"
1715
"github.com/pkg/errors"
@@ -31,9 +29,9 @@ func (i *ImageService) ImageHistory(ctx context.Context, name string) ([]*imaget
3129

3230
presentImages, err := i.presentImages(ctx, img, platform)
3331
if err != nil {
34-
if errdefs.IsNotFound(err) {
35-
r, _ := reference.ParseAnyReference(name)
36-
return nil, images.ErrImageDoesNotExist{Ref: r}
32+
var e *errPlatformNotFound
33+
if errors.As(err, &e) {
34+
e.wanted = platforms.DefaultSpec()
3735
}
3836
return nil, err
3937
}

0 commit comments

Comments
 (0)