Skip to content

Commit c75bf13

Browse files
committed
more thought put into error messages
1 parent 0468080 commit c75bf13

File tree

3 files changed

+69
-54
lines changed

3 files changed

+69
-54
lines changed

Microsoft.NET.Build.Containers/ContainerBuilder.cs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,73 @@ public static async Task Containerize(DirectoryInfo folder, string workingDir, s
1616
Registry baseRegistry = new Registry(ContainerHelpers.TryExpandRegistryToUri(registryName));
1717

1818
var img = await baseRegistry.GetImageManifest(baseName, baseTag, containerRuntimeIdentifier);
19-
if (img is not null) {
20-
img.WorkingDirectory = workingDir;
19+
if (img is null) {
20+
throw new ArgumentException($"Could not find image {baseName}:{baseTag} in registry {registryName} matching RuntimeIdentifier {containerRuntimeIdentifier}");
21+
}
2122

22-
JsonSerializerOptions options = new()
23-
{
24-
WriteIndented = true,
25-
};
23+
img.WorkingDirectory = workingDir;
2624

27-
Layer l = Layer.FromDirectory(folder.FullName, workingDir);
25+
JsonSerializerOptions options = new()
26+
{
27+
WriteIndented = true,
28+
};
2829

29-
img.AddLayer(l);
30+
Layer l = Layer.FromDirectory(folder.FullName, workingDir);
3031

31-
img.SetEntrypoint(entrypoint, entrypointArgs);
32+
img.AddLayer(l);
3233

33-
var isDockerPush = String.IsNullOrEmpty(outputRegistry);
34-
Registry? outputReg = isDockerPush ? null : new Registry(ContainerHelpers.TryExpandRegistryToUri(outputRegistry));
34+
img.SetEntrypoint(entrypoint, entrypointArgs);
3535

36-
foreach (var label in labels)
37-
{
38-
string[] labelPieces = label.Split('=');
36+
var isDockerPush = String.IsNullOrEmpty(outputRegistry);
37+
Registry? outputReg = isDockerPush ? null : new Registry(ContainerHelpers.TryExpandRegistryToUri(outputRegistry));
3938

40-
// labels are validated by System.CommandLine API
41-
img.Label(labelPieces[0], labelPieces[1]);
42-
}
39+
foreach (var label in labels)
40+
{
41+
string[] labelPieces = label.Split('=');
4342

44-
foreach (string envVar in envVars)
45-
{
46-
string[] envPieces = envVar.Split('=', 2);
43+
// labels are validated by System.CommandLine API
44+
img.Label(labelPieces[0], labelPieces[1]);
45+
}
4746

48-
img.AddEnvironmentVariable(envPieces[0], envPieces[1]);
49-
}
47+
foreach (string envVar in envVars)
48+
{
49+
string[] envPieces = envVar.Split('=', 2);
50+
51+
img.AddEnvironmentVariable(envPieces[0], envPieces[1]);
52+
}
53+
54+
foreach (var (number, type) in exposedPorts)
55+
{
56+
// ports are validated by System.CommandLine API
57+
img.ExposePort(number, type);
58+
}
5059

51-
foreach (var (number, type) in exposedPorts)
60+
foreach (var tag in imageTags)
61+
{
62+
if (isDockerPush)
5263
{
53-
// ports are validated by System.CommandLine API
54-
img.ExposePort(number, type);
64+
try
65+
{
66+
LocalDocker.Load(img, imageName, tag, baseName).Wait();
67+
Console.WriteLine("Containerize: Pushed container '{0}:{1}' to Docker daemon", imageName, tag);
68+
}
69+
catch (Exception e)
70+
{
71+
Console.WriteLine($"Containerize: error CONTAINER001: Failed to push to local docker registry: {e}");
72+
Environment.ExitCode = -1;
73+
}
5574
}
56-
57-
foreach (var tag in imageTags)
75+
else
5876
{
59-
if (isDockerPush)
77+
try
6078
{
61-
try
62-
{
63-
LocalDocker.Load(img, imageName, tag, baseName).Wait();
64-
Console.WriteLine("Containerize: Pushed container '{0}:{1}' to Docker daemon", imageName, tag);
65-
}
66-
catch (Exception e)
67-
{
68-
Console.WriteLine($"Containerize: error CONTAINER001: Failed to push to local docker registry: {e}");
69-
Environment.ExitCode = -1;
70-
}
79+
outputReg?.Push(img, imageName, tag, imageName, (message) => Console.WriteLine($"Containerize: {message}")).Wait();
80+
Console.WriteLine($"Containerize: Pushed container '{imageName}:{tag}' to registry '{outputRegistry}'");
7181
}
72-
else
82+
catch (Exception e)
7383
{
74-
try
75-
{
76-
outputReg?.Push(img, imageName, tag, imageName, (message) => Console.WriteLine($"Containerize: {message}")).Wait();
77-
Console.WriteLine($"Containerize: Pushed container '{imageName}:{tag}' to registry '{outputRegistry}'");
78-
}
79-
catch (Exception e)
80-
{
81-
Console.WriteLine($"Containerize: error CONTAINER001: Failed to push to output registry: {e}");
82-
Environment.ExitCode = -1;
83-
}
84+
Console.WriteLine($"Containerize: error CONTAINER001: Failed to push to output registry: {e}");
85+
Environment.ExitCode = -1;
8486
}
8587
}
8688
}

Microsoft.NET.Build.Containers/Image.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ public void AddLayer(Layer l)
5656
{
5757
newLayers.Add(l);
5858

59-
manifest.layers.Add(new (l.Descriptor.MediaType, l.Descriptor.Size, l.Descriptor.Digest, l.Descriptor.Urls));
59+
manifest.layers.Add(new(l.Descriptor.MediaType, l.Descriptor.Size, l.Descriptor.Digest, l.Descriptor.Urls));
6060
config["rootfs"]!["diff_ids"]!.AsArray().Add(l.Descriptor.UncompressedDigest);
6161
RecalculateDigest();
6262
}
6363

6464
private void RecalculateDigest()
6565
{
6666
config["created"] = DateTime.UtcNow;
67-
manifest = manifest with { config = manifest.config with { digest = GetDigest(config), size = Encoding.UTF8.GetBytes(config.ToJsonString()).Length } };
67+
var newManifestConfig = manifest.config with
68+
{
69+
digest = GetDigest(config),
70+
size = Encoding.UTF8.GetBytes(config.ToJsonString()).Length
71+
};
72+
manifest.config = newManifestConfig;
6873
}
6974

7075
private JsonObject CreatePortMap()

Microsoft.NET.Build.Containers/Registry.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ public record struct PlatformInformation(string architecture, string os, string?
2727
public record struct PlatformSpecificManifest(string mediaType, long size, string digest, PlatformInformation platform);
2828
public record struct ManifestListV2(int schemaVersion, string mediaType, PlatformSpecificManifest[] manifests);
2929

30+
3031
public record Registry(Uri BaseUri)
3132
{
3233
private const string DockerManifestV2 = "application/vnd.docker.distribution.manifest.v2+json";
3334
private const string DockerManifestListV2 = "application/vnd.docker.distribution.manifest.list.v2+json";
3435
private const string DockerContainerV1 = "application/vnd.docker.container.image.v1+json";
3536
private const int MaxChunkSizeBytes = 1024 * 64;
3637

38+
public static string[] SupportedRuntimeIdentifiers = new [] {
39+
"linux-x86",
40+
"linux-x64",
41+
"linux-arm",
42+
"linux-arm64",
43+
"win-x64"
44+
};
45+
3746
private string RegistryName { get; } = BaseUri.Host;
3847

3948
public async Task<Image?> GetImageManifest(string name, string reference, string runtimeIdentifier)
@@ -44,7 +53,7 @@ public record Registry(Uri BaseUri)
4453
return initialManifestResponse.Content.Headers.ContentType?.MediaType switch {
4554
DockerManifestV2 => await TryReadSingleImage(await initialManifestResponse.Content.ReadFromJsonAsync<ManifestV2>()),
4655
DockerManifestListV2 => await TryPickBestImageFromManifestList(await initialManifestResponse.Content.ReadFromJsonAsync<ManifestListV2>(), runtimeIdentifier),
47-
var unknownMediaType => throw new NotImplementedException($"Do not understand the mediaType {unknownMediaType}")
56+
var unknownMediaType => throw new NotImplementedException($"The manifest for {name}:{reference} from registry {BaseUri} was an unknown type: {unknownMediaType}. Please raise an issue at https://github.com/dotnet/sdk-container-builds/issues with this message.")
4857
};
4958

5059
async Task<HttpResponseMessage> GetManifest(string reference) {
@@ -84,7 +93,7 @@ async Task<HttpResponseMessage> GetBlob(string digest) {
8493
["linux", "arm"] => ("linux", "arm", "v7"),
8594
["linux", "arm64"] => ("linux", "arm64", "v8"),
8695
["win", "x64"] => ("windows", "amd64", null),
87-
var parts => throw new ArgumentException($"Unknown OS/platform combination {String.Join(' ', parts)}")
96+
var parts => throw new ArgumentException($"The runtimeIdentifier '{runtimeIdentifier}' is not supported. The supported RuntimeIdentifiers are {Registry.SupportedRuntimeIdentifiers}.")
8897
};
8998

9099
var potentialManifest = manifestList.manifests.SingleOrDefault(manifest => manifest.platform.os == os && manifest.platform.architecture == arch && manifest.platform.variant == variant);
@@ -95,7 +104,6 @@ async Task<HttpResponseMessage> GetBlob(string digest) {
95104
return null;
96105
}
97106
}
98-
99107
}
100108

101109
/// <summary>

0 commit comments

Comments
 (0)