Skip to content

Commit

Permalink
Merge pull request #208 from ktock/nestedidx
Browse files Browse the repository at this point in the history
`create-spec`: support images with nested index
  • Loading branch information
ktock authored Jan 15, 2024
2 parents 8a75ade + 4bdbf65 commit da3f35c
Showing 1 changed file with 40 additions and 2 deletions.
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

0 comments on commit da3f35c

Please sign in to comment.