From 2e7a04fd4a90ec0ac2535dbd444fcd542b872712 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Sat, 20 May 2017 15:16:58 +0200 Subject: [PATCH] server: container_status: return image name if available If we create a container using the image ID like 771cd5947d5ea4bf8e8f4900dd357dbb67e7b16486c270f8274087d182d457c6, then a call to container_status will return that same ID for the "Image" field in ContainerStatusResponse. This patch matches dockershim behavior and return the first tagged name if available from the image store. This is also needed to fix a failure in k8s e2d tests. Reference: https://github.com/kubernetes/kubernetes/pull/39298/files#diff-c7dd39479fd733354254e70845075db5R369 Reference: https://github.com/kubernetes/kubernetes/blob/67a5bf8454bb839ee80a4245e9e09fc8f700fa84/test/e2e/framework/util.go#L1941 Signed-off-by: Antonio Murdaca --- cmd/crioctl/container.go | 3 +++ server/container_status.go | 21 ++++++++++++++++++--- test/image.bats | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cmd/crioctl/container.go b/cmd/crioctl/container.go index 693804d0..20ba367b 100644 --- a/cmd/crioctl/container.go +++ b/cmd/crioctl/container.go @@ -460,6 +460,9 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID string) error { ftm := time.Unix(0, r.Status.FinishedAt) fmt.Printf("Finished: %v\n", ftm) fmt.Printf("Exit Code: %v\n", r.Status.ExitCode) + if r.Status.Image != nil { + fmt.Printf("Image: %v\n", r.Status.Image.Image) + } return nil } diff --git a/server/container_status.go b/server/container_status.go index febe7060..5e7b2b22 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -2,8 +2,10 @@ package server import ( "encoding/json" + "fmt" "github.com/Sirupsen/logrus" + "github.com/docker/distribution/reference" "github.com/kubernetes-incubator/cri-o/oci" rspec "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" @@ -24,14 +26,12 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq s.containerStateToDisk(c) containerID := c.ID() - image := c.Image() resp := &pb.ContainerStatusResponse{ Status: &pb.ContainerStatus{ Id: containerID, Metadata: c.Metadata(), Labels: c.Labels(), Annotations: c.Annotations(), - Image: image, }, } @@ -41,13 +41,28 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq } resp.Status.Mounts = mounts - status, err := s.storageImageServer.ImageStatus(s.imageContext, image.Image) + imageName := c.Image().Image + status, err := s.storageImageServer.ImageStatus(s.imageContext, imageName) if err != nil { return nil, err } + // TODO: use status.ID only if no digested names!!! + // need to modify ImageStatus to split tagged and digested! resp.Status.ImageRef = status.ID + for _, n := range status.Names { + r, err := reference.ParseNormalizedNamed(n) + if err != nil { + return nil, fmt.Errorf("failed to normalize image name for Image: %v", err) + } + if tagged, isTagged := r.(reference.Tagged); isTagged { + imageName = reference.FamiliarString(tagged) + break + } + } + resp.Status.Image = &pb.ImageSpec{Image: imageName} + cState := s.runtime.ContainerStatus(c) rStatus := pb.ContainerState_CONTAINER_UNKNOWN diff --git a/test/image.bats b/test/image.bats index ebad58fc..3dc50416 100644 --- a/test/image.bats +++ b/test/image.bats @@ -23,6 +23,31 @@ function teardown() { stop_crio } +@test "container status return image:tag if created by image ID" { + start_crio + + run crioctl pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + + sed -e "s/%VALUE%/$REDIS_IMAGEID/g" "$TESTDATA"/container_config_by_imageid.json > "$TESTDIR"/ctr_by_imageid.json + + run crioctl ctr create --config "$TESTDIR"/ctr_by_imageid.json --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + ctr_id="$output" + + run crioctl ctr status --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" =~ "Image: redis:alpine" ]] + + cleanup_ctrs + cleanup_pods + stop_crio +} + @test "image pull" { start_crio "" "" --no-pause-image run crioctl image pull "$IMAGE"