Skip to content

Commit

Permalink
feat: allow to decorate scratch
Browse files Browse the repository at this point in the history
Just creates an index with only runx related image

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 9, 2024
1 parent d5e5b0f commit 08ebcb0
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions runkit/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit 08ebcb0

Please sign in to comment.