diff --git a/runkit/decorate.go b/runkit/decorate.go index bc61f1b..a56b36f 100644 --- a/runkit/decorate.go +++ b/runkit/decorate.go @@ -30,57 +30,69 @@ func Decorate(ctx context.Context, src, dest string, runxConfig, runxDoc []byte) return fmt.Errorf("could not create runx image: %w", err) } - desc, err = remote.Get(ref, remoteOpts...) - if err != nil { - return fmt.Errorf("could not get image %s: %w", src, err) - } - - if desc.MediaType.IsImage() { - img, err := remote.Image(ref, remoteOpts...) - if err != nil { - return fmt.Errorf("could not get image %s: %w", src, err) - } - configFile, _ := img.ConfigFile() - imgDesc := desc.Descriptor - imgDesc.Platform = configFile.Platform() - + if src == "scratch" { index = // create a manifest mutate.AppendManifests( // as an index mutate.IndexMediaType(empty.Index, types.OCIImageIndex), - // with the referenced image - mutate.IndexAddendum{ - Add: img, - Descriptor: imgDesc, - }, // and the new runx image mutate.IndexAddendum{ Add: runxImage, Descriptor: *runxDesc, }) - } else if desc.MediaType.IsIndex() { - index, err = remote.Index(ref, remoteOpts...) + } else { + desc, err = remote.Get(ref, remoteOpts...) if err != nil { - return fmt.Errorf("could not get image index %s: %w", src, err) + return fmt.Errorf("could not get image %s: %w", src, err) } - // remove existing runx manifest - manifests, _ := index.IndexManifest() - for _, manifest := range manifests.Manifests { - if _, ok := manifest.Annotations[runkit.RunxManifestType]; ok { - index = mutate.RemoveManifests(index, match.Digests(manifest.Digest)) + if desc.MediaType.IsImage() { + img, err := remote.Image(ref, remoteOpts...) + if err != nil { + return fmt.Errorf("could not get image %s: %w", src, err) } - } + configFile, _ := img.ConfigFile() + imgDesc := desc.Descriptor + imgDesc.Platform = configFile.Platform() - // add the new runx manifest - index = mutate.AppendManifests( - index, - mutate.IndexAddendum{ - Add: runxImage, - Descriptor: *runxDesc, - }) - } else { - return fmt.Errorf("unsupported media type %s", desc.MediaType) + index = // create a manifest + mutate.AppendManifests( + // as an index + mutate.IndexMediaType(empty.Index, types.OCIImageIndex), + // with the referenced image + mutate.IndexAddendum{ + Add: img, + Descriptor: imgDesc, + }, + // and the new runx image + mutate.IndexAddendum{ + Add: runxImage, + Descriptor: *runxDesc, + }) + } else if desc.MediaType.IsIndex() { + index, err = remote.Index(ref, remoteOpts...) + if err != nil { + return fmt.Errorf("could not get image index %s: %w", src, err) + } + + // remove existing runx manifest + manifests, _ := index.IndexManifest() + for _, manifest := range manifests.Manifests { + if _, ok := manifest.Annotations[runkit.RunxManifestType]; ok { + index = mutate.RemoveManifests(index, match.Digests(manifest.Digest)) + } + } + + // add the new runx manifest + index = mutate.AppendManifests( + index, + mutate.IndexAddendum{ + Add: runxImage, + Descriptor: *runxDesc, + }) + } else { + return fmt.Errorf("unsupported media type %s", desc.MediaType) + } } err = remote.WriteIndex(destRef, index, remoteOpts...)