From 7121d251b0f1e5890e22529ab4f28f4ec2ae4140 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Fri, 1 Sep 2017 20:58:06 +0000 Subject: [PATCH] Return image repo digest in container status. Signed-off-by: Lantao Liu --- pkg/server/container_status.go | 16 ++++++++++--- pkg/server/container_status_test.go | 36 +++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pkg/server/container_status.go b/pkg/server/container_status.go index e323c3486..45950ed95 100644 --- a/pkg/server/container_status.go +++ b/pkg/server/container_status.go @@ -32,13 +32,23 @@ 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) } + 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) + } + if len(image.RepoDigests) > 0 { + // Based on the CRI definition, this field will be consumed by user. + imageRef = image.RepoDigests[0] + } + return &runtime.ContainerStatusResponse{ - Status: toCRIContainerStatus(container), + Status: toCRIContainerStatus(container, imageRef), }, nil } // toCRIContainerStatus converts internal container object to CRI container status. -func toCRIContainerStatus(container containerstore.Container) *runtime.ContainerStatus { +func toCRIContainerStatus(container containerstore.Container, imageRef string) *runtime.ContainerStatus { meta := container.Metadata status := container.Status.Get() reason := status.Reason @@ -58,7 +68,7 @@ func toCRIContainerStatus(container containerstore.Container) *runtime.Container FinishedAt: status.FinishedAt, ExitCode: status.ExitCode, Image: meta.Config.GetImage(), - ImageRef: meta.ImageRef, + ImageRef: imageRef, Reason: reason, Message: status.Message, Labels: meta.Config.GetLabels(), diff --git a/pkg/server/container_status_test.go b/pkg/server/container_status_test.go index 8ef334993..7ead96b32 100644 --- a/pkg/server/container_status_test.go +++ b/pkg/server/container_status_test.go @@ -25,9 +25,11 @@ import ( "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container" + imagestore "github.com/kubernetes-incubator/cri-containerd/pkg/store/image" ) -func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Status, *runtime.ContainerStatus) { +func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Status, + *imagestore.Image, *runtime.ContainerStatus) { testID := "test-id" config := &runtime.ContainerConfig{ Metadata: &runtime.ContainerMetadata{ @@ -51,13 +53,17 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta Name: "test-long-name", SandboxID: "test-sandbox-id", Config: config, - ImageRef: "test-image-ref", + ImageRef: "test-image-id", } status := &containerstore.Status{ Pid: 1234, CreatedAt: createdAt, StartedAt: startedAt, } + image := &imagestore.Image{ + ID: "test-image-id", + RepoDigests: []string{"test-image-repo-digest"}, + } expected := &runtime.ContainerStatus{ Id: testID, Metadata: config.GetMetadata(), @@ -65,14 +71,14 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta CreatedAt: createdAt, StartedAt: startedAt, Image: config.GetImage(), - ImageRef: "test-image-ref", + ImageRef: "test-image-repo-digest", Reason: completeExitReason, Labels: config.GetLabels(), Annotations: config.GetAnnotations(), Mounts: config.GetMounts(), } - return metadata, status, expected + return metadata, status, image, expected } func TestToCRIContainerStatus(t *testing.T) { @@ -110,7 +116,7 @@ func TestToCRIContainerStatus(t *testing.T) { expectedReason: errorExitReason, }, } { - metadata, status, expected := getContainerStatusTestData() + metadata, status, image, expected := getContainerStatusTestData() // Update status with test case. status.FinishedAt = test.finishedAt status.ExitCode = test.exitCode @@ -124,13 +130,14 @@ func TestToCRIContainerStatus(t *testing.T) { expected.FinishedAt = test.finishedAt expected.ExitCode = test.exitCode expected.Message = test.message - assert.Equal(t, expected, toCRIContainerStatus(container), desc) + assert.Equal(t, expected, toCRIContainerStatus(container, image.RepoDigests[0]), desc) } } func TestContainerStatus(t *testing.T) { for desc, test := range map[string]struct { exist bool + imageExist bool finishedAt int64 reason string expectedState runtime.ContainerState @@ -138,22 +145,30 @@ func TestContainerStatus(t *testing.T) { }{ "container running": { exist: true, + imageExist: true, expectedState: runtime.ContainerState_CONTAINER_RUNNING, }, "container exited": { exist: true, + imageExist: true, finishedAt: time.Now().UnixNano(), reason: "test-reason", expectedState: runtime.ContainerState_CONTAINER_EXITED, }, "container not exist": { - exist: false, - expectErr: true, + exist: false, + imageExist: true, + expectErr: true, + }, + "image not exist": { + exist: false, + imageExist: false, + expectErr: true, }, } { t.Logf("TestCase %q", desc) c := newTestCRIContainerdService() - metadata, status, expected := getContainerStatusTestData() + metadata, status, image, expected := getContainerStatusTestData() // Update status with test case. status.FinishedAt = test.finishedAt status.Reason = test.reason @@ -162,6 +177,9 @@ func TestContainerStatus(t *testing.T) { if test.exist { assert.NoError(t, c.containerStore.Add(container)) } + if test.imageExist { + c.imageStore.Add(*image) + } resp, err := c.ContainerStatus(context.Background(), &runtime.ContainerStatusRequest{ContainerId: container.ID}) if test.expectErr { assert.Error(t, err)