7
7
#endif
8
8
using System . Text . Json ;
9
9
using System . Text . Json . Nodes ;
10
- using System . Text . Json . Serialization ;
11
10
using Microsoft . DotNet . Cli . Utils ;
12
11
using Microsoft . Extensions . Logging ;
13
12
using Microsoft . NET . Build . Containers . Resources ;
@@ -461,35 +460,14 @@ private static async Task WriteIndexJsonForOciImage(
461
460
{
462
461
cancellationToken . ThrowIfCancellationRequested ( ) ;
463
462
464
- var manifests = new PlatformSpecificOciManifest [ destinationReference . Tags . Length ] ;
465
- for ( int i = 0 ; i < destinationReference . Tags . Length ; i ++ )
466
- {
467
- var tag = destinationReference . Tags [ i ] ;
468
- manifests [ i ] = new PlatformSpecificOciManifest
469
- {
470
- mediaType = SchemaTypes . OciManifestV1 ,
471
- size = image . Manifest . Length ,
472
- digest = image . ManifestDigest ,
473
- annotations = new Dictionary < string , string >
474
- {
475
- { "io.containerd.image.name" , $ "{ destinationReference . Repository } :{ tag } " } ,
476
- { "org.opencontainers.image.ref.name" , tag }
477
- }
478
- } ;
479
- }
463
+ string indexJson = ImageIndexGenerator . GenerateImageIndexWithAnnotations (
464
+ SchemaTypes . OciManifestV1 ,
465
+ image . ManifestDigest ,
466
+ image . Manifest . Length ,
467
+ destinationReference . Repository ,
468
+ destinationReference . Tags ) ;
480
469
481
- var index = new ImageIndexV1
482
- {
483
- schemaVersion = 2 ,
484
- mediaType = SchemaTypes . OciImageIndexV1 ,
485
- manifests = manifests
486
- } ;
487
-
488
- var options = new JsonSerializerOptions
489
- {
490
- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
491
- } ;
492
- using ( MemoryStream indexStream = new ( Encoding . UTF8 . GetBytes ( JsonSerializer . SerializeToNode ( index , options ) ! . ToJsonString ( ) ) ) )
470
+ using ( MemoryStream indexStream = new ( Encoding . UTF8 . GetBytes ( indexJson ) ) )
493
471
{
494
472
PaxTarEntry indexEntry = new ( TarEntryType . RegularFile , "index.json" )
495
473
{
@@ -548,31 +526,9 @@ private static async Task WriteIndexJsonForMultiArchOciImage(
548
526
// 1. create manifest list for the blobs
549
527
cancellationToken . ThrowIfCancellationRequested ( ) ;
550
528
551
- var manifests = new PlatformSpecificOciManifest [ images . Length ] ;
552
- for ( int i = 0 ; i < images . Length ; i ++ )
553
- {
554
- var manifest = new PlatformSpecificOciManifest
555
- {
556
- mediaType = SchemaTypes . OciManifestV1 ,
557
- size = images [ i ] . Manifest . Length ,
558
- digest = images [ i ] . ManifestDigest ,
559
- platform = new PlatformInformation { architecture = images [ i ] . Architecture ! , os = images [ i ] . OS ! }
560
- } ;
561
- manifests [ i ] = manifest ;
562
- }
529
+ // For multi-arch we publish only oci-formatted image tarballs.
530
+ string manifestListJson = ImageIndexGenerator . GenerateImageIndex ( images , SchemaTypes . OciManifestV1 , SchemaTypes . OciImageIndexV1 ) ;
563
531
564
- var manifestList = new ImageIndexV1
565
- {
566
- schemaVersion = 2 ,
567
- mediaType = SchemaTypes . OciImageIndexV1 ,
568
- manifests = manifests
569
- } ;
570
-
571
- var options = new JsonSerializerOptions
572
- {
573
- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
574
- } ;
575
- var manifestListJson = JsonSerializer . SerializeToNode ( manifestList , options ) ! . ToJsonString ( ) ;
576
532
var manifestListDigest = DigestUtils . GetDigest ( manifestListJson ) ;
577
533
var manifestListSha = DigestUtils . GetShaFromDigest ( manifestListDigest ) ;
578
534
var manifestListPath = $ "{ _blobsPath } /{ manifestListSha } ";
@@ -586,34 +542,17 @@ private static async Task WriteIndexJsonForMultiArchOciImage(
586
542
await writer . WriteEntryAsync ( indexEntry , cancellationToken ) . ConfigureAwait ( false ) ;
587
543
}
588
544
589
- // 2. create index json that points to manifest list in the blobs
545
+ // 2. create index. json that points to manifest list in the blobs
590
546
cancellationToken . ThrowIfCancellationRequested ( ) ;
591
547
592
- var manifestsIndexJson = new PlatformSpecificOciManifest [ destinationReference . Tags . Length ] ;
593
- for ( int i = 0 ; i < destinationReference . Tags . Length ; i ++ )
594
- {
595
- var tag = destinationReference . Tags [ i ] ;
596
- manifestsIndexJson [ i ] = new PlatformSpecificOciManifest
597
- {
598
- mediaType = SchemaTypes . OciImageIndexV1 ,
599
- size = manifestListJson . Length ,
600
- digest = manifestListDigest ,
601
- annotations = new Dictionary < string , string >
602
- {
603
- { "io.containerd.image.name" , $ "{ destinationReference . Repository } :{ tag } " } ,
604
- { "org.opencontainers.image.ref.name" , tag }
605
- }
606
- } ;
607
- }
608
-
609
- var index = new ImageIndexV1
610
- {
611
- schemaVersion = 2 ,
612
- mediaType = SchemaTypes . OciImageIndexV1 ,
613
- manifests = manifestsIndexJson
614
- } ;
548
+ string indexJson = ImageIndexGenerator . GenerateImageIndexWithAnnotations (
549
+ SchemaTypes . OciImageIndexV1 ,
550
+ manifestListDigest ,
551
+ manifestListJson . Length ,
552
+ destinationReference . Repository ,
553
+ destinationReference . Tags ) ;
615
554
616
- using ( MemoryStream indexStream = new ( Encoding . UTF8 . GetBytes ( JsonSerializer . SerializeToNode ( index , options ) ! . ToJsonString ( ) ) ) )
555
+ using ( MemoryStream indexStream = new ( Encoding . UTF8 . GetBytes ( indexJson ) ) )
617
556
{
618
557
PaxTarEntry indexEntry = new ( TarEntryType . RegularFile , "index.json" )
619
558
{
0 commit comments