Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/ImageBuilder.Tests/CommandLine/OptionsBindingTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using Microsoft.DotNet.ImageBuilder.Commands;

namespace Microsoft.DotNet.ImageBuilder.Tests.CommandLine;
Expand All @@ -22,7 +24,7 @@ internal static ParseResult Parse(Options options, string[] args)
{
Command command = new("test", "test");
command.AddOptions(options);
return command.Parse(args);
return command.Parse(GetParseArgs(options, args));
}

/// <summary>
Expand All @@ -37,4 +39,25 @@ internal static TOptions ParseAndBind<TOptions>(string[] args)
options.Bind(parseResult);
return options;
}

private static string[] GetParseArgs(Options options, string[] args)
{
if (options is not IFilterableOptions and not IPlatformFilterableOptions)
{
return args;
}

return
[
..args,
..GetOptionArgs(args, "--architecture", "amd64"),
..GetOptionArgs(args, "--os-type", "linux"),
];
}

private static string[] GetOptionArgs(string[] args, string optionName, string optionValue) =>
HasOption(args, optionName) ? [] : [optionName, optionValue];

private static bool HasOption(string[] args, string optionName) =>
args.Any(arg => arg == optionName || arg.StartsWith($"{optionName}=", StringComparison.Ordinal));
}
8 changes: 7 additions & 1 deletion src/ImageBuilder.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Linq;
using Microsoft.DotNet.ImageBuilder.Commands;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shouldly;

namespace Microsoft.DotNet.ImageBuilder.Tests;
Expand All @@ -13,7 +16,10 @@ public class DependencyInjectionTests
[TestMethod]
public void DependencyResolution()
{
var commands = ImageBuilder.Commands.ToArray();
using IHost host = ImageBuilder.CreateAppHost();

ICommand[] commands = host.Services.GetServices<ICommand>().ToArray();

commands.ShouldNotBeNull();
commands.ShouldNotBeEmpty();
}
Expand Down
240 changes: 117 additions & 123 deletions src/ImageBuilder/ImageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.DotNet.ImageBuilder.Commands;
using Microsoft.DotNet.ImageBuilder.Commands.Signing;
using Microsoft.DotNet.ImageBuilder.Configuration;
Expand All @@ -21,129 +20,124 @@ namespace Microsoft.DotNet.ImageBuilder;

