5
5
"encoding/base64"
6
6
"encoding/json"
7
7
"fmt"
8
+ "sort"
8
9
"strconv"
9
10
"strings"
10
11
@@ -358,6 +359,32 @@ func (e *imageExporterInstance) pushImage(ctx context.Context, src *exporter.Sou
358
359
}
359
360
360
361
func (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
+
361
388
unpackDone := progress .OneOff (ctx , "unpacking to " + img .Name )
362
389
defer func () {
363
390
unpackDone (err0 )
@@ -375,15 +402,6 @@ func (e *imageExporterInstance) unpackImage(ctx context.Context, img images.Imag
375
402
return err
376
403
}
377
404
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
-
387
405
remotes , err := ref .GetRemotes (ctx , true , e .opts .RefCfg , false , s )
388
406
if err != nil {
389
407
return err
@@ -457,12 +475,6 @@ func addAnnotations(m map[digest.Digest]map[string]string, desc ocispecs.Descrip
457
475
}
458
476
}
459
477
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
-
466
478
func NewDescriptorReference (desc ocispecs.Descriptor , release func (context.Context ) error ) exporter.DescriptorReference {
467
479
return & descriptorReference {
468
480
desc : desc ,
0 commit comments