Skip to content

Commit

Permalink
Merge pull request #669 from vrothberg/fix-import
Browse files Browse the repository at this point in the history
libimage: import: fix tags
  • Loading branch information
openshift-merge-robot authored Jul 7, 2021
2 parents a7351a8 + f24d6fb commit a8e84e7
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 a8e84e7

Please sign in to comment.