public static class ImageBuilder
{
public static IEnumerable<ICommand> Commands => ServiceProvider.Value.GetServices<ICommand>();
internal static IServiceProvider Services => ServiceProvider.Value;
public static IHost CreateAppHost()
{
var builder = Host.CreateApplicationBuilder();

private static Lazy<IServiceProvider> ServiceProvider { get; } = new(() =>
{
var builder = Host.CreateApplicationBuilder();

// Configuration
builder.AddPublishConfiguration();
builder.AddBuildConfiguration();
// Configuration
builder.AddPublishConfiguration();
builder.AddBuildConfiguration();

// Logging
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
});

// Register abstractions
builder.Services.AddSingleton<IFileSystem, FileSystem>();

// Register services
builder.Services.AddSingleton<IAzdoGitHttpClientFactory, AzdoGitHttpClientFactory>();
builder.Services.AddSingleton<IAzureTokenCredentialProvider, AzureTokenCredentialProvider>();
builder.Services.AddSingleton<IAcrClientFactory, AcrClientFactory>();
builder.Services.AddSingleton<IAcrContentClientFactory, AcrContentClientFactory>();
builder.Services.AddSingleton<IAcrImageImporter, AcrImageImporter>();
builder.Services.AddSingleton<ICopyImageService, CopyImageService>();
builder.Services.AddSingleton<IDateTimeService, DateTimeService>();
builder.Services.AddSingleton<IDockerService, DockerService>();
builder.Services.AddSingleton<IEnvironmentService, EnvironmentService>();
builder.Services.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
builder.Services.AddSingleton<IGitService, GitService>();

// Add ACR rate limiting:
// Singleton limiter holds the rate limiting state.
builder.Services.AddSingleton(_ => new AcrRateLimiter());
// Transient handler gets instantiated for each client and uses the singleton rate limiter.
builder.Services.AddTransient<AcrRateLimitingHandler>();

builder.Services.ConfigureHttpClientDefaults(httpClientBuilder =>
// Logging
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
});

// Register abstractions
builder.Services.AddSingleton<IFileSystem, FileSystem>();

// Register services
builder.Services.AddSingleton<IAzdoGitHttpClientFactory, AzdoGitHttpClientFactory>();
builder.Services.AddSingleton<IAzureTokenCredentialProvider, AzureTokenCredentialProvider>();
builder.Services.AddSingleton<IAcrClientFactory, AcrClientFactory>();
builder.Services.AddSingleton<IAcrContentClientFactory, AcrContentClientFactory>();
builder.Services.AddSingleton<IAcrImageImporter, AcrImageImporter>();
builder.Services.AddSingleton<ICopyImageService, CopyImageService>();
builder.Services.AddSingleton<IDateTimeService, DateTimeService>();
builder.Services.AddSingleton<IDockerService, DockerService>();
builder.Services.AddSingleton<IEnvironmentService, EnvironmentService>();
builder.Services.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
builder.Services.AddSingleton<IGitService, GitService>();

// Add ACR rate limiting:
// Singleton limiter holds the rate limiting state.
builder.Services.AddSingleton(_ => new AcrRateLimiter());
// Transient handler gets instantiated for each client and uses the singleton rate limiter.
builder.Services.AddTransient<AcrRateLimitingHandler>();

builder.Services.ConfigureHttpClientDefaults(httpClientBuilder =>
{
var retryOptions = new HttpRetryStrategyOptions()
{
var retryOptions = new HttpRetryStrategyOptions()
{
MaxRetryAttempts = 5,
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromSeconds(3),
UseJitter = true,
};
// Don't retry for requests that might not be idempotent
retryOptions.DisableForUnsafeHttpMethods();

// Retry should be the outer-most policy
httpClientBuilder.AddResilienceHandler(
"image-builder-retry",
pipeline => pipeline.AddRetry(retryOptions));

// ACR rate limiting must be per-retry-attempt, otherwise retries would eat up rate
// limited requests without acquiring additional rate limiting leases/tokens.
httpClientBuilder.AddHttpMessageHandler<AcrRateLimitingHandler>();

// Per-request timeout covers individual requests, and doesn't apply to the outer
// retry policy.
httpClientBuilder.AddResilienceHandler(
"image-builder-timeout",
pipeline => pipeline.AddTimeout(TimeSpan.FromSeconds(10)));
});

builder.Services.AddSingleton<IImageCacheService, ImageCacheService>();
builder.Services.AddSingleton<IKustoClient, KustoClientWrapper>();
builder.Services.AddSingleton<ILifecycleMetadataService, LifecycleMetadataService>();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<IManifestJsonService, ManifestJsonService>();
builder.Services.AddSingleton<IManifestServiceFactory, ManifestServiceFactory>();
builder.Services.AddSingleton<IMarImageIngestionReporter, MarImageIngestionReporter>();
builder.Services.AddSingleton<IMcrStatusClientFactory, McrStatusClientFactory>();
builder.Services.AddSingleton<INotificationService, NotificationService>();
builder.Services.AddSingleton<Notation.INotationClient, Notation.NotationClient>();
builder.Services.AddSingleton<IOctokitClientFactory, OctokitClientFactory>();
builder.Services.AddSingleton<Oras.IOrasService, Oras.OrasDotNetService>();
builder.Services.AddSingleton<Oras.IOrasServiceFactory, Oras.OrasServiceFactory>();
builder.Services.AddSingleton<IProcessService, ProcessService>();
builder.Services.AddSingleton<IRegistryResolver, RegistryResolver>();
builder.Services.AddSingleton<IRegistryCredentialsProvider, RegistryCredentialsProvider>();
builder.Services.AddSingleton<IVssConnectionFactory, VssConnectionFactory>();

builder.Services.AddSingleton<IEsrpSigningService, EsrpSigningService>();
builder.Services.AddSingleton<IImageSigningService, ImageSigningService>();

// Commands
builder.Services.AddSingleton<ICommand, AnnotateEolDigestsCommand>();
builder.Services.AddSingleton<ICommand, BuildCommand>();
builder.Services.AddSingleton<ICommand, CleanAcrImagesCommand>();
builder.Services.AddSingleton<ICommand, CopyAcrImagesCommand>();
builder.Services.AddSingleton<ICommand, CopyBaseImagesCommand>();
builder.Services.AddSingleton<ICommand, CreateManifestListCommand>();
builder.Services.AddSingleton<ICommand, GenerateBuildMatrixCommand>();
builder.Services.AddSingleton<ICommand, GenerateDockerfilesCommand>();
builder.Services.AddSingleton<ICommand, GenerateEolAnnotationDataForAllImagesCommand>();
builder.Services.AddSingleton<ICommand, GenerateEolAnnotationDataForPublishCommand>();
builder.Services.AddSingleton<ICommand, GenerateReadmesCommand>();
builder.Services.AddSingleton<ICommand, GetBaseImageStatusCommand>();
builder.Services.AddSingleton<ICommand, GetStaleImagesCommand>();
builder.Services.AddSingleton<ICommand, IngestKustoImageInfoCommand>();
builder.Services.AddSingleton<ICommand, MergeImageInfoCommand>();
builder.Services.AddSingleton<ICommand, PostPublishNotificationCommand>();
builder.Services.AddSingleton<ICommand, PublishImageInfoCommand>();
builder.Services.AddSingleton<ICommand, PublishMcrDocsCommand>();
builder.Services.AddSingleton<ICommand, PullImagesCommand>();
builder.Services.AddSingleton<ICommand, QueueBuildCommand>();
builder.Services.AddSingleton<ICommand, ShowImageStatsCommand>();
builder.Services.AddSingleton<ICommand, ShowManifestSchemaCommand>();
builder.Services.AddSingleton<ICommand, SignImagesCommand>();
builder.Services.AddSingleton<ICommand, TrimUnchangedPlatformsCommand>();
builder.Services.AddSingleton<ICommand, VerifySignaturesCommand>();
builder.Services.AddSingleton<ICommand, WaitForMarAnnotationIngestionCommand>();
builder.Services.AddSingleton<ICommand, WaitForMcrDocIngestionCommand>();
builder.Services.AddSingleton<ICommand, WaitForMcrImageIngestionCommand>();

var host = builder.Build();
return host.Services;
}
);
MaxRetryAttempts = 5,
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromSeconds(3),
UseJitter = true,
};
// Don't retry for requests that might not be idempotent
retryOptions.DisableForUnsafeHttpMethods();

