|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Text.Encodings.Web; |
4 | 5 | using System.Text.Json; |
5 | | -using System.Text.Json.Nodes; |
| 6 | +using System.Text.Json.Serialization; |
6 | 7 | using Microsoft.NET.Build.Containers.Resources; |
7 | | -using Microsoft.NET.Build.Containers.Tasks; |
8 | 8 |
|
9 | 9 | namespace Microsoft.NET.Build.Containers; |
10 | 10 |
|
11 | | -internal readonly struct ImageInfo |
12 | | -{ |
13 | | - internal string Config { get; init; } |
14 | | - internal string ManifestDigest { get; init; } |
15 | | - internal string Manifest { get; init; } |
16 | | - internal string ManifestMediaType { get; init; } |
17 | | - |
18 | | - public override string ToString() => ManifestDigest; |
19 | | -} |
20 | | - |
21 | 11 | internal static class ImageIndexGenerator |
22 | 12 | { |
23 | 13 | /// <summary> |
24 | 14 | /// Generates an image index from the given images. |
25 | 15 | /// </summary> |
26 | | - /// <param name="imageInfos"></param> |
| 16 | + /// <param name="images">Images to generate image index from.</param> |
27 | 17 | /// <returns>Returns json string of image index and image index mediaType.</returns> |
28 | 18 | /// <exception cref="ArgumentException"></exception> |
29 | 19 | /// <exception cref="NotSupportedException"></exception> |
30 | | - internal static (string, string) GenerateImageIndex(ImageInfo[] imageInfos) |
| 20 | + internal static (string, string) GenerateImageIndex(BuiltImage[] images) |
31 | 21 | { |
32 | | - if (imageInfos.Length == 0) |
| 22 | + if (images.Length == 0) |
33 | 23 | { |
34 | | - throw new ArgumentException(string.Format(Strings.ImagesEmpty)); |
| 24 | + throw new ArgumentException(Strings.ImagesEmpty); |
35 | 25 | } |
36 | 26 |
|
37 | | - string manifestMediaType = imageInfos[0].ManifestMediaType; |
| 27 | + string manifestMediaType = images[0].ManifestMediaType; |
38 | 28 |
|
39 | | - if (!imageInfos.All(image => string.Equals(image.ManifestMediaType, manifestMediaType, StringComparison.OrdinalIgnoreCase))) |
| 29 | + if (!images.All(image => string.Equals(image.ManifestMediaType, manifestMediaType, StringComparison.OrdinalIgnoreCase))) |
40 | 30 | { |
41 | 31 | throw new ArgumentException(Strings.MixedMediaTypes); |
42 | 32 | } |
43 | 33 |
|
44 | 34 | if (manifestMediaType == SchemaTypes.DockerManifestV2) |
45 | 35 | { |
46 | | - return GenerateImageIndex(imageInfos, SchemaTypes.DockerManifestV2, SchemaTypes.DockerManifestListV2); |
| 36 | + return (GenerateImageIndex(images, SchemaTypes.DockerManifestV2, SchemaTypes.DockerManifestListV2), SchemaTypes.DockerManifestListV2); |
47 | 37 | } |
48 | 38 | else if (manifestMediaType == SchemaTypes.OciManifestV1) |
49 | 39 | { |
50 | | - return GenerateImageIndex(imageInfos, SchemaTypes.OciManifestV1, SchemaTypes.OciImageIndexV1); |
| 40 | + return (GenerateImageIndex(images, SchemaTypes.OciManifestV1, SchemaTypes.OciImageIndexV1), SchemaTypes.OciImageIndexV1); |
51 | 41 | } |
52 | 42 | else |
53 | 43 | { |
54 | 44 | throw new NotSupportedException(string.Format(Strings.UnsupportedMediaType, manifestMediaType)); |
55 | 45 | } |
56 | 46 | } |
57 | 47 |
|
58 | | - private static (string, string) GenerateImageIndex(ImageInfo[] images, string manifestMediaType, string imageIndexMediaType) |
| 48 | + /// <summary> |
| 49 | + /// Generates an image index from the given images. |
| 50 | + /// </summary> |
| 51 | + /// <param name="images">Images to generate image index from.</param> |
| 52 | + /// <param name="manifestMediaType">Media type of the manifest.</param> |
| 53 | + /// <param name="imageIndexMediaType">Media type of the produced image index.</param> |
| 54 | + /// <returns>Returns json string of image index and image index mediaType.</returns> |
| 55 | + /// <exception cref="ArgumentException"></exception> |
| 56 | + /// <exception cref="NotSupportedException"></exception> |
| 57 | + internal static string GenerateImageIndex(BuiltImage[] images, string manifestMediaType, string imageIndexMediaType) |
59 | 58 | { |
| 59 | + if (images.Length == 0) |
| 60 | + { |
| 61 | + throw new ArgumentException(Strings.ImagesEmpty); |
| 62 | + } |
| 63 | + |
60 | 64 | // Here we are using ManifestListV2 struct, but we could use ImageIndexV1 struct as well. |
61 | | - // We are filling the same fiels, so we can use the same struct. |
| 65 | + // We are filling the same fields, so we can use the same struct. |
62 | 66 | var manifests = new PlatformSpecificManifest[images.Length]; |
| 67 | + |
63 | 68 | for (int i = 0; i < images.Length; i++) |
64 | 69 | { |
65 | | - var image = images[i]; |
66 | | - |
67 | | - var manifest = new PlatformSpecificManifest |
| 70 | + manifests[i] = new PlatformSpecificManifest |
68 | 71 | { |
69 | 72 | mediaType = manifestMediaType, |
70 | | - size = image.Manifest.Length, |
71 | | - digest = image.ManifestDigest, |
72 | | - platform = GetArchitectureAndOsFromConfig(image) |
| 73 | + size = images[i].Manifest.Length, |
| 74 | + digest = images[i].ManifestDigest, |
| 75 | + platform = new PlatformInformation |
| 76 | + { |
| 77 | + architecture = images[i].Architecture!, |
| 78 | + os = images[i].OS! |
| 79 | + } |
73 | 80 | }; |
74 | | - manifests[i] = manifest; |
75 | 81 | } |
76 | 82 |
|
77 | | - var dockerManifestList = new ManifestListV2 |
| 83 | + var imageIndex = new ManifestListV2 |
78 | 84 | { |
79 | 85 | schemaVersion = 2, |
80 | 86 | mediaType = imageIndexMediaType, |
81 | 87 | manifests = manifests |
82 | 88 | }; |
83 | 89 |
|
84 | | - return (JsonSerializer.SerializeToNode(dockerManifestList)?.ToJsonString() ?? "", dockerManifestList.mediaType); |
| 90 | + return GetJsonStringFromImageIndex(imageIndex); |
85 | 91 | } |
86 | 92 |
|
87 | | - private static PlatformInformation GetArchitectureAndOsFromConfig(ImageInfo image) |
| 93 | + internal static string GenerateImageIndexWithAnnotations(string manifestMediaType, string manifestDigest, long manifestSize, string repository, string[] tags) |
88 | 94 | { |
89 | | - var configJson = JsonNode.Parse(image.Config) as JsonObject ?? |
90 | | - throw new ArgumentException($"{nameof(image.Config)} should be a JSON object.", nameof(image.Config)); |
| 95 | + string containerdImageNamePrefix = repository.Contains('/') ? "docker.io/" : "docker.io/library/"; |
| 96 | + |
| 97 | + var manifests = new PlatformSpecificOciManifest[tags.Length]; |
| 98 | + for (int i = 0; i < tags.Length; i++) |
| 99 | + { |
| 100 | + var tag = tags[i]; |
| 101 | + manifests[i] = new PlatformSpecificOciManifest |
| 102 | + { |
| 103 | + mediaType = manifestMediaType, |
| 104 | + size = manifestSize, |
| 105 | + digest = manifestDigest, |
| 106 | + annotations = new Dictionary<string, string> |
| 107 | + { |
| 108 | + { "io.containerd.image.name", $"{containerdImageNamePrefix}{repository}:{tag}" }, |
| 109 | + { "org.opencontainers.image.ref.name", tag } |
| 110 | + } |
| 111 | + }; |
| 112 | + } |
91 | 113 |
|
92 | | - var architecture = configJson["architecture"]?.ToString() ?? |
93 | | - throw new ArgumentException($"{nameof(image.Config)} should contain 'architecture'.", nameof(image.Config)); |
| 114 | + var index = new ImageIndexV1 |
| 115 | + { |
| 116 | + schemaVersion = 2, |
| 117 | + mediaType = SchemaTypes.OciImageIndexV1, |
| 118 | + manifests = manifests |
| 119 | + }; |
94 | 120 |
|
95 | | - var os = configJson["os"]?.ToString() ?? |
96 | | - throw new ArgumentException($"{nameof(image.Config)} should contain 'os'.", nameof(image.Config)); |
| 121 | + return GetJsonStringFromImageIndex(index); |
| 122 | + } |
| 123 | + |
| 124 | + private static string GetJsonStringFromImageIndex<T>(T imageIndex) |
| 125 | + { |
| 126 | + var nullIgnoreOptions = new JsonSerializerOptions |
| 127 | + { |
| 128 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 129 | + }; |
| 130 | + // To avoid things like \u002B for '+' especially in media types ("application/vnd.oci.image.manifest.v1\u002Bjson"), we use UnsafeRelaxedJsonEscaping. |
| 131 | + var escapeOptions = new JsonSerializerOptions |
| 132 | + { |
| 133 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 134 | + }; |
97 | 135 |
|
98 | | - return new PlatformInformation { architecture = architecture, os = os }; |
| 136 | + return JsonSerializer.SerializeToNode(imageIndex, nullIgnoreOptions)?.ToJsonString(escapeOptions) ?? ""; |
99 | 137 | } |
100 | 138 | } |
0 commit comments