From ec51ec868b13834755b6bde14456f56c7b932a6c Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Fri, 30 Jun 2023 15:32:36 +0100 Subject: [PATCH 1/2] exporter: silently skip unpacking unknown reference Signed-off-by: Justin Chadwell --- exporter/containerimage/export.go | 39 +++++++++++++++++++------------ 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/exporter/containerimage/export.go b/exporter/containerimage/export.go index ea380326e7cb..230021bd18e9 100644 --- a/exporter/containerimage/export.go +++ b/exporter/containerimage/export.go @@ -358,6 +358,30 @@ 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) { + p := platforms.Format(platforms.Normalize(platforms.DefaultSpec())) + + ps, err := exptypes.ParsePlatforms(src.Metadata) + if err != nil { + return err + } + found := false + for _, p2 := range ps.Platforms { + if p2.ID == p { + found = true + break + } + } + if !found { + // current platform was not found, so skip unpacking + return nil + } + + ref, _ := src.FindRef(p) + 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) @@ -375,15 +399,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 @@ -457,12 +472,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, From 6c1d491b57d33f65f8a07935c6c86f7e4eccfe1a Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 6 Jul 2023 22:12:27 -0700 Subject: [PATCH 2/2] containerimage: use platform matcher to detect platform to unpack Signed-off-by: Tonis Tiigi --- exporter/containerimage/export.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/exporter/containerimage/export.go b/exporter/containerimage/export.go index 230021bd18e9..2c2775ac7e4b 100644 --- a/exporter/containerimage/export.go +++ b/exporter/containerimage/export.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "sort" "strconv" "strings" @@ -358,25 +359,27 @@ 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) { - p := platforms.Format(platforms.Normalize(platforms.DefaultSpec())) + matcher := platforms.Only(platforms.Normalize(platforms.DefaultSpec())) ps, err := exptypes.ParsePlatforms(src.Metadata) if err != nil { return err } - found := false + matching := []exptypes.Platform{} for _, p2 := range ps.Platforms { - if p2.ID == p { - found = true - break + if matcher.Match(p2.Platform) { + matching = append(matching, p2) } } - if !found { + 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(p) + ref, _ := src.FindRef(matching[0].ID) if ref == nil { // ref has no layers, so nothing to unpack return nil