// Retry should be the outer-most policy
httpClientBuilder.AddResilienceHandler(
"image-builder-retry",
pipeline => pipeline.AddRetry(retryOptions));

// ACR rate limiting must be per-retry-attempt, otherwise retries would eat up rate
// limited requests without acquiring additional rate limiting leases/tokens.
httpClientBuilder.AddHttpMessageHandler<AcrRateLimitingHandler>();

// Per-request timeout covers individual requests, and doesn't apply to the outer
// retry policy.
httpClientBuilder.AddResilienceHandler(
"image-builder-timeout",
pipeline => pipeline.AddTimeout(TimeSpan.FromSeconds(10)));
});

builder.Services.AddSingleton<IImageCacheService, ImageCacheService>();
builder.Services.AddSingleton<IKustoClient, KustoClientWrapper>();
builder.Services.AddSingleton<ILifecycleMetadataService, LifecycleMetadataService>();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<IManifestJsonService, ManifestJsonService>();
builder.Services.AddSingleton<IManifestServiceFactory, ManifestServiceFactory>();
builder.Services.AddSingleton<IMarImageIngestionReporter, MarImageIngestionReporter>();
builder.Services.AddSingleton<IMcrStatusClientFactory, McrStatusClientFactory>();
builder.Services.AddSingleton<INotificationService, NotificationService>();
builder.Services.AddSingleton<Notation.INotationClient, Notation.NotationClient>();
builder.Services.AddSingleton<IOctokitClientFactory, OctokitClientFactory>();
builder.Services.AddSingleton<Oras.IOrasService, Oras.OrasDotNetService>();
builder.Services.AddSingleton<Oras.IOrasServiceFactory, Oras.OrasServiceFactory>();
builder.Services.AddSingleton<IProcessService, ProcessService>();
builder.Services.AddSingleton<IRegistryResolver, RegistryResolver>();
builder.Services.AddSingleton<IRegistryCredentialsProvider, RegistryCredentialsProvider>();
builder.Services.AddSingleton<IVssConnectionFactory, VssConnectionFactory>();

