Skip to content

Commit

Permalink
Merge pull request #1630 from vrothberg/oci-ref
Browse files Browse the repository at this point in the history
libimage: preserve optional name when pulling from OCI transport
  • Loading branch information
openshift-merge-robot authored Aug 30, 2023
2 parents e17fa2f + 08fc0b4 commit 1240538
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
14 changes: 12 additions & 2 deletions libimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,18 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference,

case ociTransport.Transport.Name():
split := strings.SplitN(ref.StringWithinTransport(), ":", 2)
storageName = toLocalImageName(split[0])
imageName = storageName
if len(split) == 1 || split[1] == "" {
// Same trick as for the dir transport: we cannot use
// the path to a directory as the name.
storageName, err = getImageID(ctx, ref, nil)
if err != nil {
return nil, err
}
imageName = "sha256:" + storageName[1:]
} else { // If the OCI-reference includes an image reference, use it
storageName = split[1]
imageName = storageName
}

case ociArchiveTransport.Transport.Name():
manifestDescriptor, err := ociArchiveTransport.LoadManifestDescriptorWithContext(r.SystemContext(), ref)
Expand Down
49 changes: 49 additions & 0 deletions libimage/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,52 @@ func TestShortNameAndIDconflict(t *testing.T) {
require.NoError(t, err)
require.Equal(t, busybox[0].ID(), img.ID())
}

func TestPullOCINoReference(t *testing.T) {
// Exercise pulling from the OCI transport and make sure that a
// specified reference is preserved in the image name.

busybox := "docker.io/library/busybox:latest"
runtime, cleanup := testNewRuntime(t)
defer cleanup()
ctx := context.Background()
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout

images, err := runtime.Pull(ctx, busybox, config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, images, 1)

// Push one image without the optional reference
ociPathNoRef := "oci:" + t.TempDir() + "noRef"
_, err = runtime.Push(ctx, busybox, ociPathNoRef, nil)
require.NoError(t, err)

// Push another image _with_ the optional reference which allows for
// preserving the name.
ociPathWithRef := "oci:" + t.TempDir() + "withRef:" + busybox
_, err = runtime.Push(ctx, busybox, ociPathWithRef, nil)
require.NoError(t, err)

_, errors := runtime.RemoveImages(ctx, []string{busybox}, nil)
require.Nil(t, errors)

images, err = runtime.Pull(ctx, ociPathNoRef, config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, images, 1)

exists, err := runtime.Exists(busybox) // busybox does not exist
require.NoError(t, err)
require.False(t, exists)

names := images[0].Names() // The image has no names (i.e., <none>)
require.Nil(t, names)

images, err = runtime.Pull(ctx, ociPathWithRef, config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, images, 1)

exists, err = runtime.Exists(busybox) // busybox does exist now
require.NoError(t, err)
require.True(t, exists)
}

0 comments on commit 1240538

Please sign in to comment.