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
14 changes: 7 additions & 7 deletions src/ImageBuilder.Tests/CleanAcrImagesCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task StagingRepos()
IAcrClientFactory acrClientFactory = CreateAcrClientFactory(AcrName, acrClientMock.Object);

CleanAcrImagesCommand command = new(
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "build-staging/*";
command.Options.Action = CleanAcrImagesAction.Delete;
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task PublicNightlyRepos()
AcrName, [repo1ContentClient, repo2ContentClient, repo3ContentClient, repo4ContentClient]);

CleanAcrImagesCommand command = new(
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "public/dotnet/*nightly/*";
command.Options.Action = CleanAcrImagesAction.PruneDangling;
Expand Down Expand Up @@ -166,7 +166,7 @@ public async Task DeleteEmptyTestRepo()
IAcrClientFactory acrClientFactory = CreateAcrClientFactory(AcrName, acrClientMock.Object);

CleanAcrImagesCommand command = new(
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "test/*";
command.Options.Action = CleanAcrImagesAction.PruneAll;
Expand Down Expand Up @@ -205,7 +205,7 @@ public async Task DeleteAllExpiredImagesTestRepo()
IAcrClientFactory acrClientFactory = CreateAcrClientFactory(AcrName, acrClientMock.Object);

CleanAcrImagesCommand command = new CleanAcrImagesCommand(
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, Mock.Of<IAcrContentClientFactory>(), Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "test/*";
command.Options.Action = CleanAcrImagesAction.PruneAll;
Expand Down Expand Up @@ -254,7 +254,7 @@ public async Task TestRepos()
IAcrContentClientFactory acrContentClientFactory = CreateAcrContentClientFactory(AcrName, [repo1ContentClientMock, repo2ContentClientMock]);

CleanAcrImagesCommand command = new CleanAcrImagesCommand(
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "test/*";
command.Options.Action = CleanAcrImagesAction.PruneAll;
Expand Down Expand Up @@ -311,7 +311,7 @@ public async Task DeleteEolImages()
Mock<ILifecycleMetadataService> lifecycleMetadataServiceMock = CreateLifecycleMetadataServiceMock(age, repo1Name);

CleanAcrImagesCommand command = new CleanAcrImagesCommand(
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), lifecycleMetadataServiceMock.Object, Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), lifecycleMetadataServiceMock.Object, Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "test/*";
command.Options.Action = CleanAcrImagesAction.PruneEol;
Expand Down Expand Up @@ -361,7 +361,7 @@ public async Task ExcludedImages()
AcrName, [repo1ContentClient, repo2ContentClient]);

CleanAcrImagesCommand command = new(
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
acrClientFactory, acrContentClientFactory, Mock.Of<ILogger<CleanAcrImagesCommand>>(), Mock.Of<ILifecycleMetadataService>(), Mock.Of<IRegistryCredentialsProvider>(), Microsoft.Extensions.Options.Options.Create(new PublishConfiguration()));
command.Options.RegistryName = AcrName;
command.Options.RepoName = "public/dotnet/nightly/*";
command.Options.Action = CleanAcrImagesAction.PruneAll;
Expand Down
32 changes: 21 additions & 11 deletions src/ImageBuilder/Commands/CleanAcrImagesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CleanAcrImagesCommand : Command<CleanAcrImagesOptions, CleanAcrImag
private readonly IAcrContentClientFactory _acrContentClientFactory;
private readonly ILogger<CleanAcrImagesCommand> _logger;
private readonly ILifecycleMetadataService _lifecycleMetadataService;
private readonly IRegistryCredentialsProvider _registryCredentialsProvider;
private readonly PublishConfiguration _publishConfig;

private const int MaxConcurrentDeleteRequestsPerRepo = 5;
Expand All @@ -34,12 +35,14 @@ public CleanAcrImagesCommand(
IAcrContentClientFactory acrContentClientFactory,
ILogger<CleanAcrImagesCommand> logger,
ILifecycleMetadataService lifecycleMetadataService,
IRegistryCredentialsProvider registryCredentialsProvider,
IOptions<PublishConfiguration> publishConfigOptions)
{
_acrClientFactory = acrClientFactory ?? throw new ArgumentNullException(nameof(acrClientFactory));
_acrContentClientFactory = acrContentClientFactory;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_lifecycleMetadataService = lifecycleMetadataService ?? throw new ArgumentNullException(nameof(lifecycleMetadataService));
_registryCredentialsProvider = registryCredentialsProvider ?? throw new ArgumentNullException(nameof(registryCredentialsProvider));
_publishConfig = publishConfigOptions.Value;
}

Expand Down Expand Up @@ -67,18 +70,25 @@ public override async Task ExecuteAsync()
List<string> deletedRepos = new List<string>();
List<string> deletedImages = new List<string>();

IEnumerable<Task> cleanupTasks = await repositoryNames
.Where(repoName => repoNameFilterRegex.IsMatch(repoName))
.Select(repoName => acrClient.GetRepository(repoName))
.Select(repo =>
await _registryCredentialsProvider.ExecuteWithCredentialsAsync(
isDryRun: false,
async () =>
{
Acr acr = Acr.Parse(Options.RegistryName);
IAcrContentClient acrContentClient = CreateAcrContentClient(acr, repo.Name);
return ProcessRepoAsync(acrClient, acrContentClient, repo, deletedRepos, deletedImages);
})
.ToArrayAsync();

await Task.WhenAll(cleanupTasks);
IEnumerable<Task> cleanupTasks = await repositoryNames
.Where(repoName => repoNameFilterRegex.IsMatch(repoName))
.Select(repoName => acrClient.GetRepository(repoName))
.Select(repo =>
{
Acr acr = Acr.Parse(Options.RegistryName);
IAcrContentClient acrContentClient = CreateAcrContentClient(acr, repo.Name);
return ProcessRepoAsync(acrClient, acrContentClient, repo, deletedRepos, deletedImages);
})
.ToArrayAsync();

await Task.WhenAll(cleanupTasks);
},
Options.CredentialsOptions,
registryName: Options.RegistryName);

await LogSummaryAsync(acrClient, deletedRepos, deletedImages);
}
Expand Down
Loading