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: fix for indexed images #212

Merged
merged 2 commits into from
Jan 16, 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
61 changes: 40 additions & 21 deletions cmd/create-spec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func unpack(ctx context.Context, imgDir string, platform *spec.Platform, rootfs
return nil, err
}
var platformMC platforms.MatchComparer
if platformMC != nil {
if platform != nil {
platformMC = platforms.Only(*platform)
}
return unpackOCI(ctx, imgDir, platformMC, rootfs, idx.Manifests)
Expand All @@ -119,6 +119,10 @@ func unpackOCI(ctx context.Context, imgDir string, platformMC platforms.MatchCom
if err := json.Unmarshal(mfstD, &manifest); err != nil {
return nil, err
}
if !isContainerManifest(manifest) {
fmt.Printf("%v is not a container manifest. skipping...", desc.Digest.String())
continue
}
configD, err := os.ReadFile(filepath.Join(imgDir, "/blobs/sha256", manifest.Config.Digest.Encoded()))
if err != nil {
return nil, err
Expand Down Expand Up @@ -163,37 +167,52 @@ func unpackOCI(ctx context.Context, imgDir string, platformMC platforms.MatchCom
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]
children = append(children, idx.Manifests...)
default:
return nil, fmt.Errorf("unsupported mediatype %v", desc.MediaType)
}
}
if len(children) > 0 {
var childrenDescs []ocispec.Descriptor
for _, d := range children {
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
}
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")
}

func isContainerManifest(manifest ocispec.Manifest) bool {
if !images.IsConfigType(manifest.Config.MediaType) {
return false
}
for _, desc := range manifest.Layers {
if !images.IsLayerType(desc.MediaType) {
return false
}
}
return true
}

func unpackDocker(ctx context.Context, imgDir string, platform *spec.Platform, rootfs string) (io.Reader, error) {
fmt.Println("Trying to unpack image as a docker image")
if rootfs == "" {
Expand Down
Loading