Beispiel #1
0
        public override async Task Execute(ContainerImageRepositoryPollingJobContext context)
        {
            var allContainerImages = (await _containerImageMetadataService
                                      .GetTagsForRepository(context.ContainerImageRepository))
                                     .ToList();


            IEnumerable <ContainerImage> filter = allContainerImages.OrderByDescending(x => x.CreationDateTime);

            if (LastContainerImageCheck.TryGetValue(context.ContainerImageRepository, out var lastCheckTime))
            {
                filter = filter.Where(x => x.CreationDateTime > lastCheckTime);
            }

            var newContainerImages = filter.ToList();

            if (newContainerImages.Any())
            {
                LastContainerImageCheck[context.ContainerImageRepository] = newContainerImages.First().CreationDateTime;

                await _deploymentWorkflowService.StartImageDeployment(
                    context.ContainerImageRepository,
                    newContainerImages,
                    true);
            }
        }
Beispiel #2
0
        public override async Task Execute(ContainerRepositoryPollingContext context)
        {
            var containerRepository = context.ContainerRepository;

            using (_log.BeginScope(new Dictionary <string, object>()
            {
                { "Repository", containerRepository }
            }))
            {
                try
                {
                    _log.LogTrace("Fetching tags for {imagerepository}", containerRepository);

                    var client = await _registryClientPool.GetRegistryClientForRepository(containerRepository);

                    var remoteContainerRepositoryTags = await client.GetRepositoryTags(containerRepository);

                    var localContainerRepositoryTags =
                        await _containerImageMetadataService.GetTagsForRepository(containerRepository);

                    // remove tags and container images that we already know about
                    var newOrUpdatedContainerRepositoryTagsQuery = remoteContainerRepositoryTags
                                                                   .Except(localContainerRepositoryTags);

                    // we don't need tags older than a month, so we'll truncate all the junk
                    newOrUpdatedContainerRepositoryTagsQuery = newOrUpdatedContainerRepositoryTagsQuery
                                                               .OrderByDescending(x => x.CreationDateTime)
                                                               .Where(x => x.CreationDateTime >= DateTimeOffset.Now.AddDays(-30))
                                                               .Take(20);

                    // ReSharper disable once PossibleMultipleEnumeration
                    var newOrUpdatedContainerRepositoryTags = newOrUpdatedContainerRepositoryTagsQuery.ToList();
                    if (newOrUpdatedContainerRepositoryTags.Any())
                    {
                        _log.LogInformation($"Adding '{newOrUpdatedContainerRepositoryTags.Count}' new image tags ...");
                        await _containerImageMetadataService.AddOrUpdate(newOrUpdatedContainerRepositoryTags);
                    }
                }
                catch (Exception e)
                {
                    _log.LogWarning(e, "An error occured when fetching the latest image tags from the registry.");
                }
            }
        }
Beispiel #3
0
        public async Task <ActionResult> GetContainerImageIntoLocalStore([FromQuery] string repository, [FromQuery] string pattern)
        {
            if (string.IsNullOrEmpty(repository))
            {
                return(BadRequest());
            }

            var containerImages = await _containerImageMetadataService.GetTagsForRepository(repository);

            if (string.IsNullOrWhiteSpace(pattern))
            {
                return(Ok(new
                {
                    Items = containerImages.Select(x => (ContainerImageDto)x).ToList()
                }));
            }

            var parts = pattern.Split(':');

            if (parts.Length == 0)
            {
                throw new InvalidOperationException("wtf?");
            }

            var actualPattern = pattern;

            if (!Enum.TryParse <UpdatePolicy>(parts[0], ignoreCase: true, out var policyType))
            {
                // first part is not a policy type, handle later
            }
            else
            {
                // since above we split the pattern with ':', let's assume that the type is part[0],
                // while the pattern is part[1..end].  If there are more than two parts, then the pattern contained
                // ':', thus we need to rebuild the pattern as is by joining all the parts together.
                actualPattern = string.Join(":", parts.Skip(1));
            }

            var policy = policyType switch
            {
                UpdatePolicy.Glob => (ImageUpdatePolicy) new GlobImageUpdatePolicy(actualPattern),
                UpdatePolicy.Regex => new RegexImageUpdatePolicy(actualPattern),
                UpdatePolicy.Semver => new SemverImageUpdatePolicy(actualPattern),
                _ => null
            };

            if (policy == null)
            {
                return(BadRequest());
            }

            return(Ok(new
            {
                policy = policy,
                Items = containerImages
                        .Where(x => policy.IsMatch(x.Tag))
                        .OrderByDescending(x => x.CreationDateTime)
                        .Select(x => (ContainerImageDto)x)
                        .ToList()
            }));
        }