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

Skip over non-native platforms when unpacking image #3983

Merged
merged 2 commits into from
Jul 7, 2023
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: 27 additions & 15 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -358,6 +359,32 @@ func (e *imageExporterInstance) pushImage(ctx context.Context, src *exporter.Sou
}

func (e *imageExporterInstance) unpackImage(ctx context.Context, img images.Image, src *exporter.Source, s session.Group) (err0 error) {
matcher := platforms.Only(platforms.Normalize(platforms.DefaultSpec()))

ps, err := exptypes.ParsePlatforms(src.Metadata)
if err != nil {
return err
}
matching := []exptypes.Platform{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a slice? Should we not take the first one found and break instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to get the platform that has the highest priority, not just the first platform from the array. This should match how the specific platform is found when making a container for a specific platform from an image.

for _, p2 := range ps.Platforms {
if matcher.Match(p2.Platform) {
matching = append(matching, p2)
}
}
if len(matching) == 0 {
// current platform was not found, so skip unpacking
return nil
}
sort.SliceStable(matching, func(i, j int) bool {
return matcher.Less(matching[i].Platform, matching[j].Platform)
})

ref, _ := src.FindRef(matching[0].ID)
if ref == nil {
// ref has no layers, so nothing to unpack
return nil
}

unpackDone := progress.OneOff(ctx, "unpacking to "+img.Name)
defer func() {
unpackDone(err0)
Expand All @@ -375,15 +402,6 @@ func (e *imageExporterInstance) unpackImage(ctx context.Context, img images.Imag
return err
}

ref, ok := src.FindRef(defaultPlatform())
if !ok {
return errors.Errorf("no reference for default platform %s", defaultPlatform())
}
if ref == nil {
// ref has no layers, so nothing to unpack
return nil
}

remotes, err := ref.GetRemotes(ctx, true, e.opts.RefCfg, false, s)
if err != nil {
return err
Expand Down Expand Up @@ -457,12 +475,6 @@ func addAnnotations(m map[digest.Digest]map[string]string, desc ocispecs.Descrip
}
}

func defaultPlatform() string {
// Use normalized platform string to avoid the mismatch with platform options which
// are normalized using platforms.Normalize()
return platforms.Format(platforms.Normalize(platforms.DefaultSpec()))
}

func NewDescriptorReference(desc ocispecs.Descriptor, release func(context.Context) error) exporter.DescriptorReference {
return &descriptorReference{
desc: desc,
Expand Down
Loading