From 050ee1de952be667e9c11754b869c312a438e3df Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Mon, 6 Nov 2017 06:05:49 +0000 Subject: [PATCH] Return image tag as image spec. Signed-off-by: Lantao Liu --- pkg/server/container_status.go | 19 +++++++++++++++---- pkg/server/container_status_test.go | 7 +++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/server/container_status.go b/pkg/server/container_status.go index cb5728465..d1ef13d1c 100644 --- a/pkg/server/container_status.go +++ b/pkg/server/container_status.go @@ -32,10 +32,21 @@ func (c *criContainerdService) ContainerStatus(ctx context.Context, r *runtime.C return nil, fmt.Errorf("an error occurred when try to find container %q: %v", r.GetContainerId(), err) } + // TODO(random-liu): Clean up the following logic in CRI. + // Current assumption: + // * ImageSpec in container config is image ID. + // * ImageSpec in container status is image tag. + // * ImageRef in container status is repo digest. + spec := container.Metadata.Config.GetImage() imageRef := container.ImageRef image, err := c.imageStore.Get(imageRef) if err != nil { - return nil, fmt.Errorf("failed to get image %q: %v", container.ImageRef, err) + return nil, fmt.Errorf("failed to get image %q: %v", imageRef, err) + } + if len(image.RepoTags) > 0 { + // Based on current behavior of dockershim, this field should be + // image tag. + spec = &runtime.ImageSpec{Image: image.RepoTags[0]} } if len(image.RepoDigests) > 0 { // Based on the CRI definition, this field will be consumed by user. @@ -43,12 +54,12 @@ func (c *criContainerdService) ContainerStatus(ctx context.Context, r *runtime.C } return &runtime.ContainerStatusResponse{ - Status: toCRIContainerStatus(container, imageRef), + Status: toCRIContainerStatus(container, spec, imageRef), }, nil } // toCRIContainerStatus converts internal container object to CRI container status. -func toCRIContainerStatus(container containerstore.Container, imageRef string) *runtime.ContainerStatus { +func toCRIContainerStatus(container containerstore.Container, spec *runtime.ImageSpec, imageRef string) *runtime.ContainerStatus { meta := container.Metadata status := container.Status.Get() reason := status.Reason @@ -67,7 +78,7 @@ func toCRIContainerStatus(container containerstore.Container, imageRef string) * StartedAt: status.StartedAt, FinishedAt: status.FinishedAt, ExitCode: status.ExitCode, - Image: meta.Config.GetImage(), + Image: spec, ImageRef: imageRef, Reason: reason, Message: status.Message, diff --git a/pkg/server/container_status_test.go b/pkg/server/container_status_test.go index c25ae2a9a..5f69ce2b3 100644 --- a/pkg/server/container_status_test.go +++ b/pkg/server/container_status_test.go @@ -63,6 +63,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta } image := &imagestore.Image{ ID: "test-image-id", + RepoTags: []string{"test-image-repo-tag"}, RepoDigests: []string{"test-image-repo-digest"}, } expected := &runtime.ContainerStatus{ @@ -71,7 +72,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta State: runtime.ContainerState_CONTAINER_RUNNING, CreatedAt: createdAt, StartedAt: startedAt, - Image: config.GetImage(), + Image: &runtime.ImageSpec{Image: "test-image-repo-tag"}, ImageRef: "test-image-repo-digest", Reason: completeExitReason, Labels: config.GetLabels(), @@ -135,7 +136,9 @@ func TestToCRIContainerStatus(t *testing.T) { expected.FinishedAt = test.finishedAt expected.ExitCode = test.exitCode expected.Message = test.message - assert.Equal(t, expected, toCRIContainerStatus(container, image.RepoDigests[0]), desc) + assert.Equal(t, expected, toCRIContainerStatus(container, + &runtime.ImageSpec{Image: image.RepoTags[0]}, + image.RepoDigests[0]), desc) } }