55 "encoding/base64"
66 "encoding/json"
77 "fmt"
8+ "sort"
89 "strconv"
910 "strings"
1011
@@ -358,6 +359,32 @@ func (e *imageExporterInstance) pushImage(ctx context.Context, src *exporter.Sou
358359}
359360
360361func (e * imageExporterInstance ) unpackImage (ctx context.Context , img images.Image , src * exporter.Source , s session.Group ) (err0 error ) {
362+ matcher := platforms .Only (platforms .Normalize (platforms .DefaultSpec ()))
363+
364+ ps , err := exptypes .ParsePlatforms (src .Metadata )
365+ if err != nil {
366+ return err
367+ }
368+ matching := []exptypes.Platform {}
369+ for _ , p2 := range ps .Platforms {
370+ if matcher .Match (p2 .Platform ) {
371+ matching = append (matching , p2 )
372+ }
373+ }
374+ if len (matching ) == 0 {
375+ // current platform was not found, so skip unpacking
376+ return nil
377+ }
378+ sort .SliceStable (matching , func (i , j int ) bool {
379+ return matcher .Less (matching [i ].Platform , matching [j ].Platform )
380+ })
381+
382+ ref , _ := src .FindRef (matching [0 ].ID )
383+ if ref == nil {
384+ // ref has no layers, so nothing to unpack
385+ return nil
386+ }
387+
361388 unpackDone := progress .OneOff (ctx , "unpacking to " + img .Name )
362389 defer func () {
363390 unpackDone (err0 )
@@ -375,15 +402,6 @@ func (e *imageExporterInstance) unpackImage(ctx context.Context, img images.Imag
375402 return err
376403 }
377404
378- ref , ok := src .FindRef (defaultPlatform ())
379- if ! ok {
380- return errors .Errorf ("no reference for default platform %s" , defaultPlatform ())
381- }
382- if ref == nil {
383- // ref has no layers, so nothing to unpack
384- return nil
385- }
386-
387405 remotes , err := ref .GetRemotes (ctx , true , e .opts .RefCfg , false , s )
388406 if err != nil {
389407 return err
@@ -457,12 +475,6 @@ func addAnnotations(m map[digest.Digest]map[string]string, desc ocispecs.Descrip
457475 }
458476}
459477
460- func defaultPlatform () string {
461- // Use normalized platform string to avoid the mismatch with platform options which
462- // are normalized using platforms.Normalize()
463- return platforms .Format (platforms .Normalize (platforms .DefaultSpec ()))
464- }
465-
466478func NewDescriptorReference (desc ocispecs.Descriptor , release func (context.Context ) error ) exporter.DescriptorReference {
467479 return & descriptorReference {
468480 desc : desc ,
0 commit comments