Skip to content

Commit 611837e

Browse files
authored
Merge pull request #1112 from YaoZengzeng/fix-digest
bugfix: we should get image ID from containerd
2 parents 453e2ed + 1f0f2f5 commit 611837e

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

ctrd/image.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag
9696
continue
9797
}
9898

99+
desc, err := image.Config(ctx, wrapperCli.client.ContentStore(), platforms.Default())
100+
if err != nil {
101+
logrus.Errorf("failed to get image id %s: %v", image.Name, err)
102+
continue
103+
}
104+
id := desc.Digest.String()
105+
99106
refNamed, err := reference.ParseNamedReference(image.Name)
100107
// occur error, skip it
101108
if err != nil {
@@ -118,13 +125,7 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag
118125
continue
119126
}
120127
imageInfo.Size = size
121-
122-
// generate image ID by imageInfo JSON.
123-
imageID, err := generateID(&imageInfo)
124-
if err != nil {
125-
return nil, err
126-
}
127-
imageInfo.ID = imageID.String()
128+
imageInfo.ID = id
128129

129130
if refDigest, ok := refNamed.(reference.Digested); ok {
130131
imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String())
@@ -206,6 +207,12 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au
206207
return types.ImageInfo{}, err
207208
}
208209

210+
desc, err := img.Config(ctx)
211+
if err != nil {
212+
return types.ImageInfo{}, err
213+
}
214+
id := desc.Digest.String()
215+
209216
logrus.Infof("success to pull image: %s", img.Name())
210217

211218
ociImage, err := c.GetOciImage(ctx, ref)
@@ -217,24 +224,21 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au
217224
// fill struct ImageInfo
218225
{
219226
imageInfo.Size = size
220-
// generate image ID by imageInfo JSON.
221-
imageID, err := generateID(&imageInfo)
227+
imageInfo.ID = id
228+
229+
refNamed, err := reference.ParseNamedReference(ref)
222230
if err != nil {
223231
return types.ImageInfo{}, err
224232
}
225-
imageInfo.ID = imageID.String()
226233

227-
refNamed, err := reference.ParseNamedReference(img.Name())
228-
if err != nil {
229-
return types.ImageInfo{}, err
234+
if refTag, ok := refNamed.(reference.Tagged); ok {
235+
imageInfo.RepoTags = append(imageInfo.RepoTags, refTag.String())
230236
}
231237

232238
if refDigest, ok := refNamed.(reference.Digested); ok {
233239
imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String())
234240
} else {
235-
refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged)
236-
imageInfo.RepoTags = append(imageInfo.RepoTags, refTagged.String())
237-
imageInfo.RepoDigests = append(imageInfo.RepoDigests, refTagged.Name()+"@"+img.Target().Digest.String())
241+
imageInfo.RepoDigests = append(imageInfo.RepoDigests, refNamed.Name()+"@"+img.Target().Digest.String())
238242
}
239243
}
240244

daemon/mgr/cri.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,15 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques
761761
return nil, err
762762
}
763763

764+
// We may get images with same id and different repoTag or repoDigest,
765+
// so we need idExist to de-dup.
766+
idExist := make(map[string]bool)
767+
764768
images := make([]*runtime.Image, 0, len(imageList))
765769
for _, i := range imageList {
770+
if _, ok := idExist[i.ID]; ok {
771+
continue
772+
}
766773
// NOTE: we should query image cache to get the correct image info.
767774
imageInfo, err := c.ImageMgr.GetImage(ctx, strings.TrimPrefix(i.ID, "sha256:"))
768775
if err != nil {
@@ -774,6 +781,7 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques
774781
continue
775782
}
776783
images = append(images, image)
784+
idExist[i.ID] = true
777785
}
778786

779787
return &runtime.ListImagesResponse{Images: images}, nil

hack/cri-test/test-cri.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ POUCH_SOCK="/var/run/pouchcri.sock"
2626
CRI_FOCUS=${CRI_FOCUS:-}
2727

2828
# CRI_SKIP skips the test to skip.
29-
CRI_SKIP=${CRI_SKIP:-"RunAsUserName|seccomp localhost|should error on create with wrong options|listImage should get exactly 2 repoTags"}
29+
CRI_SKIP=${CRI_SKIP:-"RunAsUserName|seccomp localhost|should error on create with wrong options"}
3030
# REPORT_DIR is the the directory to store test logs.
3131
REPORT_DIR=${REPORT_DIR:-"/tmp/test-cri"}
3232

0 commit comments

Comments
 (0)