diff --git a/ctrd/image.go b/ctrd/image.go index 5331b782e..1b3d26c19 100644 --- a/ctrd/image.go +++ b/ctrd/image.go @@ -96,6 +96,13 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag continue } + desc, err := image.Config(ctx, wrapperCli.client.ContentStore(), platforms.Default()) + if err != nil { + logrus.Errorf("failed to get image id %s: %v", image.Name, err) + continue + } + id := desc.Digest.String() + refNamed, err := reference.ParseNamedReference(image.Name) // occur error, skip it if err != nil { @@ -118,13 +125,7 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag continue } imageInfo.Size = size - - // generate image ID by imageInfo JSON. - imageID, err := generateID(&imageInfo) - if err != nil { - return nil, err - } - imageInfo.ID = imageID.String() + imageInfo.ID = id if refDigest, ok := refNamed.(reference.Digested); ok { imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String()) @@ -206,6 +207,12 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au return types.ImageInfo{}, err } + desc, err := img.Config(ctx) + if err != nil { + return types.ImageInfo{}, err + } + id := desc.Digest.String() + logrus.Infof("success to pull image: %s", img.Name()) ociImage, err := c.GetOciImage(ctx, ref) @@ -217,24 +224,21 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au // fill struct ImageInfo { imageInfo.Size = size - // generate image ID by imageInfo JSON. - imageID, err := generateID(&imageInfo) + imageInfo.ID = id + + refNamed, err := reference.ParseNamedReference(ref) if err != nil { return types.ImageInfo{}, err } - imageInfo.ID = imageID.String() - refNamed, err := reference.ParseNamedReference(img.Name()) - if err != nil { - return types.ImageInfo{}, err + if refTag, ok := refNamed.(reference.Tagged); ok { + imageInfo.RepoTags = append(imageInfo.RepoTags, refTag.String()) } if refDigest, ok := refNamed.(reference.Digested); ok { imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String()) } else { - refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged) - imageInfo.RepoTags = append(imageInfo.RepoTags, refTagged.String()) - imageInfo.RepoDigests = append(imageInfo.RepoDigests, refTagged.Name()+"@"+img.Target().Digest.String()) + imageInfo.RepoDigests = append(imageInfo.RepoDigests, refNamed.Name()+"@"+img.Target().Digest.String()) } } diff --git a/daemon/mgr/cri.go b/daemon/mgr/cri.go index 8a5e06c36..f8015a999 100644 --- a/daemon/mgr/cri.go +++ b/daemon/mgr/cri.go @@ -761,8 +761,15 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques return nil, err } + // We may get images with same id and different repoTag or repoDigest, + // so we need idExist to de-dup. + idExist := make(map[string]bool) + images := make([]*runtime.Image, 0, len(imageList)) for _, i := range imageList { + if _, ok := idExist[i.ID]; ok { + continue + } // NOTE: we should query image cache to get the correct image info. imageInfo, err := c.ImageMgr.GetImage(ctx, strings.TrimPrefix(i.ID, "sha256:")) if err != nil { @@ -774,6 +781,7 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques continue } images = append(images, image) + idExist[i.ID] = true } return &runtime.ListImagesResponse{Images: images}, nil diff --git a/hack/cri-test/test-cri.sh b/hack/cri-test/test-cri.sh index 537242612..1a39ebb19 100755 --- a/hack/cri-test/test-cri.sh +++ b/hack/cri-test/test-cri.sh @@ -26,7 +26,7 @@ POUCH_SOCK="/var/run/pouchcri.sock" CRI_FOCUS=${CRI_FOCUS:-} # CRI_SKIP skips the test to skip. -CRI_SKIP=${CRI_SKIP:-"RunAsUserName|seccomp localhost|should error on create with wrong options|listImage should get exactly 2 repoTags"} +CRI_SKIP=${CRI_SKIP:-"RunAsUserName|seccomp localhost|should error on create with wrong options"} # REPORT_DIR is the the directory to store test logs. REPORT_DIR=${REPORT_DIR:-"/tmp/test-cri"}