From b5366f8d7e0c3f902c2f5c939dabe79bbf49153a Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 26 May 2022 12:38:55 -0700 Subject: [PATCH] CRI: Retrieve image spec on client Signed-off-by: Maksym Pavlenko --- image.go | 22 ++++++++++++++++++++++ pkg/cri/store/image/image.go | 13 ++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/image.go b/image.go index 784df5dd9..392b9e7a7 100644 --- a/image.go +++ b/image.go @@ -64,6 +64,8 @@ type Image interface { Metadata() images.Image // Platform returns the platform match comparer. Can be nil. Platform() platforms.MatchComparer + // Spec returns the OCI image spec for a given image. + Spec(ctx context.Context) (ocispec.Image, error) } type usageOptions struct { @@ -279,6 +281,26 @@ func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, e return false, nil } +func (i *image) Spec(ctx context.Context) (ocispec.Image, error) { + var ociImage ocispec.Image + + desc, err := i.Config(ctx) + if err != nil { + return ociImage, fmt.Errorf("get image config descriptor: %w", err) + } + + blob, err := content.ReadBlob(ctx, i.ContentStore(), desc) + if err != nil { + return ociImage, fmt.Errorf("read image config from content store: %w", err) + } + + if err := json.Unmarshal(blob, &ociImage); err != nil { + return ociImage, fmt.Errorf("unmarshal image config %s: %w", blob, err) + } + + return ociImage, nil +} + // UnpackConfig provides configuration for the unpack of an image type UnpackConfig struct { // ApplyOpts for applying a diff to a snapshotter diff --git a/pkg/cri/store/image/image.go b/pkg/cri/store/image/image.go index f9c057086..5cd7bb2ed 100644 --- a/pkg/cri/store/image/image.go +++ b/pkg/cri/store/image/image.go @@ -18,12 +18,10 @@ package image import ( "context" - "encoding/json" "fmt" "sync" "github.com/containerd/containerd" - "github.com/containerd/containerd/content" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/pkg/cri/util" @@ -132,15 +130,12 @@ func getImage(ctx context.Context, i containerd.Image) (*Image, error) { if err != nil { return nil, fmt.Errorf("get image config descriptor: %w", err) } + id := desc.Digest.String() - rb, err := content.ReadBlob(ctx, i.ContentStore(), desc) + spec, err := i.Spec(ctx) if err != nil { - return nil, fmt.Errorf("read image config from content store: %w", err) - } - var ociimage imagespec.Image - if err := json.Unmarshal(rb, &ociimage); err != nil { - return nil, fmt.Errorf("unmarshal image config %s: %w", rb, err) + return nil, fmt.Errorf("failed to read get image spec: %w", err) } return &Image{ @@ -148,7 +143,7 @@ func getImage(ctx context.Context, i containerd.Image) (*Image, error) { References: []string{i.Name()}, ChainID: chainID.String(), Size: size, - ImageSpec: ociimage, + ImageSpec: spec, }, nil }