Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create-spec: support images with nested index #208

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions cmd/create-spec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"sort"

"github.com/containerd/containerd/archive"
"github.com/containerd/containerd/archive/compression"
Expand Down Expand Up @@ -99,7 +100,12 @@ func unpack(ctx context.Context, imgDir string, platform *spec.Platform, rootfs
if platformMC != nil {
platformMC = platforms.Only(*platform)
}
for _, desc := range idx.Manifests {
return unpackOCI(ctx, imgDir, platformMC, rootfs, idx.Manifests)
}

func unpackOCI(ctx context.Context, imgDir string, platformMC platforms.MatchComparer, rootfs string, descs []ocispec.Descriptor) (io.Reader, error) {
var children []ocispec.Descriptor
for _, desc := range descs {
switch desc.MediaType {
case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest:
if desc.Platform != nil && platformMC != nil && !platformMC.Match(*desc.Platform) {
Expand Down Expand Up @@ -148,11 +154,43 @@ func unpack(ctx context.Context, imgDir string, platform *spec.Platform, rootfs
}
}
return bytes.NewReader(configD), nil
case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
idxD, err := os.ReadFile(filepath.Join(imgDir, "/blobs/sha256", desc.Digest.Encoded()))
if err != nil {
return nil, err
}
var idx ocispec.Index
if err := json.Unmarshal(idxD, &idx); err != nil {
return nil, err
}
var childrenDescs []ocispec.Descriptor
for _, d := range idx.Manifests {
if d.Platform != nil && platformMC != nil && !platformMC.Match(*d.Platform) {
continue
}
childrenDescs = append(childrenDescs, d)
}
sort.SliceStable(childrenDescs, func(i, j int) bool {
if childrenDescs[i].Platform == nil {
return false
}
if childrenDescs[j].Platform == nil {
return true
}
if platformMC != nil {
return platformMC.Less(*childrenDescs[i].Platform, *childrenDescs[j].Platform)
}
return true
})
children = childrenDescs[:1]
default:
// TODO: support nested manifest lists?
return nil, fmt.Errorf("unsupported mediatype %v", desc.MediaType)
}
}
if len(children) > 0 {
fmt.Printf("nested manifest: processing %v\n", children)
return unpackOCI(ctx, imgDir, platformMC, rootfs, children)
}
return nil, fmt.Errorf("target config not found")
}

Expand Down
Loading