Skip to content

Commit 1240538

Browse files
Merge pull request #1630 from vrothberg/oci-ref
libimage: preserve optional name when pulling from OCI transport
2 parents e17fa2f + 08fc0b4 commit 1240538

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

libimage/pull.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,18 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference,
230230

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

236246
case ociArchiveTransport.Transport.Name():
237247
manifestDescriptor, err := ociArchiveTransport.LoadManifestDescriptorWithContext(r.SystemContext(), ref)

libimage/pull_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,52 @@ func TestShortNameAndIDconflict(t *testing.T) {
220220
require.NoError(t, err)
221221
require.Equal(t, busybox[0].ID(), img.ID())
222222
}
223+
224+
func TestPullOCINoReference(t *testing.T) {
225+
// Exercise pulling from the OCI transport and make sure that a
226+
// specified reference is preserved in the image name.
227+
228+
busybox := "docker.io/library/busybox:latest"
229+
runtime, cleanup := testNewRuntime(t)
230+
defer cleanup()
231+
ctx := context.Background()
232+
pullOptions := &PullOptions{}
233+
pullOptions.Writer = os.Stdout
234+
235+
images, err := runtime.Pull(ctx, busybox, config.PullPolicyAlways, pullOptions)
236+
require.NoError(t, err)
237+
require.Len(t, images, 1)
238+
239+
// Push one image without the optional reference
240+
ociPathNoRef := "oci:" + t.TempDir() + "noRef"
241+
_, err = runtime.Push(ctx, busybox, ociPathNoRef, nil)
242+
require.NoError(t, err)
243+
244+
// Push another image _with_ the optional reference which allows for
245+
// preserving the name.
246+
ociPathWithRef := "oci:" + t.TempDir() + "withRef:" + busybox
247+
_, err = runtime.Push(ctx, busybox, ociPathWithRef, nil)
248+
require.NoError(t, err)
249+
250+
_, errors := runtime.RemoveImages(ctx, []string{busybox}, nil)
251+
require.Nil(t, errors)
252+
253+
images, err = runtime.Pull(ctx, ociPathNoRef, config.PullPolicyAlways, pullOptions)
254+
require.NoError(t, err)
255+
require.Len(t, images, 1)
256+
257+
exists, err := runtime.Exists(busybox) // busybox does not exist
258+
require.NoError(t, err)
259+
require.False(t, exists)
260+
261+
names := images[0].Names() // The image has no names (i.e., <none>)
262+
require.Nil(t, names)
263+
264+
images, err = runtime.Pull(ctx, ociPathWithRef, config.PullPolicyAlways, pullOptions)
265+
require.NoError(t, err)
266+
require.Len(t, images, 1)
267+
268+
exists, err = runtime.Exists(busybox) // busybox does exist now
269+
require.NoError(t, err)
270+
require.True(t, exists)
271+
}

0 commit comments

Comments
 (0)