Merge pull request #375 from yanxuean/image-trunc
support get image by truncindex
This commit is contained in:
@@ -30,6 +30,7 @@ import (
|
||||
|
||||
func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Status,
|
||||
*imagestore.Image, *runtime.ContainerStatus) {
|
||||
imageID := "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
testID := "test-id"
|
||||
config := &runtime.ContainerConfig{
|
||||
Metadata: &runtime.ContainerMetadata{
|
||||
@@ -53,7 +54,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
||||
Name: "test-long-name",
|
||||
SandboxID: "test-sandbox-id",
|
||||
Config: config,
|
||||
ImageRef: "test-image-id",
|
||||
ImageRef: imageID,
|
||||
LogPath: "test-log-path",
|
||||
}
|
||||
status := &containerstore.Status{
|
||||
@@ -62,7 +63,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
||||
StartedAt: startedAt,
|
||||
}
|
||||
image := &imagestore.Image{
|
||||
ID: "test-image-id",
|
||||
ID: imageID,
|
||||
RepoTags: []string{"test-image-repo-tag"},
|
||||
RepoDigests: []string{"test-image-repo-digest"},
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/containerd/cgroups"
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/docker/distribution/reference"
|
||||
imagedigest "github.com/opencontainers/go-digest"
|
||||
"github.com/opencontainers/image-spec/identity"
|
||||
@@ -205,34 +204,41 @@ func getRepoDigestAndTag(namedRef reference.Named, digest imagedigest.Digest, sc
|
||||
|
||||
// localResolve resolves image reference locally and returns corresponding image metadata. It returns
|
||||
// nil without error if the reference doesn't exist.
|
||||
func (c *criContainerdService) localResolve(ctx context.Context, ref string) (*imagestore.Image, error) {
|
||||
_, err := imagedigest.Parse(ref)
|
||||
if err != nil {
|
||||
// ref is not image id, try to resolve it locally.
|
||||
normalized, err := util.NormalizeImageRef(ref)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid image reference %q: %v", ref, err)
|
||||
func (c *criContainerdService) localResolve(ctx context.Context, refOrID string) (*imagestore.Image, error) {
|
||||
getImageID := func(refOrId string) string {
|
||||
if _, err := imagedigest.Parse(refOrID); err == nil {
|
||||
return refOrID
|
||||
}
|
||||
image, err := c.client.GetImage(ctx, normalized.String())
|
||||
if err != nil {
|
||||
if errdefs.IsNotFound(err) {
|
||||
return nil, nil
|
||||
|
||||
return func(ref string) string {
|
||||
// ref is not image id, try to resolve it locally.
|
||||
normalized, err := util.NormalizeImageRef(ref)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get image from containerd: %v", err)
|
||||
}
|
||||
desc, err := image.Config(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get image config descriptor: %v", err)
|
||||
}
|
||||
ref = desc.Digest.String()
|
||||
image, err := c.client.GetImage(ctx, normalized.String())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
desc, err := image.Config(ctx)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return desc.Digest.String()
|
||||
}(refOrID)
|
||||
}
|
||||
|
||||
imageID := getImageID(refOrID)
|
||||
if imageID == "" {
|
||||
// Try to treat ref as imageID
|
||||
imageID = refOrID
|
||||
}
|
||||
imageID := ref
|
||||
image, err := c.imageStore.Get(imageID)
|
||||
if err != nil {
|
||||
if err == store.ErrNotExist {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get image %q metadata: %v", imageID, err)
|
||||
return nil, fmt.Errorf("failed to get image %q : %v", imageID, err)
|
||||
}
|
||||
return &image, nil
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestListImages(t *testing.T) {
|
||||
c := newTestCRIContainerdService()
|
||||
imagesInStore := []imagestore.Image{
|
||||
{
|
||||
ID: "test-id-1",
|
||||
ID: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
ChainID: "test-chainid-1",
|
||||
RepoTags: []string{"tag-a-1", "tag-b-1"},
|
||||
RepoDigests: []string{"digest-a-1", "digest-b-1"},
|
||||
@@ -42,7 +42,7 @@ func TestListImages(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "test-id-2",
|
||||
ID: "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
ChainID: "test-chainid-2",
|
||||
RepoTags: []string{"tag-a-2", "tag-b-2"},
|
||||
RepoDigests: []string{"digest-a-2", "digest-b-2"},
|
||||
@@ -52,7 +52,7 @@ func TestListImages(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "test-id-3",
|
||||
ID: "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
ChainID: "test-chainid-3",
|
||||
RepoTags: []string{"tag-a-3", "tag-b-3"},
|
||||
RepoDigests: []string{"digest-a-3", "digest-b-3"},
|
||||
@@ -64,21 +64,21 @@ func TestListImages(t *testing.T) {
|
||||
}
|
||||
expect := []*runtime.Image{
|
||||
{
|
||||
Id: "test-id-1",
|
||||
Id: "sha256:1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
RepoTags: []string{"tag-a-1", "tag-b-1"},
|
||||
RepoDigests: []string{"digest-a-1", "digest-b-1"},
|
||||
Size_: uint64(1000),
|
||||
Username: "root",
|
||||
},
|
||||
{
|
||||
Id: "test-id-2",
|
||||
Id: "sha256:2123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
RepoTags: []string{"tag-a-2", "tag-b-2"},
|
||||
RepoDigests: []string{"digest-a-2", "digest-b-2"},
|
||||
Size_: uint64(2000),
|
||||
Uid: &runtime.Int64Value{Value: 1234},
|
||||
},
|
||||
{
|
||||
Id: "test-id-3",
|
||||
Id: "sha256:3123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
RepoTags: []string{"tag-a-3", "tag-b-3"},
|
||||
RepoDigests: []string{"digest-a-3", "digest-b-3"},
|
||||
Size_: uint64(3000),
|
||||
|
||||
Reference in New Issue
Block a user