Пример #1
0
        private async Task ProcessManifestsAsync(
            IAcrClient acrClient, List <string> deletedImages, List <string> deletedRepos, Repository repository,
            Func <ManifestAttributes, bool> canDeleteManifest)
        {
            _loggerService.WriteMessage($"Querying manifests for repo '{repository.Name}'");
            RepositoryManifests repoManifests = await acrClient.GetRepositoryManifestsAsync(repository.Name);

            _loggerService.WriteMessage($"Finished querying manifests for repo '{repository.Name}'. Manifest count: {repoManifests.Manifests.Count}");

            if (!repoManifests.Manifests.Any())
            {
                await DeleteRepositoryAsync(acrClient, deletedRepos, repository);

                return;
            }

            ManifestAttributes[] expiredTestImages = repoManifests.Manifests
                                                     .Where(manifest => canDeleteManifest(manifest))
                                                     .ToArray();

            // If all the images in the repo are expired, delete the whole repo instead of
            // deleting each individual image.
            if (expiredTestImages.Length == repoManifests.Manifests.Count)
            {
                await DeleteRepositoryAsync(acrClient, deletedRepos, repository);

                return;
            }

            await DeleteManifestsAsync(acrClient, deletedImages, repository, expiredTestImages);
        }
Пример #2
0
        private async Task DeleteRepositoryAsync(IAcrClient acrClient, List <string> deletedRepos, Repository repository)
        {
            string[] manifestsDeleted;
            string[] tagsDeleted;

            ManifestAttributes[] manifests = (await acrClient.GetRepositoryManifestsAsync(repository.Name)).Manifests.ToArray();

            if (!Options.IsDryRun)
            {
                DeleteRepositoryResponse deleteResponse =
                    await acrClient.DeleteRepositoryAsync(repository.Name);

                manifestsDeleted = deleteResponse.ManifestsDeleted;
                tagsDeleted      = deleteResponse.TagsDeleted;
            }
            else
            {
                manifestsDeleted = manifests
                                   .Select(manifest => manifest.Digest)
                                   .ToArray();

                tagsDeleted = manifests
                              .SelectMany(manifest => manifest.Tags)
                              .ToArray();
            }

            StringBuilder messageBuilder = new StringBuilder();

            messageBuilder.AppendLine($"Deleted repository '{repository.Name}'");
            messageBuilder.AppendLine($"\tIncluded manifests:");
            foreach (string manifest in manifestsDeleted.OrderBy(manifest => manifest))
            {
                messageBuilder.AppendLine($"\t{manifest}");
            }

            messageBuilder.AppendLine();
            messageBuilder.AppendLine($"\tIncluded tags:");
            foreach (string tag in tagsDeleted.OrderBy(tag => tag))
            {
                messageBuilder.AppendLine($"\t{tag}");
            }

            _loggerService.WriteMessage(messageBuilder.ToString());

            lock (deletedRepos)
            {
                deletedRepos.Add(repository.Name);
            }
        }