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/container/container.go b/pkg/cri/store/container/container.go index 13c3da209..b89464375 100644 --- a/pkg/cri/store/container/container.go +++ b/pkg/cri/store/container/container.go @@ -25,7 +25,7 @@ import ( "github.com/containerd/containerd/pkg/cri/store" "github.com/containerd/containerd/pkg/cri/store/label" "github.com/containerd/containerd/pkg/cri/store/stats" - "github.com/containerd/containerd/pkg/cri/store/truncindex" + "github.com/containerd/containerd/pkg/truncindex" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" ) diff --git a/pkg/cri/store/errors.go b/pkg/cri/store/errors.go deleted file mode 100644 index d8398e45f..000000000 --- a/pkg/cri/store/errors.go +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package store - -import "github.com/containerd/containerd/errdefs" - -var ( - // ErrAlreadyExist is the error returned when data added in the store - // already exists. - // - // This error has been DEPRECATED and will be removed in 1.5. Please switch - // usage directly to `errdefs.ErrAlreadyExists`. - ErrAlreadyExist = errdefs.ErrAlreadyExists - // ErrNotExist is the error returned when data is not in the store. - // - // This error has been DEPRECATED and will be removed in 1.5. Please switch - // usage directly to `errdefs.ErrNotFound`. - ErrNotExist = errdefs.ErrNotFound -) diff --git a/pkg/cri/store/errors_test.go b/pkg/cri/store/errors_test.go deleted file mode 100644 index 11b38bfd7..000000000 --- a/pkg/cri/store/errors_test.go +++ /dev/null @@ -1,48 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package store - -import ( - "testing" - - "github.com/containerd/containerd/errdefs" - - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" -) - -func TestStoreErrAlreadyExistGRPCStatus(t *testing.T) { - err := errdefs.ToGRPC(errdefs.ErrAlreadyExists) - s, ok := status.FromError(err) - if !ok { - t.Fatalf("failed to convert err: %v to status: %d", err, codes.AlreadyExists) - } - if s.Code() != codes.AlreadyExists { - t.Fatalf("expected code: %d got: %d", codes.AlreadyExists, s.Code()) - } -} - -func TestStoreErrNotExistGRPCStatus(t *testing.T) { - err := errdefs.ToGRPC(errdefs.ErrNotFound) - s, ok := status.FromError(err) - if !ok { - t.Fatalf("failed to convert err: %v to status: %d", err, codes.NotFound) - } - if s.Code() != codes.NotFound { - t.Fatalf("expected code: %d got: %d", codes.NotFound, s.Code()) - } -} diff --git a/pkg/cri/store/image/image.go b/pkg/cri/store/image/image.go index f9c057086..7d71d3741 100644 --- a/pkg/cri/store/image/image.go +++ b/pkg/cri/store/image/image.go @@ -18,14 +18,13 @@ 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" + "github.com/containerd/containerd/reference" imagedigest "github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest/digestset" @@ -132,15 +131,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 get OCI image spec: %w", err) } return &Image{ @@ -148,7 +144,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 } @@ -210,7 +206,7 @@ func (s *store) add(img Image) error { return nil } // Or else, merge and sort the references. - i.References = sortReferences(util.MergeStringSlices(i.References, img.References)) + i.References = reference.Sort(util.MergeStringSlices(i.References, img.References)) s.images[img.ID] = i return nil } diff --git a/pkg/cri/store/sandbox/sandbox.go b/pkg/cri/store/sandbox/sandbox.go index 854372afb..6683f8737 100644 --- a/pkg/cri/store/sandbox/sandbox.go +++ b/pkg/cri/store/sandbox/sandbox.go @@ -24,8 +24,8 @@ import ( "github.com/containerd/containerd/pkg/cri/store" "github.com/containerd/containerd/pkg/cri/store/label" "github.com/containerd/containerd/pkg/cri/store/stats" - "github.com/containerd/containerd/pkg/cri/store/truncindex" "github.com/containerd/containerd/pkg/netns" + "github.com/containerd/containerd/pkg/truncindex" ) // Sandbox contains all resources associated with the sandbox. All methods to diff --git a/pkg/cri/store/truncindex/truncindex.go b/pkg/truncindex/truncindex.go similarity index 100% rename from pkg/cri/store/truncindex/truncindex.go rename to pkg/truncindex/truncindex.go diff --git a/pkg/cri/store/image/sort.go b/reference/sort.go similarity index 92% rename from pkg/cri/store/image/sort.go rename to reference/sort.go index 403d7d220..38c3d0672 100644 --- a/pkg/cri/store/image/sort.go +++ b/reference/sort.go @@ -14,7 +14,7 @@ limitations under the License. */ -package image +package reference import ( "sort" @@ -22,8 +22,8 @@ import ( "github.com/containerd/containerd/reference/docker" ) -// sortReferences sorts references by refRank then string comparison -func sortReferences(references []string) []string { +// Sort sorts references by refRank then string comparison +func Sort(references []string) []string { var prefs []docker.Reference var bad []string diff --git a/pkg/cri/store/image/sort_test.go b/reference/sort_test.go similarity index 97% rename from pkg/cri/store/image/sort_test.go rename to reference/sort_test.go index 00731ec53..f1e68bb95 100644 --- a/pkg/cri/store/image/sort_test.go +++ b/reference/sort_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package image +package reference import ( "io" @@ -68,7 +68,7 @@ func TestReferenceSorting(t *testing.T) { expected: []string{r2("name-1", "tag"), r2("name-2", "tag"), r2("name-3", "tag")}, }, } { - sorted := sortReferences(tc.unsorted) + sorted := Sort(tc.unsorted) if len(sorted) != len(tc.expected) { t.Errorf("[%d]: Mismatched sized, got %d, expected %d", i, len(sorted), len(tc.expected)) continue