Skip to content

Commit

Permalink
libimage: import: fix tags
Browse files Browse the repository at this point in the history
When importing, first create the image and tag it afterwards.  This also
makes sure that an imported image *without* a tag is correctly listed as
"<none>".  Previously, such images were tagged as
"docker.io/library/sha256:$ID" (inherited from older Podman code).

Context: containers/podman/issues/10854
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Jul 6, 2021
1 parent a7351a8 commit f24d6fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
28 changes: 19 additions & 9 deletions libimage/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,12 @@ func (r *Runtime) Import(ctx context.Context, path string, options *ImportOption
return "", err
}

name := options.Tag
if name == "" {
name, err = getImageDigest(ctx, srcRef, r.systemContextCopy())
if err != nil {
return "", err
}
name = "sha256:" + name[1:] // strip leading "@"
id, err := getImageDigest(ctx, srcRef, r.systemContextCopy())
if err != nil {
return "", err
}

destRef, err := storageTransport.Transport.ParseStoreReference(r.store, name)
destRef, err := storageTransport.Transport.ParseStoreReference(r.store, id)
if err != nil {
return "", err
}
Expand All @@ -110,5 +106,19 @@ func (r *Runtime) Import(ctx context.Context, path string, options *ImportOption
return "", err
}

return name, nil
// Strip the leading @ off the id.
name := id[1:]

// If requested, tag the imported image.
if options.Tag != "" {
image, _, err := r.LookupImage(name, nil)
if err != nil {
return "", errors.Wrap(err, "looking up imported image")
}
if err := image.Tag(options.Tag); err != nil {
return "", err
}
}

return "sha256:" + name, nil
}
20 changes: 14 additions & 6 deletions libimage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ func TestImport(t *testing.T) {
importOptions := &ImportOptions{}
importOptions.Writer = os.Stdout

imported, err := runtime.Import(ctx, "testdata/exported-container.tar", importOptions)
require.NoError(t, err)
for _, tag := range []string{"", "foobar"} {
importOptions.Tag = tag
imported, err := runtime.Import(ctx, "testdata/exported-container.tar", importOptions)
require.NoError(t, err)

image, resolvedName, err := runtime.LookupImage(imported, nil)
require.NoError(t, err)
require.Equal(t, imported, resolvedName)
require.Equal(t, imported, "sha256:"+image.ID())
image, resolvedName, err := runtime.LookupImage(imported, nil)
require.NoError(t, err)
require.Equal(t, imported, resolvedName)
require.Equal(t, "sha256:"+image.ID(), imported)

if tag != "" {
_, _, err := runtime.LookupImage(tag, nil)
require.NoError(t, err)
}
}
}

0 comments on commit f24d6fb

Please sign in to comment.