Skip to content

Commit cac1bab

Browse files
committed
Add a new image label if it is docker schema 1
Signed-off-by: Qiutong Song <[email protected]> (cherry picked from commit 7712375) Signed-off-by: Qiutong Song <[email protected]>
1 parent 9211aff commit cac1bab

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

integration/containerd_image_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
goruntime "runtime"
24+
"strings"
2325
"testing"
2426
"time"
2527

@@ -225,3 +227,41 @@ func TestContainerdSandboxImage(t *testing.T) {
225227
t.Log("verify pinned field is set for pause image")
226228
assert.True(t, pimg.Pinned)
227229
}
230+
231+
func TestContainerdImageWithDockerSchema1(t *testing.T) {
232+
if goruntime.GOOS == "windows" {
233+
t.Skip("Skipped on Windows because the test image is not a multi-platform one.")
234+
}
235+
236+
var testImage = images.Get(images.DockerSchema1)
237+
digest := strings.Split(testImage, "@")[1]
238+
ctx := context.Background()
239+
240+
t.Logf("make sure the test image doesn't exist in the cri plugin")
241+
i, err := imageService.ImageStatus(&runtime.ImageSpec{Image: testImage})
242+
require.NoError(t, err)
243+
if i != nil {
244+
require.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: testImage}))
245+
}
246+
247+
t.Logf("pull the image into containerd")
248+
//nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
249+
_, err = containerdClient.Pull(ctx, testImage, containerd.WithPullUnpack, containerd.WithSchema1Conversion)
250+
require.NoError(t, err)
251+
defer func() {
252+
// Make sure the image is cleaned up in any case.
253+
if err := containerdClient.ImageService().Delete(ctx, testImage); err != nil {
254+
assert.True(t, errdefs.IsNotFound(err), err)
255+
}
256+
assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: testImage}))
257+
}()
258+
259+
imgByRef, err := containerdClient.GetImage(ctx, testImage)
260+
require.NoError(t, err)
261+
262+
t.Logf("the image should be marked as managed")
263+
assert.Equal(t, "managed", imgByRef.Labels()["io.cri-containerd.image"])
264+
265+
t.Logf("the image should be marked as dokcker schema1 with its original digest")
266+
assert.Equal(t, digest, imgByRef.Labels()["io.containerd.image/converted-docker-schema1"])
267+
}

integration/images/image_list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ImageList struct {
3737
VolumeCopyUp string
3838
VolumeOwnership string
3939
ArgsEscaped string
40+
DockerSchema1 string
4041
}
4142

4243
var (
@@ -55,6 +56,7 @@ func initImages(imageListFile string) {
5556
VolumeCopyUp: "ghcr.io/containerd/volume-copy-up:2.1",
5657
VolumeOwnership: "ghcr.io/containerd/volume-ownership:2.1",
5758
ArgsEscaped: "cplatpublic.azurecr.io/args-escaped-test-image-ns:1.0",
59+
DockerSchema1: "registry.k8s.io/busybox@sha256:4bdd623e848417d96127e16037743f0cd8b528c026e9175e22a84f639eca58ff",
5860
}
5961

6062
if imageListFile != "" {
@@ -92,6 +94,8 @@ const (
9294
VolumeOwnership
9395
// Test image for ArgsEscaped windows bug
9496
ArgsEscaped
97+
// DockerSchema1 image with docker schema 1
98+
DockerSchema1
9599
)
96100

97101
func initImageMap(imageList ImageList) map[int]string {
@@ -103,6 +107,7 @@ func initImageMap(imageList ImageList) map[int]string {
103107
images[VolumeCopyUp] = imageList.VolumeCopyUp
104108
images[VolumeOwnership] = imageList.VolumeOwnership
105109
images[ArgsEscaped] = imageList.ArgsEscaped
110+
images[DockerSchema1] = imageList.DockerSchema1
106111
return images
107112
}
108113

pull.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import (
3434
)
3535

3636
const (
37-
pullSpanPrefix = "pull"
37+
pullSpanPrefix = "pull"
38+
convertedDockerSchema1LabelKey = "io.containerd.image/converted-docker-schema1"
3839
)
3940

4041
// Pull downloads the provided content into containerd's content store
@@ -189,9 +190,10 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
189190
var (
190191
handler images.Handler
191192

192-
isConvertible bool
193-
converterFunc func(context.Context, ocispec.Descriptor) (ocispec.Descriptor, error)
194-
limiter *semaphore.Weighted
193+
isConvertible bool
194+
originalSchema1Digest string
195+
converterFunc func(context.Context, ocispec.Descriptor) (ocispec.Descriptor, error)
196+
limiter *semaphore.Weighted
195197
)
196198

197199
if desc.MediaType == images.MediaTypeDockerSchema1Manifest && rCtx.ConvertSchema1 {
@@ -204,6 +206,8 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
204206
converterFunc = func(ctx context.Context, _ ocispec.Descriptor) (ocispec.Descriptor, error) {
205207
return schema1Converter.Convert(ctx)
206208
}
209+
210+
originalSchema1Digest = desc.Digest.String()
207211
} else {
208212
// Get all the children for a descriptor
209213
childrenHandler := images.ChildrenHandler(store)
@@ -270,6 +274,13 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
270274
}
271275
}
272276

277+
if originalSchema1Digest != "" {
278+
if rCtx.Labels == nil {
279+
rCtx.Labels = make(map[string]string)
280+
}
281+
rCtx.Labels[convertedDockerSchema1LabelKey] = originalSchema1Digest
282+
}
283+
273284
return images.Image{
274285
Name: name,
275286
Target: desc,

0 commit comments

Comments
 (0)