Merge pull request #395 from Random-Liu/fix-image-in-container-status

Return image tag as image spec.
This commit is contained in:
Lantao Liu 2017-11-06 10:55:37 -08:00 committed by GitHub
commit e363c218d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -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) 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 imageRef := container.ImageRef
image, err := c.imageStore.Get(imageRef) image, err := c.imageStore.Get(imageRef)
if err != nil { 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 { if len(image.RepoDigests) > 0 {
// Based on the CRI definition, this field will be consumed by user. // 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{ return &runtime.ContainerStatusResponse{
Status: toCRIContainerStatus(container, imageRef), Status: toCRIContainerStatus(container, spec, imageRef),
}, nil }, nil
} }
// toCRIContainerStatus converts internal container object to CRI container status. // 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 meta := container.Metadata
status := container.Status.Get() status := container.Status.Get()
reason := status.Reason reason := status.Reason
@ -67,7 +78,7 @@ func toCRIContainerStatus(container containerstore.Container, imageRef string) *
StartedAt: status.StartedAt, StartedAt: status.StartedAt,
FinishedAt: status.FinishedAt, FinishedAt: status.FinishedAt,
ExitCode: status.ExitCode, ExitCode: status.ExitCode,
Image: meta.Config.GetImage(), Image: spec,
ImageRef: imageRef, ImageRef: imageRef,
Reason: reason, Reason: reason,
Message: status.Message, Message: status.Message,

View File

@ -63,6 +63,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
} }
image := &imagestore.Image{ image := &imagestore.Image{
ID: "test-image-id", ID: "test-image-id",
RepoTags: []string{"test-image-repo-tag"},
RepoDigests: []string{"test-image-repo-digest"}, RepoDigests: []string{"test-image-repo-digest"},
} }
expected := &runtime.ContainerStatus{ expected := &runtime.ContainerStatus{
@ -71,7 +72,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
State: runtime.ContainerState_CONTAINER_RUNNING, State: runtime.ContainerState_CONTAINER_RUNNING,
CreatedAt: createdAt, CreatedAt: createdAt,
StartedAt: startedAt, StartedAt: startedAt,
Image: config.GetImage(), Image: &runtime.ImageSpec{Image: "test-image-repo-tag"},
ImageRef: "test-image-repo-digest", ImageRef: "test-image-repo-digest",
Reason: completeExitReason, Reason: completeExitReason,
Labels: config.GetLabels(), Labels: config.GetLabels(),
@ -135,7 +136,9 @@ func TestToCRIContainerStatus(t *testing.T) {
expected.FinishedAt = test.finishedAt expected.FinishedAt = test.finishedAt
expected.ExitCode = test.exitCode expected.ExitCode = test.exitCode
expected.Message = test.message 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)
} }
} }