Expand image refs and handle refs with digests

If an image that we're pulling from a registry has a digest in its
reference, use that to construct the destination image's reference.
This should help us detect cases where the image has previously been
pulled.

When we have a filter to use when listing images, expand it into a
reference so that we can properly match against names of images that
we've previously stored using fully expanded references.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2017-04-06 13:28:55 -04:00
parent 3d15bc571b
commit a0b1da15a3
2 changed files with 51 additions and 1 deletions

View file

@ -43,7 +43,19 @@ type ImageServer interface {
func (svc *imageService) ListImages(filter string) ([]ImageResult, error) { func (svc *imageService) ListImages(filter string) ([]ImageResult, error) {
results := []ImageResult{} results := []ImageResult{}
if filter != "" { if filter != "" {
if image, err := svc.store.GetImage(filter); err == nil { ref, err := alltransports.ParseImageName(filter)
if err != nil {
ref2, err2 := istorage.Transport.ParseStoreReference(svc.store, "@"+filter)
if err2 != nil {
ref3, err3 := istorage.Transport.ParseStoreReference(svc.store, filter)
if err3 != nil {
return nil, err
}
ref2 = ref3
}
ref = ref2
}
if image, err := istorage.Transport.GetStoreImage(svc.store, ref); err == nil {
results = append(results, ImageResult{ results = append(results, ImageResult{
ID: image.ID, ID: image.ID,
Names: image.Names, Names: image.Names,
@ -136,6 +148,9 @@ func (svc *imageService) PullImage(systemContext *types.SystemContext, imageName
if tagged, ok := srcRef.DockerReference().(reference.NamedTagged); ok { if tagged, ok := srcRef.DockerReference().(reference.NamedTagged); ok {
dest = dest + ":" + tagged.Tag() dest = dest + ":" + tagged.Tag()
} }
if canonical, ok := srcRef.DockerReference().(reference.Canonical); ok {
dest = dest + "@" + canonical.Digest().String()
}
} }
destRef, err := istorage.Transport.ParseStoreReference(svc.store, dest) destRef, err := istorage.Transport.ParseStoreReference(svc.store, dest)
if err != nil { if err != nil {

View file

@ -32,6 +32,36 @@ function teardown() {
stop_ocid stop_ocid
} }
@test "image pull and list by digest" {
start_ocid "" "" --no-pause-image
run ocic image pull nginx@sha256:4aacdcf186934dcb02f642579314075910f1855590fd3039d8fa4c9f96e48315
echo "$output"
[ "$status" -eq 0 ]
run ocic image list --quiet nginx@sha256:4aacdcf186934dcb02f642579314075910f1855590fd3039d8fa4c9f96e48315
[ "$status" -eq 0 ]
echo "$output"
[ "$output" != "" ]
run ocic image list --quiet nginx@4aacdcf186934dcb02f642579314075910f1855590fd3039d8fa4c9f96e48315
[ "$status" -eq 0 ]
echo "$output"
[ "$output" != "" ]
run ocic image list --quiet @4aacdcf186934dcb02f642579314075910f1855590fd3039d8fa4c9f96e48315
[ "$status" -eq 0 ]
echo "$output"
[ "$output" != "" ]
run ocic image list --quiet 4aacdcf186934dcb02f642579314075910f1855590fd3039d8fa4c9f96e48315
[ "$status" -eq 0 ]
echo "$output"
[ "$output" != "" ]
cleanup_images
stop_ocid
}
@test "image list with filter" { @test "image list with filter" {
start_ocid "" "" --no-pause-image start_ocid "" "" --no-pause-image
run ocic image pull "$IMAGE" run ocic image pull "$IMAGE"
@ -64,6 +94,7 @@ function teardown() {
run ocic image list --quiet run ocic image list --quiet
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" != "" ]
printf '%s\n' "$output" | while IFS= read -r id; do printf '%s\n' "$output" | while IFS= read -r id; do
run ocic image remove --id "$id" run ocic image remove --id "$id"
echo "$output" echo "$output"
@ -72,6 +103,7 @@ function teardown() {
run ocic image list --quiet run ocic image list --quiet
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "" ]
printf '%s\n' "$output" | while IFS= read -r id; do printf '%s\n' "$output" | while IFS= read -r id; do
echo "$id" echo "$id"
status=1 status=1
@ -88,10 +120,12 @@ function teardown() {
run ocic image list --quiet run ocic image list --quiet
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" != "" ]
printf '%s\n' "$output" | while IFS= read -r id; do printf '%s\n' "$output" | while IFS= read -r id; do
run ocic image status --id "$id" run ocic image status --id "$id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" != "" ]
run ocic image remove --id "$id" run ocic image remove --id "$id"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
@ -99,6 +133,7 @@ function teardown() {
run ocic image list --quiet run ocic image list --quiet
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "" ]
printf '%s\n' "$output" | while IFS= read -r id; do printf '%s\n' "$output" | while IFS= read -r id; do
echo "$id" echo "$id"
status=1 status=1