浏览代码

c8d: Fix getting image with platform

We weren't checking for the asked platform in the case the image was a
manifest, only if it was a manifest list.

Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
Djordje Lukic 1 年之前
父节点
当前提交
39fe25b69e
共有 1 个文件被更改,包括 13 次插入5 次删除
  1. 13 5
      daemon/containerd/image.go

+ 13 - 5
daemon/containerd/image.go

@@ -150,6 +150,11 @@ func (i *ImageService) GetImage(ctx context.Context, refOrID string, options ima
 }
 }
 
 
 func (i *ImageService) GetImageManifest(ctx context.Context, refOrID string, options imagetype.GetImageOpts) (*ocispec.Descriptor, error) {
 func (i *ImageService) GetImageManifest(ctx context.Context, refOrID string, options imagetype.GetImageOpts) (*ocispec.Descriptor, error) {
+	platform := platforms.AllPlatformsWithPreference(cplatforms.Default())
+	if options.Platform != nil {
+		platform = cplatforms.Only(*options.Platform)
+	}
+
 	cs := i.client.ContentStore()
 	cs := i.client.ContentStore()
 
 
 	desc, err := i.resolveDescriptor(ctx, refOrID)
 	desc, err := i.resolveDescriptor(ctx, refOrID)
@@ -158,15 +163,18 @@ func (i *ImageService) GetImageManifest(ctx context.Context, refOrID string, opt
 	}
 	}
 
 
 	if containerdimages.IsManifestType(desc.MediaType) {
 	if containerdimages.IsManifestType(desc.MediaType) {
+		if options.Platform != nil {
+			if desc.Platform == nil {
+				return nil, errdefs.NotFound(errors.Errorf("image with reference %s was found but does not match the specified platform: wanted %s, actual: nil", refOrID, cplatforms.Format(*options.Platform)))
+			} else if !platform.Match(*desc.Platform) {
+				return nil, errdefs.NotFound(errors.Errorf("image with reference %s was found but does not match the specified platform: wanted %s, actual: %s", refOrID, cplatforms.Format(*options.Platform), cplatforms.Format(*desc.Platform)))
+			}
+		}
+
 		return &desc, nil
 		return &desc, nil
 	}
 	}
 
 
 	if containerdimages.IsIndexType(desc.MediaType) {
 	if containerdimages.IsIndexType(desc.MediaType) {
-		platform := platforms.AllPlatformsWithPreference(cplatforms.Default())
-		if options.Platform != nil {
-			platform = cplatforms.Only(*options.Platform)
-		}
-
 		childManifests, err := containerdimages.LimitManifests(containerdimages.ChildrenHandler(cs), platform, 1)(ctx, desc)
 		childManifests, err := containerdimages.LimitManifests(containerdimages.ChildrenHandler(cs), platform, 1)(ctx, desc)
 		if err != nil {
 		if err != nil {
 			if cerrdefs.IsNotFound(err) {
 			if cerrdefs.IsNotFound(err) {