Skip to content

Commit 1eb20bf

Browse files
committed
Don't read image spec when listing images
Instead embed label with user information in metadata store. Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent b53b783 commit 1eb20bf

5 files changed

Lines changed: 31 additions & 30 deletions

File tree

pkg/cri/server/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const (
9292
imageLabelChainID = criContainerdPrefix + ".image-chain-id"
9393
// imageLabelSize is the label key for image size information.
9494
imageLabelSize = criContainerdPrefix + ".image-size"
95+
// imageLabelUser is the label key to retrieve image spec user.
96+
imageLabelUser = criContainerdPrefix + ".image-spec-user"
9597

9698
// defaultIfName is the default network interface for the pods
9799
defaultIfName = "eth0"
@@ -519,6 +521,16 @@ func (c *criService) ensureImageMetadata(ctx context.Context, name string) error
519521
update = true
520522
}
521523

524+
if _, ok := metadata.Labels[imageLabelUser]; !ok {
525+
spec, err := image.Spec(ctx)
526+
if err != nil {
527+
return fmt.Errorf("failed to read image spec: %w", err)
528+
}
529+
530+
metadata.Labels[imageLabelUser] = spec.Config.User
531+
update = true
532+
}
533+
522534
if update {
523535
metadata.Labels[imageLabelKey] = imageLabelValue
524536

pkg/cri/server/image_list.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222

2323
"github.com/containerd/containerd"
24-
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2524
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2625
)
2726

@@ -37,7 +36,6 @@ func (c *criService) ListImages(ctx context.Context, r *runtime.ListImagesReques
3736
type imageInfo struct {
3837
image containerd.Image
3938
references []string
40-
spec imagespec.Image
4139
}
4240

4341
// Group by image id and gather image references
@@ -51,15 +49,9 @@ func (c *criService) ListImages(ctx context.Context, r *runtime.ListImagesReques
5149
if existing, ok := groups[imageID]; ok {
5250
existing.references = append(existing.references, image.Name())
5351
} else {
54-
spec, err := getImageSpec(ctx, image)
55-
if err != nil {
56-
return nil, err
57-
}
58-
5952
groups[imageID] = &imageInfo{
6053
image: image,
6154
references: []string{image.Name()},
62-
spec: spec,
6355
}
6456
}
6557
}
@@ -68,7 +60,7 @@ func (c *criService) ListImages(ctx context.Context, r *runtime.ListImagesReques
6860
for _, info := range groups {
6961
// TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot
7062
// doesn't exist?
71-
image, err := toCRIImage(info.image, info.references, info.spec)
63+
image, err := toCRIImage(info.image, info.references)
7264
if err != nil {
7365
return nil, err
7466
}

pkg/cri/server/image_list_test.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package server
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/containerd/containerd"
2423
"github.com/containerd/containerd/images"
2524
"github.com/containerd/containerd/metadata"
26-
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2725
"github.com/stretchr/testify/assert"
2826
"github.com/stretchr/testify/require"
2927
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -39,6 +37,7 @@ func TestListImages(t *testing.T) {
3937
imageLabelConfigDigest: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
4038
imageLabelSize: "1000",
4139
imageLabelKey: imageLabelValue,
40+
imageLabelUser: "root",
4241
},
4342
},
4443
{
@@ -48,6 +47,7 @@ func TestListImages(t *testing.T) {
4847
imageLabelConfigDigest: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
4948
imageLabelSize: "1000",
5049
imageLabelKey: imageLabelValue,
50+
imageLabelUser: "root",
5151
},
5252
},
5353
{
@@ -57,6 +57,7 @@ func TestListImages(t *testing.T) {
5757
imageLabelConfigDigest: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
5858
imageLabelSize: "1000",
5959
imageLabelKey: imageLabelValue,
60+
imageLabelUser: "root",
6061
},
6162
},
6263
// Image 2
@@ -67,6 +68,7 @@ func TestListImages(t *testing.T) {
6768
imageLabelConfigDigest: "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
6869
imageLabelSize: "2000",
6970
imageLabelKey: imageLabelValue,
71+
imageLabelUser: "1234:1234",
7072
},
7173
},
7274
{
@@ -76,6 +78,7 @@ func TestListImages(t *testing.T) {
7678
imageLabelConfigDigest: "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
7779
imageLabelSize: "2000",
7880
imageLabelKey: imageLabelValue,
81+
imageLabelUser: "1234:1234",
7982
},
8083
},
8184
{
@@ -85,6 +88,7 @@ func TestListImages(t *testing.T) {
8588
imageLabelConfigDigest: "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
8689
imageLabelSize: "2000",
8790
imageLabelKey: imageLabelValue,
91+
imageLabelUser: "1234:1234",
8892
},
8993
},
9094
// Image 3
@@ -95,6 +99,7 @@ func TestListImages(t *testing.T) {
9599
imageLabelConfigDigest: "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
96100
imageLabelSize: "3000",
97101
imageLabelKey: imageLabelValue,
102+
imageLabelUser: "nobody",
98103
},
99104
},
100105
{
@@ -104,6 +109,7 @@ func TestListImages(t *testing.T) {
104109
imageLabelConfigDigest: "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
105110
imageLabelSize: "3000",
106111
imageLabelKey: imageLabelValue,
112+
imageLabelUser: "nobody",
107113
},
108114
}, {
109115
Name: "gcr.io/library/ubuntu@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
@@ -112,25 +118,11 @@ func TestListImages(t *testing.T) {
112118
imageLabelConfigDigest: "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
113119
imageLabelSize: "3000",
114120
imageLabelKey: imageLabelValue,
121+
imageLabelUser: "nobody",
115122
},
116123
},
117124
}
118125

