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"