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."); } } }
public async Task <ContainerImage> GetContainerImageByTag(string repository, string tag) { var normalizedContainerRepository = NormalizeContainerRepository(repository); var r = await _containerImageRepository.Query() .FirstOrDefaultAsync(x => x.Name.Equals(normalizedContainerRepository)); var containerImageTags = await _containerImageTagRepository.Query() .Where(x => x.RepositoryId == r.Id && x.Tag == tag) .Include(x => x.Metadata) .ToListAsync(); if (containerImageTags.Any()) { return(containerImageTags .Select( x => new ContainerImage( r.Name, x.Tag, x.Metadata.Hash, DateTime.SpecifyKind(x.Metadata.CreatedDateTime, DateTimeKind.Utc) ) ) .First()); } // we did not find the tag into the local cache, try to get the tag information from remote var client = await _registryClientPool.GetRegistryClientForRepository(repository); var remoteContainerRepositoryTags = await client.GetRepositoryTags(repository); var remoteContainerImage = remoteContainerRepositoryTags.FirstOrDefault(x => x.Tag == tag); if (remoteContainerImage != null) { await AddOrUpdate(remoteContainerImage); return(remoteContainerImage); } throw new InvalidOperationException("Could not find container image tag"); }