Skip to content

Commit 4eb508c

Browse files
authored
Always include tag in tarball refs (#893)
1 parent 0d81a61 commit 4eb508c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Diff for: pkg/v1/tarball/write.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,22 @@ func dedupRefToImage(refToImage map[name.Reference]v1.Image) map[v1.Image][]stri
338338

339339
for ref, img := range refToImage {
340340
if tag, ok := ref.(name.Tag); ok {
341-
if tags, ok := imageToTags[img]; ok && tags != nil {
342-
imageToTags[img] = append(tags, tag.String())
343-
} else {
344-
imageToTags[img] = []string{tag.String()}
341+
if tags, ok := imageToTags[img]; !ok || tags == nil {
342+
imageToTags[img] = []string{}
345343
}
344+
// Docker cannot load tarballs without an explicit tag:
345+
// https://github.com/google/go-containerregistry/issues/890
346+
//
347+
// We can't use the fully qualified tag.Name() because of rules_docker:
348+
// https://github.com/google/go-containerregistry/issues/527
349+
//
350+
// If the tag is "latest", but tag.String() doesn't end in ":latest",
351+
// just append it. Kind of gross, but should work for now.
352+
ts := tag.String()
353+
if tag.Identifier() == name.DefaultTag && !strings.HasSuffix(ts, ":"+name.DefaultTag) {
354+
ts = fmt.Sprintf("%s:%s", ts, name.DefaultTag)
355+
}
356+
imageToTags[img] = append(imageToTags[img], ts)
346357
} else {
347358
if _, ok := imageToTags[img]; !ok {
348359
imageToTags[img] = nil

Diff for: pkg/v1/tarball/write_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ func TestWriteSharedLayers(t *testing.T) {
378378
}
379379

380380
func TestComputeManifest(t *testing.T) {
381-
var randomTag, mutatedTag = "gcr.io/foo/bar:latest", "gcr.io/baz/bat:latest"
381+
var randomTag, mutatedTag = "ubuntu", "gcr.io/baz/bat:latest"
382+
383+
// https://github.com/google/go-containerregistry/issues/890
384+
randomTagWritten := "ubuntu:latest"
385+
382386
// Make a random image
383387
randImage, err := random.Image(256, 1)
384388
if err != nil {
@@ -388,7 +392,7 @@ func TestComputeManifest(t *testing.T) {
388392
if err != nil {
389393
t.Fatalf("error getting random image config: %v", err)
390394
}
391-
tag1, err := name.NewTag(randomTag, name.StrictValidation)
395+
tag1, err := name.NewTag(randomTag)
392396
if err != nil {
393397
t.Fatalf("Error creating test tag1.")
394398
}
@@ -441,7 +445,7 @@ func TestComputeManifest(t *testing.T) {
441445
},
442446
{
443447
Config: randConfig.String(),
444-
RepoTags: []string{randomTag},
448+
RepoTags: []string{randomTagWritten},
445449
Layers: randomLayersFilenames,
446450
},
447451
}

0 commit comments

Comments
 (0)