diff --git a/cmd/crioctl/container.go b/cmd/crioctl/container.go index 567807f6..f1a6d7fd 100644 --- a/cmd/crioctl/container.go +++ b/cmd/crioctl/container.go @@ -469,10 +469,7 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID string) error { if r.Status.Image != nil { fmt.Printf("Image: %v\n", r.Status.Image.Image) } - // - // TODO: https://github.com/kubernetes-incubator/cri-o/issues/531 - // - //fmt.Printf("ImageRef: %v\n", r.Status.ImageRef) + fmt.Printf("ImageRef: %v\n", r.Status.ImageRef) return nil } diff --git a/pkg/storage/image.go b/pkg/storage/image.go index 5a52d81c..0f0a728a 100644 --- a/pkg/storage/image.go +++ b/pkg/storage/image.go @@ -14,14 +14,16 @@ import ( "github.com/containers/image/transports/alltransports" "github.com/containers/image/types" "github.com/containers/storage" + digest "github.com/opencontainers/go-digest" ) // ImageResult wraps a subset of information about an image: its ID, its names, // and the size, if known, or nil if it isn't. type ImageResult struct { - ID string - Names []string - Size *uint64 + ID string + Names []string + Digest string + Size *uint64 } type indexInfo struct { @@ -118,13 +120,21 @@ func (svc *imageService) ImageStatus(systemContext *types.SystemContext, nameOrI return nil, err } size := imageSize(img) + manifest, _, _ := img.Manifest() img.Close() - return &ImageResult{ - ID: image.ID, - Names: image.Names, - Size: size, - }, nil + manifestDigest := "" + if len(manifest) > 0 { + manifestDigest = digest.Canonical.FromBytes(manifest).String() + } + + result := ImageResult{ + ID: image.ID, + Names: image.Names, + Digest: manifestDigest, + Size: size, + } + return &result, nil } func imageSize(img types.Image) *uint64 { diff --git a/server/container_status.go b/server/container_status.go index a59d9630..971d5f68 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -6,6 +6,7 @@ import ( "github.com/docker/distribution/reference" "github.com/kubernetes-incubator/cri-o/oci" + digest "github.com/opencontainers/go-digest" rspec "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" "golang.org/x/net/context" @@ -53,32 +54,29 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq return nil, err } - imageRef := status.ID - // - // TODO: https://github.com/kubernetes-incubator/cri-o/issues/531 - // - //for _, n := range status.Names { - //r, err := reference.ParseNormalizedNamed(n) - //if err != nil { - //return nil, fmt.Errorf("failed to normalize image name for ImageRef: %v", err) - //} - //if digested, isDigested := r.(reference.Canonical); isDigested { - //imageRef = reference.FamiliarString(digested) - //break - //} - //} - resp.Status.ImageRef = imageRef + imageRef := "" 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) + if namedtagged, isNamedTagged := r.(reference.NamedTagged); isNamedTagged { + imageName = reference.FamiliarString(namedtagged) + if status.Digest != "" { + if canonical, err2 := reference.WithDigest(reference.TrimNamed(r), digest.Digest(status.Digest)); err2 == nil { + imageRef = reference.FamiliarString(canonical) + } + } + break + } + if canonical, isCanonical := r.(reference.Canonical); isCanonical { + imageName = reference.FamiliarString(canonical) + imageRef = reference.FamiliarString(canonical) break } } + resp.Status.ImageRef = imageRef resp.Status.Image = &pb.ImageSpec{Image: imageName} cState := s.Runtime().ContainerStatus(c) diff --git a/server/image_status.go b/server/image_status.go index f7e7c4f3..a90a44b8 100644 --- a/server/image_status.go +++ b/server/image_status.go @@ -42,10 +42,10 @@ func (s *Server) ImageStatus(ctx context.Context, req *pb.ImageStatusRequest) (* } resp := &pb.ImageStatusResponse{ Image: &pb.Image{ - Id: status.ID, - RepoTags: status.Names, - Size_: *status.Size, - // TODO: https://github.com/kubernetes-incubator/cri-o/issues/531 + Id: status.ID, + RepoTags: status.Names, + RepoDigests: []string{status.Digest}, + Size_: *status.Size, }, } logrus.Debugf("ImageStatusResponse: %+v", resp) diff --git a/test/helpers.bash b/test/helpers.bash index 108c8d12..48f024cc 100644 --- a/test/helpers.bash +++ b/test/helpers.bash @@ -83,7 +83,7 @@ cp "$CONMON_BINARY" "$TESTDIR/conmon" PATH=$PATH:$TESTDIR -# Make sure we have a copy of the redis:latest image. +# Make sure we have a copy of the redis:alpine image. if ! [ -d "$ARTIFACTS_PATH"/redis-image ]; then mkdir -p "$ARTIFACTS_PATH"/redis-image if ! "$COPYIMG_BINARY" --import-from=docker://redis:alpine --export-to=dir:"$ARTIFACTS_PATH"/redis-image --signature-policy="$INTEGRATION_ROOT"/policy.json ; then @@ -93,10 +93,7 @@ if ! [ -d "$ARTIFACTS_PATH"/redis-image ]; then fi fi -# TODO: remove the code below for redis digested image id when -# https://github.com/kubernetes-incubator/cri-o/issues/531 is complete -# as the digested reference will be auto-stored when pulling the tag -# above +# Make sure we have a copy of the redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b image. if ! [ -d "$ARTIFACTS_PATH"/redis-image-digest ]; then mkdir -p "$ARTIFACTS_PATH"/redis-image-digest if ! "$COPYIMG_BINARY" --import-from=docker://redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b --export-to=dir:"$ARTIFACTS_PATH"/redis-image-digest --signature-policy="$INTEGRATION_ROOT"/policy.json ; then @@ -206,11 +203,7 @@ function start_crio() { "$BIN2IMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --source-binary "$PAUSE_BINARY" fi "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=redis:alpine --import-from=dir:"$ARTIFACTS_PATH"/redis-image --add-name=docker.io/library/redis:alpine --signature-policy="$INTEGRATION_ROOT"/policy.json -# TODO: remove the code below for redis:alpine digested image id when -# https://github.com/kubernetes-incubator/cri-o/issues/531 is complete -# as the digested reference will be auto-stored when pulling the tag -# above - "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b --import-from=dir:"$ARTIFACTS_PATH"/redis-image-digest --add-name=docker.io/library/redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b --signature-policy="$INTEGRATION_ROOT"/policy.json + "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b --import-from=dir:"$ARTIFACTS_PATH"/redis-image-digest --signature-policy="$INTEGRATION_ROOT"/policy.json "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=mrunalp/oom --import-from=dir:"$ARTIFACTS_PATH"/oom-image --add-name=docker.io/library/mrunalp/oom --signature-policy="$INTEGRATION_ROOT"/policy.json "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=mrunalp/image-volume-test --import-from=dir:"$ARTIFACTS_PATH"/image-volume-test-image --add-name=docker.io/library/mrunalp/image-volume-test --signature-policy="$INTEGRATION_ROOT"/policy.json "$COPYIMG_BINARY" --root "$TESTDIR/crio" $STORAGE_OPTS --runroot "$TESTDIR/crio-run" --image-name=busybox:latest --import-from=dir:"$ARTIFACTS_PATH"/busybox-image --add-name=docker.io/library/busybox:latest --signature-policy="$INTEGRATION_ROOT"/policy.json @@ -233,28 +226,16 @@ function start_crio() { crioctl image pull redis:alpine fi REDIS_IMAGEID=$(crioctl image status --id=redis:alpine | head -1 | sed -e "s/ID: //g") + run crioctl image status --id=redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b + if [ "$status" -ne 0 ] ; then + crioctl image pull redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b + fi + REDIS_IMAGEID_DIGESTED=$(crioctl image status --id=redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b | head -1 | sed -e "s/ID: //g") run crioctl image status --id=mrunalp/oom if [ "$status" -ne 0 ] ; then crioctl image pull mrunalp/oom fi - # - # - # - # TODO: remove the code below for redis digested image id when - # https://github.com/kubernetes-incubator/cri-o/issues/531 is complete - # as the digested reference will be auto-stored when pulling the tag - # above - # - # - # - REDIS_IMAGEID_DIGESTED="redis@sha256:03789f402b2ecfb98184bf128d180f398f81c63364948ff1454583b02442f73b" - run crioctl image status --id $REDIS_IMAGEID_DIGESTED - if [ "$status" -ne 0 ]; then - crioctl image pull $REDIS_IMAGEID_DIGESTED - fi - # - # - # + OOM_IMAGEID=$(crioctl image status --id=mrunalp/oom | head -1 | sed -e "s/ID: //g") run crioctl image status --id=runcom/stderr-test if [ "$status" -ne 0 ] ; then crioctl image pull runcom/stderr-test:latest diff --git a/test/image.bats b/test/image.bats index 8ccceefb..0f6130a8 100644 --- a/test/image.bats +++ b/test/image.bats @@ -49,8 +49,6 @@ function teardown() { } @test "container status return image@digest if created by image ID and digest available" { - skip "depends on https://github.com/kubernetes-incubator/cri-o/issues/531" - start_crio run crioctl pod run --config "$TESTDATA"/sandbox_config.json @@ -94,18 +92,14 @@ function teardown() { [ "$status" -eq 0 ] echo "$output" [ "$output" != "" ] + imageid="$output" - run crioctl image list --quiet nginx@33eb1ed1e802d4f71e52421f56af028cdf12bb3bfff5affeaf5bf0e328ffa1bc + run crioctl image list --quiet @"$imageid" [ "$status" -eq 0 ] echo "$output" [ "$output" != "" ] - run crioctl image list --quiet @33eb1ed1e802d4f71e52421f56af028cdf12bb3bfff5affeaf5bf0e328ffa1bc - [ "$status" -eq 0 ] - echo "$output" - [ "$output" != "" ] - - run crioctl image list --quiet 33eb1ed1e802d4f71e52421f56af028cdf12bb3bfff5affeaf5bf0e328ffa1bc + run crioctl image list --quiet "$imageid" [ "$status" -eq 0 ] echo "$output" [ "$output" != "" ]