Skip to content

Commit 7acc5e9

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
refactor ImagePublisher
1 parent ec26765 commit 7acc5e9

File tree

3 files changed

+45
-112
lines changed

3 files changed

+45
-112
lines changed

src/Containers/Microsoft.NET.Build.Containers/ImagePublisher.cs

Lines changed: 43 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace Microsoft.NET.Build.Containers;
88

99
internal static class ImagePublisher
1010
{
11-
public static async Task PublishImage(
12-
BuiltImage image,
11+
public static async Task PublishImageAsync(
12+
BuiltImage singleArchImage,
1313
SourceImageReference sourceImageReference,
1414
DestinationImageReference destinationImageReference,
1515
Microsoft.Build.Utilities.TaskLoggingHelper Log,
@@ -23,22 +23,26 @@ public static async Task PublishImage(
2323
{
2424
case DestinationImageReferenceKind.LocalRegistry:
2525
await PushToLocalRegistryAsync(
26-
image,
26+
singleArchImage,
2727
sourceImageReference,
2828
destinationImageReference,
2929
Log,
3030
BuildEngine,
3131
telemetry,
32-
cancellationToken).ConfigureAwait(false);
32+
cancellationToken,
33+
destinationImageReference.LocalRegistry!.LoadAsync,
34+
Strings.ContainerBuilder_ImageUploadedToLocalDaemon).ConfigureAwait(false);
3335
break;
3436
case DestinationImageReferenceKind.RemoteRegistry:
3537
await PushToRemoteRegistryAsync(
36-
image,
38+
singleArchImage,
3739
sourceImageReference,
3840
destinationImageReference,
3941
Log,
4042
BuildEngine,
41-
cancellationToken).ConfigureAwait(false);
43+
cancellationToken,
44+
destinationImageReference.RemoteRegistry!.PushAsync,
45+
Strings.ContainerBuilder_ImageUploadedToRegistry).ConfigureAwait(false);
4246
break;
4347
default:
4448
throw new ArgumentOutOfRangeException();
@@ -47,8 +51,8 @@ await PushToRemoteRegistryAsync(
4751
telemetry.LogPublishSuccess();
4852
}
4953

50-
public static async Task PublishImage(
51-
BuiltImage[] images,
54+
public static async Task PublishImageAsync(
55+
BuiltImage[] multiArchImage,
5256
SourceImageReference sourceImageReference,
5357
DestinationImageReference destinationImageReference,
5458
Microsoft.Build.Utilities.TaskLoggingHelper Log,
@@ -62,22 +66,35 @@ public static async Task PublishImage(
6266
{
6367
case DestinationImageReferenceKind.LocalRegistry:
6468
await PushToLocalRegistryAsync(
65-
images,
69+
multiArchImage,
6670
sourceImageReference,
6771
destinationImageReference,
6872
Log,
6973
BuildEngine,
7074
telemetry,
71-
cancellationToken).ConfigureAwait(false);
75+
cancellationToken,
76+
destinationImageReference.LocalRegistry!.LoadAsync,
77+
Strings.ContainerBuilder_ImageUploadedToLocalDaemon).ConfigureAwait(false);
7278
break;
7379
case DestinationImageReferenceKind.RemoteRegistry:
7480
await PushToRemoteRegistryAsync(
75-
images,
81+
multiArchImage,
7682
sourceImageReference,
7783
destinationImageReference,
7884
Log,
7985
BuildEngine,
80-
cancellationToken).ConfigureAwait(false);
86+
cancellationToken,
87+
async (images, source, destination, token) =>
88+
{
89+
(string imageIndex, string mediaType) = ImageIndexGenerator.GenerateImageIndex(images);
90+
await destinationImageReference.RemoteRegistry!.PushManifestListAsync(
91+
destinationImageReference.Repository,
92+
destinationImageReference.Tags,
93+
imageIndex,
94+
mediaType,
95+
cancellationToken).ConfigureAwait(false);
96+
},
97+
Strings.ImageIndexUploadedToRegistry).ConfigureAwait(false);
8198
break;
8299
default:
83100
throw new ArgumentOutOfRangeException();
@@ -86,14 +103,16 @@ await PushToRemoteRegistryAsync(
86103
telemetry.LogPublishSuccess();
87104
}
88105

89-
private static async Task PushToLocalRegistryAsync(
90-
BuiltImage image,
106+
private static async Task PushToLocalRegistryAsync<T>(
107+
T image,
91108
SourceImageReference sourceImageReference,
92109
DestinationImageReference destinationImageReference,
93110
Microsoft.Build.Utilities.TaskLoggingHelper Log,
94111
IBuildEngine? BuildEngine,
95112
Telemetry telemetry,
96-
CancellationToken cancellationToken)
113+
CancellationToken cancellationToken,
114+
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> loadFunc,
115+
string successMessage)
97116
{
98117
ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
99118
if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
@@ -104,10 +123,10 @@ private static async Task PushToLocalRegistryAsync(
104123
}
105124
try
106125
{
107-
await localRegistry.LoadAsync(image, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
126+
await loadFunc(image, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
108127
if (BuildEngine != null)
109128
{
110-
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
129+
Log.LogMessage(MessageImportance.High, successMessage, destinationImageReference, localRegistry);
111130
}
112131
}
113132
catch (ContainerHttpException e)
@@ -128,112 +147,26 @@ private static async Task PushToLocalRegistryAsync(
128147
}
129148
}
130149

131-
private static async Task PushToLocalRegistryAsync(
132-
BuiltImage[] images,
150+
private static async Task PushToRemoteRegistryAsync<T>(
151+
T image,
133152
SourceImageReference sourceImageReference,
134153
DestinationImageReference destinationImageReference,
135154
Microsoft.Build.Utilities.TaskLoggingHelper Log,
136155
IBuildEngine? BuildEngine,
137-
Telemetry telemetry,
138-
CancellationToken cancellationToken)
156+
CancellationToken cancellationToken,
157+
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> pushFunc,
158+
string successMessage)
139159
{
140-
ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
141-
if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
142-
{
143-
telemetry.LogMissingLocalBinary();
144-
Log.LogErrorWithCodeFromResources(nameof(Strings.LocalRegistryNotAvailable));
145-
return;
146-
}
147160
try
148161
{
149-
await localRegistry.LoadAsync(images, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
150-
if (BuildEngine != null)
151-
{
152-
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
153-
}
154-
}
155-
catch (ContainerHttpException e)
156-
{
157-
if (BuildEngine != null)
158-
{
159-
Log.LogErrorFromException(e, true);
160-
}
161-
}
162-
catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
163-
{
164-
telemetry.LogLocalLoadError();
165-
Log.LogErrorFromException(dle, showStackTrace: false);
166-
}
167-
catch (ArgumentException argEx)
168-
{
169-
Log.LogErrorFromException(argEx, showStackTrace: false);
170-
}
171-
}
172-
173-
private static async Task PushToRemoteRegistryAsync(
174-
BuiltImage image,
175-
SourceImageReference sourceImageReference,
176-
DestinationImageReference destinationImageReference,
177-
Microsoft.Build.Utilities.TaskLoggingHelper Log,
178-
IBuildEngine? BuildEngine,
179-
CancellationToken cancellationToken)
180-
{
181-
try
182-
{
183-
await destinationImageReference.RemoteRegistry!.PushAsync(
162+
await pushFunc(
184163
image,
185164
sourceImageReference,
186165
destinationImageReference,
187166
cancellationToken).ConfigureAwait(false);
188167
if (BuildEngine != null)
189168
{
190-
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToRegistry, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
191-
}
192-
}
193-
catch (UnableToAccessRepositoryException)
194-
{
195-
if (BuildEngine != null)
196-
{
197-
Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
198-
}
199-
}
200-
catch (ContainerHttpException e)
201-
{
202-
if (BuildEngine != null)
203-
{
204-
Log.LogErrorFromException(e, true);
205-
}
206-
}
207-
catch (Exception e)
208-
{
209-
if (BuildEngine != null)
210-
{
211-
Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
212-
Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
213-
}
214-
}
215-
}
216-
217-
private static async Task PushToRemoteRegistryAsync(
218-
BuiltImage[] images,
219-
SourceImageReference sourceImageReference,
220-
DestinationImageReference destinationImageReference,
221-
Microsoft.Build.Utilities.TaskLoggingHelper Log,
222-
IBuildEngine? BuildEngine,
223-
CancellationToken cancellationToken)
224-
{
225-
try
226-
{
227-
(string imageIndex, string mediaType) = ImageIndexGenerator.GenerateImageIndex(images);
228-
await destinationImageReference.RemoteRegistry!.PushManifestListAsync(
229-
destinationImageReference.Repository,
230-
destinationImageReference.Tags,
231-
imageIndex,
232-
mediaType,
233-
cancellationToken).ConfigureAwait(false);
234-
if (BuildEngine != null)
235-
{
236-
Log.LogMessage(MessageImportance.High, Strings.ImageIndexUploadedToRegistry, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
169+
Log.LogMessage(MessageImportance.High, successMessage, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
237170
}
238171
}
239172
catch (UnableToAccessRepositoryException)

src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateImageIndex.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ internal async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
155155

156156
var telemetry = new Telemetry(sourceImageReference, destinationImageReference, Log);
157157

158-
await ImagePublisher.PublishImage(images, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
158+
await ImagePublisher.PublishImageAsync(images, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
159159
.ConfigureAwait(false);
160160

161161
return !Log.HasLoggedErrors;

src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
178178
GeneratedContainerMediaType = builtImage.ManifestMediaType;
179179
GeneratedContainerNames = destinationImageReference.FullyQualifiedImageNames().Select(name => new Microsoft.Build.Utilities.TaskItem(name)).ToArray();
180180

181-
await ImagePublisher.PublishImage(builtImage, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
181+
await ImagePublisher.PublishImageAsync(builtImage, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
182182
.ConfigureAwait(false);
183183

184184
return !Log.HasLoggedErrors;

0 commit comments

Comments
 (0)