imagetools(create): set correct media type when combining manifests

When using imagetools create and combining multiple sources
we should check the media type of each manifest and set
the right media type for the manifest list.

If there is a mismatch we set OCI index as best effort.

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-05-16 11:46:48 +02:00
committed by CrazyMax
parent 1eb9ad979e
commit b702188b65

View File

@@ -122,24 +122,29 @@ func (r *Resolver) Combine(ctx context.Context, srcs []*Source) ([]byte, ocispec
} }
} }
mt := images.MediaTypeDockerSchema2ManifestList //ocispec.MediaTypeImageIndex dockerMfsts := 0
idx := struct { for _, desc := range newDescs {
// MediaType is reserved in the OCI spec but if strings.HasPrefix(desc.MediaType, "application/vnd.docker.") {
// excluded from go types. dockerMfsts++
MediaType string `json:"mediaType,omitempty"` }
}
ocispec.Index var mt string
}{ if dockerMfsts == len(newDescs) {
// all manifests are Docker types, use Docker manifest list
mt = images.MediaTypeDockerSchema2ManifestList
} else {
// otherwise, use OCI index
mt = ocispec.MediaTypeImageIndex
}
idxBytes, err := json.MarshalIndent(ocispec.Index{
MediaType: mt, MediaType: mt,
Index: ocispec.Index{
Versioned: specs.Versioned{ Versioned: specs.Versioned{
SchemaVersion: 2, SchemaVersion: 2,
}, },
Manifests: newDescs, Manifests: newDescs,
}, }, "", " ")
}
idxBytes, err := json.MarshalIndent(idx, "", " ")
if err != nil { if err != nil {
return nil, ocispec.Descriptor{}, errors.Wrap(err, "failed to marshal index") return nil, ocispec.Descriptor{}, errors.Wrap(err, "failed to marshal index")
} }