Browse Source

Merge pull request #47568 from vvoland/c8d-list-fix

c8d/list: Fix premature `Images` return
Sebastiaan van Stijn 1 năm trước cách đây
mục cha
commit
dd146571ea

+ 5 - 1
daemon/containerd/image_list.go

@@ -154,9 +154,13 @@ func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions)
 
 	for _, img := range uniqueImages {
 		image, allChainsIDs, err := i.imageSummary(ctx, img, platformMatcher, opts, tagsByDigest)
-		if err != nil || image == nil {
+		if err != nil {
 			return nil, err
 		}
+		// No error, but image should be skipped.
+		if image == nil {
+			continue
+		}
 
 		summaries = append(summaries, image)
 

+ 10 - 0
daemon/containerd/image_list_test.go

@@ -48,6 +48,9 @@ func TestImageList(t *testing.T) {
 	twoplatform, err := specialimage.TwoPlatform(blobsDir)
 	assert.NilError(t, err)
 
+	emptyIndex, err := specialimage.EmptyIndex(blobsDir)
+	assert.NilError(t, err)
+
 	cs := &blobsDirContentStore{blobs: filepath.Join(blobsDir, "blobs/sha256")}
 
 	snapshotter := &testSnapshotterService{}
@@ -92,6 +95,13 @@ func TestImageList(t *testing.T) {
 				assert.Check(t, is.DeepEqual(all[1].RepoTags, []string{"twoplatform:latest"}))
 			},
 		},
+		{
+			name:   "three images, one is an empty index",
+			images: imagesFromIndex(multilayer, emptyIndex, twoplatform),
+			check: func(t *testing.T, all []*imagetypes.Summary) {
+				assert.Check(t, is.Len(all, 2))
+			},
+		},
 	} {
 		tc := tc
 		t.Run(tc.name, func(t *testing.T) {

+ 25 - 0
internal/testutils/specialimage/emptyindex.go

@@ -0,0 +1,25 @@
+package specialimage
+
+import (
+	"github.com/distribution/reference"
+	"github.com/opencontainers/image-spec/specs-go"
+	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
+)
+
+// EmptyIndex creates an image index with no manifests.
+// This is equivalent to `tianon/scratch:index`.
+func EmptyIndex(dir string) (*ocispec.Index, error) {
+	const imageRef = "emptyindex:latest"
+
+	index := ocispec.Index{
+		Versioned: specs.Versioned{SchemaVersion: 2},
+		MediaType: ocispec.MediaTypeImageIndex,
+		Manifests: []ocispec.Descriptor{},
+	}
+
+	ref, err := reference.ParseNormalizedNamed(imageRef)
+	if err != nil {
+		return nil, err
+	}
+	return multiPlatformImage(dir, ref, index)
+}