builder.Services.AddSingleton<IEsrpSigningService, EsrpSigningService>();
builder.Services.AddSingleton<IImageSigningService, ImageSigningService>();

// Commands
builder.Services.AddSingleton<ICommand, AnnotateEolDigestsCommand>();
builder.Services.AddSingleton<ICommand, BuildCommand>();
builder.Services.AddSingleton<ICommand, CleanAcrImagesCommand>();
builder.Services.AddSingleton<ICommand, CopyAcrImagesCommand>();
builder.Services.AddSingleton<ICommand, CopyBaseImagesCommand>();
builder.Services.AddSingleton<ICommand, CreateManifestListCommand>();
builder.Services.AddSingleton<ICommand, GenerateBuildMatrixCommand>();
builder.Services.AddSingleton<ICommand, GenerateDockerfilesCommand>();
builder.Services.AddSingleton<ICommand, GenerateEolAnnotationDataForAllImagesCommand>();
builder.Services.AddSingleton<ICommand, GenerateEolAnnotationDataForPublishCommand>();
builder.Services.AddSingleton<ICommand, GenerateReadmesCommand>();
builder.Services.AddSingleton<ICommand, GetBaseImageStatusCommand>();
builder.Services.AddSingleton<ICommand, GetStaleImagesCommand>();
builder.Services.AddSingleton<ICommand, IngestKustoImageInfoCommand>();
builder.Services.AddSingleton<ICommand, MergeImageInfoCommand>();
builder.Services.AddSingleton<ICommand, PostPublishNotificationCommand>();
builder.Services.AddSingleton<ICommand, PublishImageInfoCommand>();
builder.Services.AddSingleton<ICommand, PublishMcrDocsCommand>();
builder.Services.AddSingleton<ICommand, PullImagesCommand>();
builder.Services.AddSingleton<ICommand, QueueBuildCommand>();
builder.Services.AddSingleton<ICommand, ShowImageStatsCommand>();
builder.Services.AddSingleton<ICommand, ShowManifestSchemaCommand>();
builder.Services.AddSingleton<ICommand, SignImagesCommand>();
builder.Services.AddSingleton<ICommand, TrimUnchangedPlatformsCommand>();
builder.Services.AddSingleton<ICommand, VerifySignaturesCommand>();
builder.Services.AddSingleton<ICommand, WaitForMarAnnotationIngestionCommand>();
builder.Services.AddSingleton<ICommand, WaitForMcrDocIngestionCommand>();
builder.Services.AddSingleton<ICommand, WaitForMcrImageIngestionCommand>();

return builder.Build();
}
}
9 changes: 5 additions & 4 deletions src/ImageBuilder/Mcr/McrTagsMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace Microsoft.DotNet.ImageBuilder.Mcr
{
public class McrTagsMetadataGenerator
{
private static readonly ILogger<McrTagsMetadataGenerator> _logger = StandaloneLoggerFactory.CreateLogger<McrTagsMetadataGenerator>();
private static ILogger<McrTagsMetadataGenerator> Logger =>
StandaloneLoggerFactory.CreateLogger<McrTagsMetadataGenerator>();

private IGitService _gitService;
private ManifestInfo _manifest;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static string Execute(

private string Execute()
{
_logger.LogInformation("GENERATING MCR TAGS METADATA");
Logger.LogInformation("GENERATING MCR TAGS METADATA");

_imageDocInfos = _repo.FilteredImages
.SelectMany(image => image.AllPlatforms
Expand Down Expand Up @@ -100,8 +101,8 @@ private string Execute()

string metadata = yaml.ToString();

_logger.LogInformation("Generated Metadata:");
_logger.LogInformation(metadata);
Logger.LogInformation("Generated Metadata:");
Logger.LogInformation(metadata);

// Validate that the YAML is in a valid format
new DeserializerBuilder()
Expand Down
Loading
Loading