119-
getImageSpec = func(ctx context.Context, image containerd.Image) (imagespec.Image, error) {
120-
switch image.Name() {
121-
case "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "gcr.io/library/busybox:latest":
122-
return imagespec.Image{Config: imagespec.ImageConfig{User: "root"}}, nil
123-
case "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "gcr.io/library/alpine:latest":
124-
return imagespec.Image{Config: imagespec.ImageConfig{User: "1234:1234"}}, nil
125-
case "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "gcr.io/library/ubuntu:latest":
126-
return imagespec.Image{Config: imagespec.ImageConfig{User: "nobody"}}, nil
127-
default:
128-
t.Fatalf("unexpected OCI spec request for image %q", image.Name())
129-
}
130-
return imagespec.Image{}, nil
131-
}
132-
t.Cleanup(func() { getImageSpec = retrieveImageSpec })
133-
134126
expect := []*runtime.Image{
135127
{
136128
Id: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",

pkg/cri/server/image_status.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ func (c *criService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ
6060
return nil, err
6161
}
6262

63-
runtimeImage, err := toCRIImage(containerdImage, references, imageSpec)
63+
runtimeImage, err := toCRIImage(containerdImage, references)
6464
if err != nil {
6565
return nil, err
6666
}
67+
6768
info, err := c.toCRIImageInfo(ctx, containerdImage.Metadata(), imageSpec, r.GetVerbose())
6869
if err != nil {
6970
return nil, fmt.Errorf("failed to generate image info: %w", err)
@@ -76,7 +77,7 @@ func (c *criService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ
7677
}
7778

7879
// toCRIImage converts internal image object to CRI runtime.Image.
79-
func toCRIImage(image containerd.Image, references []string, imageSpec imagespec.Image) (*runtime.Image, error) {
80+
func toCRIImage(image containerd.Image, references []string) (*runtime.Image, error) {
8081
imageID, err := getImageID(image)
8182
if err != nil {
8283
return nil, err
@@ -85,6 +86,7 @@ func toCRIImage(image containerd.Image, references []string, imageSpec imagespec
8586
var (
8687
labels = image.Labels()
8788
labelSize = labels[imageLabelSize]
89+
labelUser = labels[imageLabelUser]
8890
)
8991

9092
size, err := strconv.ParseUint(labelSize, 10, 64)
@@ -100,7 +102,7 @@ func toCRIImage(image containerd.Image, references []string, imageSpec imagespec
100102
Size_: size,
101103
}
102104

103-
uid, username := getUserFromImage(imageSpec.Config.User)
105+
uid, username := getUserFromImage(labelUser)
104106
if uid != nil {
105107
runtimeImage.Uid = &runtime.Int64Value{Value: *uid}
106108
}

pkg/cri/server/image_status_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestImageStatus(t *testing.T) {
4242
getImageSpec = func(ctx context.Context, image containerd.Image) (imagespec.Image, error) {
4343
switch image.Name() {
4444
case "sha256:d848ce12891bf78792cda4a23c58984033b0c397a55e93a1556202222ecc5ed4":
45-
return imagespec.Image{Config: imagespec.ImageConfig{User: "user:group"}}, nil
45+
return imagespec.Image{}, nil
4646
default:
4747
t.Fatalf("unexpected OCI spec request for image %q", image.Name())
4848
}
@@ -58,6 +58,7 @@ func TestImageStatus(t *testing.T) {
5858
imageLabelConfigDigest: "sha256:d848ce12891bf78792cda4a23c58984033b0c397a55e93a1556202222ecc5ed4",
5959
imageLabelSize: "1234",
6060
imageLabelKey: imageLabelValue,
61+
imageLabelUser: "user:group",
6162
},
6263
},
6364
{
@@ -67,6 +68,7 @@ func TestImageStatus(t *testing.T) {
6768
imageLabelConfigDigest: "sha256:d848ce12891bf78792cda4a23c58984033b0c397a55e93a1556202222ecc5ed4",
6869
imageLabelSize: "1234",
6970
imageLabelKey: imageLabelValue,
71+
imageLabelUser: "user:group",
7072
},
7173
},
7274
{
@@ -76,6 +78,7 @@ func TestImageStatus(t *testing.T) {
7678
imageLabelConfigDigest: "sha256:d848ce12891bf78792cda4a23c58984033b0c397a55e93a1556202222ecc5ed4",
7779
imageLabelSize: "1234",
7880
imageLabelKey: imageLabelValue,
81+
imageLabelUser: "user:group",
7982
},
8083
},
8184
}

0 commit comments

Comments
 (0)