Skip to content

Commit 8108f0d

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 29b96d9 commit 8108f0d

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

integration/common.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

integration/containerd_image_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package integration
1919
import (
2020
"errors"
2121
"fmt"
22+
goruntime "runtime"
23+
"strings"
2224
"testing"
2325
"time"
2426

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

pull.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import (
3232
"golang.org/x/sync/semaphore"
3333
)
3434

35+
const (
36+
convertedDockerSchema1LabelKey = "io.containerd.image/converted-docker-schema1"
37+
)
38+
3539
// Pull downloads the provided content into containerd's content store
3640
// and returns a platform specific image object
3741
func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Image, retErr error) {
@@ -141,9 +145,10 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
141145
var (
142146
handler images.Handler
143147

144-
isConvertible bool
145-
converterFunc func(context.Context, ocispec.Descriptor) (ocispec.Descriptor, error)
146-
limiter *semaphore.Weighted
148+
isConvertible bool
149+
originalSchema1Digest string
150+
converterFunc func(context.Context, ocispec.Descriptor) (ocispec.Descriptor, error)
151+
limiter *semaphore.Weighted
147152
)
148153

149154
if desc.MediaType == images.MediaTypeDockerSchema1Manifest && rCtx.ConvertSchema1 {
@@ -156,6 +161,8 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
156161
converterFunc = func(ctx context.Context, _ ocispec.Descriptor) (ocispec.Descriptor, error) {
157162
return schema1Converter.Convert(ctx)
158163
}
164+
165+
originalSchema1Digest = desc.Digest.String()
159166
} else {
160167
// Get all the children for a descriptor
161168
childrenHandler := images.ChildrenHandler(store)
@@ -222,6 +229,13 @@ func (c *Client) fetch(ctx context.Context, rCtx *RemoteContext, ref string, lim
222229
}
223230
}
224231

232+
if originalSchema1Digest != "" {
233+
if rCtx.Labels == nil {
234+
rCtx.Labels = make(map[string]string)
235+
}
236+
rCtx.Labels[convertedDockerSchema1LabelKey] = originalSchema1Digest
237+
}
238+
225239
return images.Image{
226240
Name: name,
227241
Target: desc,

0 commit comments

Comments
 (0)