Skip to content

Commit

Permalink
Fix RebuildStaleImages command to generate dependency graph (dotnet#217)
Browse files Browse the repository at this point in the history
Ensures that the set of paths produced by the RebuildStaleImages command consists of the image whose base image had changed as well as its dependent images.
  • Loading branch information
mthalman authored Jun 24, 2019
1 parent 617ce76 commit ae98840
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
.vscode/
.vs

# Visual Studio debug profile
# Visual Studio user-specific files
**/launchSettings.json
**/*.user
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ private async Task<IEnumerable<string>> GetPathsToRebuildAsync(Subscription subs

List<string> pathsToRebuild = new List<string>();

IEnumerable<PlatformInfo> allPlatforms = manifest.GetAllPlatforms().ToList();

foreach (RepoInfo repo in manifest.FilteredRepos)
{
IEnumerable<PlatformInfo> platforms = repo.FilteredImages
Expand All @@ -127,19 +129,28 @@ private async Task<IEnumerable<string>> GetPathsToRebuildAsync(Subscription subs
RepoData repoData = repos
.FirstOrDefault(s => s.Repo == repo.Model.Name);

HashSet<string> processedImages = new HashSet<string>();
foreach (var platform in platforms)
{
if (repoData != null &&
repoData.Images != null &&
repoData.Images.TryGetValue(platform.BuildContextPath, out ImageData imageData))
{
bool hasDigestChanged = false;

foreach (string fromImage in platform.ExternalFromImages)
{
if (processedImages.Contains(fromImage))
{
continue;
}

DockerHelper.PullImage(fromImage, Options.IsDryRun);
string currentDigest = DockerHelper.GetImageDigest(fromImage, Options.IsDryRun);
string lastDigest = imageData.BaseImages?[fromImage];

processedImages.Add(fromImage);

if (lastDigest != currentDigest)
{
hasDigestChanged = true;
Expand All @@ -149,7 +160,8 @@ private async Task<IEnumerable<string>> GetPathsToRebuildAsync(Subscription subs

if (hasDigestChanged)
{
pathsToRebuild.Add(platform.BuildContextPath);
IEnumerable<PlatformInfo> dependentPlatforms = platform.GetDependencyGraph(allPlatforms);
pathsToRebuild.AddRange(dependentPlatforms.Select(p => p.BuildContextPath));
}
}
else
Expand All @@ -160,7 +172,7 @@ private async Task<IEnumerable<string>> GetPathsToRebuildAsync(Subscription subs
}
}

return pathsToRebuild;
return pathsToRebuild.Distinct().ToList();
}

private async Task<string> GetGitRepoPath(Subscription sub)
Expand Down
10 changes: 7 additions & 3 deletions Microsoft.DotNet.ImageBuilder/src/ViewModel/ManifestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -71,7 +72,7 @@ private static ManifestInfo Create(string manifestPath, ManifestFilter manifestF
.ToArray();

IEnumerable<string> repoNames = manifestInfo.AllRepos.Select(repo => repo.Name).ToArray();
foreach (PlatformInfo platform in manifestInfo.AllRepos.SelectMany(repo => repo.AllImages).SelectMany(image => image.AllPlatforms))
foreach (PlatformInfo platform in manifestInfo.GetAllPlatforms())
{
platform.Initialize(repoNames, manifestInfo.Registry);
}
Expand All @@ -84,10 +85,13 @@ private static ManifestInfo Create(string manifestPath, ManifestFilter manifestF
return manifestInfo;
}

public IEnumerable<ImageInfo> GetAllImages() => AllRepos.SelectMany(repo => repo.AllImages);

public IEnumerable<PlatformInfo> GetAllPlatforms() => GetAllImages().SelectMany(image => image.AllPlatforms);

private IEnumerable<TagInfo> GetAllTags()
{
IEnumerable<ImageInfo> images = AllRepos
.SelectMany(repo => repo.AllImages)
IEnumerable<ImageInfo> images = GetAllImages()
.ToArray();
IEnumerable<TagInfo> sharedTags = images
.SelectMany(image => image.SharedTags);
Expand Down

0 comments on commit ae98840

Please sign in to comment.