From d131f00fffbc16ed527882a586cc8fccfab2cc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 26 Jan 2024 18:24:53 +0100 Subject: [PATCH] image/save: Fix untagged images not present in index.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Saving an image via digested reference, ID or truncated ID doesn't store the image reference in the archive. This also causes the save code to not add the image's manifest to the index.json. This commit explicitly adds the untagged manifests to the index.json if no tagged manifests were added. Signed-off-by: Paweł Gronowski --- image/tarexport/save.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/image/tarexport/save.go b/image/tarexport/save.go index 41857dbebe..41c3f09c69 100644 --- a/image/tarexport/save.go +++ b/image/tarexport/save.go @@ -260,6 +260,12 @@ func (s *saveSession) save(outStream io.Writer) error { } size := int64(len(data)) + untaggedMfstDesc := ocispec.Descriptor{ + MediaType: ocispec.MediaTypeImageManifest, + Digest: dgst, + Size: size, + Platform: m.Config.Platform, + } for _, ref := range imageDescr.refs { familiarName := reference.FamiliarName(ref) if _, ok := reposLegacy[familiarName]; !ok { @@ -268,16 +274,17 @@ func (s *saveSession) save(outStream io.Writer) error { reposLegacy[familiarName][ref.Tag()] = digest.Digest(imageDescr.layers[len(imageDescr.layers)-1]).Encoded() repoTags = append(repoTags, reference.FamiliarString(ref)) - manifestDescriptors = append(manifestDescriptors, ocispec.Descriptor{ - MediaType: ocispec.MediaTypeImageManifest, - Digest: dgst, - Size: size, - Platform: m.Config.Platform, - Annotations: map[string]string{ - images.AnnotationImageName: ref.String(), - ocispec.AnnotationRefName: ref.Tag(), - }, - }) + taggedManifest := untaggedMfstDesc + taggedManifest.Annotations = map[string]string{ + images.AnnotationImageName: ref.String(), + ocispec.AnnotationRefName: ref.Tag(), + } + manifestDescriptors = append(manifestDescriptors, taggedManifest) + } + + // If no ref was assigned, make sure still add the image is still included in index.json. + if len(manifestDescriptors) == 0 { + manifestDescriptors = append(manifestDescriptors, untaggedMfstDesc) } for _, l := range imageDescr.layers {