From bb98ae0123b150a53ba1cd49097edbae20f221ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Mon, 28 Oct 2024 20:37:15 +0100 Subject: [PATCH] Refactor SourceImageReference --- .../ContainerBuilder.cs | 3 +-- .../SourceImageReference.cs | 12 +++++++++--- .../Tasks/CreateNewImage.Interface.cs | 1 - .../Tasks/CreateNewImage.cs | 3 +-- .../Tasks/ParseContainerProperties.cs | 3 +-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Containers/Microsoft.NET.Build.Containers/ContainerBuilder.cs b/src/Containers/Microsoft.NET.Build.Containers/ContainerBuilder.cs index a89bf37b3321..751c697e6caf 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/ContainerBuilder.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/ContainerBuilder.cs @@ -63,11 +63,10 @@ internal static async Task ContainerizeAsync( { try { - string imageReference = !string.IsNullOrEmpty(baseImageDigest) ? baseImageDigest : baseImageTag; var ridGraphPicker = new RidGraphManifestPicker(ridGraphPath); imageBuilder = await registry.GetImageManifestAsync( baseImageName, - imageReference, + sourceImageReference.Reference, containerRuntimeIdentifier, ridGraphPicker, cancellationToken).ConfigureAwait(false); diff --git a/src/Containers/Microsoft.NET.Build.Containers/SourceImageReference.cs b/src/Containers/Microsoft.NET.Build.Containers/SourceImageReference.cs index 8a37f9b5ca8e..12fb19b82f07 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/SourceImageReference.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/SourceImageReference.cs @@ -6,17 +6,22 @@ namespace Microsoft.NET.Build.Containers; /// /// Represents a reference to a Docker image. A reference is made of a registry, a repository (aka the image name) and a tag or digest. /// -internal readonly record struct SourceImageReference(Registry? Registry, string Repository, string Tag, string? Digest) +internal readonly record struct SourceImageReference(Registry? Registry, string Repository, string? Tag, string? Digest) { public override string ToString() { - string sourceImageReference = RepositoryAndTag; + string sourceImageReference = Repository; if (Registry is { } reg) { sourceImageReference = $"{reg.RegistryName}/{sourceImageReference}"; } + if (!string.IsNullOrEmpty(Tag)) + { + sourceImageReference = $"{sourceImageReference}:{Tag}"; + } + if (!string.IsNullOrEmpty(Digest)) { sourceImageReference = $"{sourceImageReference}@{Digest}"; @@ -28,5 +33,6 @@ public override string ToString() /// /// Returns the repository and tag as a formatted string. Used in cases /// - public readonly string RepositoryAndTag => $"{Repository}:{Tag}"; + public string Reference + => !string.IsNullOrEmpty(Digest) ? Digest : !string.IsNullOrEmpty(Tag) ? Tag : "latest"; } diff --git a/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.Interface.cs b/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.Interface.cs index a74adeaa0845..32371a5aeebf 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.Interface.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.Interface.cs @@ -34,7 +34,6 @@ partial class CreateNewImage /// The base image tag. /// Ex: 6.0 /// - [Required] public string BaseImageTag { get; set; } /// diff --git a/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs b/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs index 3f554025506f..39a337203271 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs @@ -79,11 +79,10 @@ internal async Task ExecuteAsync(CancellationToken cancellationToken) { try { - string imageReference = !string.IsNullOrEmpty(BaseImageDigest) ? BaseImageDigest : BaseImageTag; var picker = new RidGraphManifestPicker(RuntimeIdentifierGraphPath); imageBuilder = await registry.GetImageManifestAsync( BaseImageName, - imageReference, + sourceImageReference.Reference, ContainerRuntimeIdentifier, picker, cancellationToken).ConfigureAwait(false); diff --git a/src/Containers/Microsoft.NET.Build.Containers/Tasks/ParseContainerProperties.cs b/src/Containers/Microsoft.NET.Build.Containers/Tasks/ParseContainerProperties.cs index 26f22c49091a..bab45e1c1ce1 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Tasks/ParseContainerProperties.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Tasks/ParseContainerProperties.cs @@ -166,8 +166,7 @@ public override bool Execute() ParsedContainerRegistry = outputReg ?? ""; ParsedContainerImage = outputImage ?? ""; - // If no Tag was provided, default to "latest" - ParsedContainerTag = outputTag ?? "latest"; + ParsedContainerTag = outputTag ?? ""; ParsedContainerDigest = outputDigest ?? ""; NewContainerRegistry = ContainerRegistry; NewContainerTags = validTags;