Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep duplicate layers to match history #1017

Merged
merged 2 commits into from
Sep 20, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CacheMetadata {
static class Builder {

private final ImageLayers.Builder<CachedLayerWithMetadata> layersBuilder =
ImageLayers.builder();
ImageLayers.<CachedLayerWithMetadata>builder().removeDuplicates();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is <CachedLayerWithMetadata> necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, get compiler errors otherwise as it resolves to ImageLayers<Layer>.


private Builder(ImageLayers<CachedLayerWithMetadata> initialLayers) {
layersBuilder.addAll(initialLayers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

/** Holds the layers for an image. Makes sure that there are no duplicate layers. */
/** Holds the layers for an image. */
public class ImageLayers<T extends Layer> implements Iterable<T> {

public static class Builder<T extends Layer> {

private final LinkedHashSet<T> layers = new LinkedHashSet<>();
private final List<T> layers = new ArrayList<>();
private final ImmutableSet.Builder<DescriptorDigest> layerDigestsBuilder =
ImmutableSet.builder();
private boolean removeDuplicates = false;

/**
* Adds a layer. Removes any prior occurrences of the same layer.
Expand All @@ -43,11 +48,7 @@ public static class Builder<T extends Layer> {
*/
public Builder<T> add(T layer) throws LayerPropertyNotFoundException {
layerDigestsBuilder.add(layer.getBlobDescriptor().getDigest());

// Remove necessary to move layer to the end of the LinkedHashSet.
layers.remove(layer);
layers.add(layer);

return this;
}

Expand All @@ -67,8 +68,26 @@ public <U extends T> Builder<T> addAll(ImageLayers<U> layers)
return this;
}

/**
* Remove any duplicate layers, keeping the last occurrence of the layer.
*
* @return this
*/
public Builder<T> removeDuplicates() {
removeDuplicates = true;
return this;
}

public ImageLayers<T> build() {
return new ImageLayers<>(ImmutableList.copyOf(layers), layerDigestsBuilder.build());
if (!removeDuplicates) {
return new ImageLayers<>(ImmutableList.copyOf(layers), layerDigestsBuilder.build());
}

// LinkedHashSet maintains the order but keeps the first occurrence. Keep last occurrence by
// adding elements in reverse, and then reversing the result
Set<T> dedupedButReversed = new LinkedHashSet<T>(Lists.reverse(this.layers));
ImmutableList<T> deduped = ImmutableList.copyOf(dedupedButReversed).reverse();
return new ImageLayers<>(deduped, layerDigestsBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,37 @@ public void testAddLayer_success() throws LayerPropertyNotFoundException {
}

@Test
public void testAddLayer_sameAsLastLayer() throws LayerPropertyNotFoundException {
public void testAddLayer_maintainDuplicates() throws LayerPropertyNotFoundException {
// must maintain duplicate
List<Layer> expectedLayers =
Arrays.asList(
mockCachedLayer,
mockReferenceLayer,
mockDigestOnlyLayer,
mockUnwrittenLayer,
mockCachedLayer);

ImageLayers<Layer> imageLayers =
ImageLayers.builder()
.add(mockCachedLayer)
.add(mockReferenceLayer)
.add(mockDigestOnlyLayer)
.add(mockUnwrittenLayer)
.add(mockCachedLayer)
.build();

Assert.assertEquals(expectedLayers, imageLayers.getLayers());
}

@Test
public void testAddLayer_removeDuplicates() throws LayerPropertyNotFoundException {
// remove duplicates: last layer should be kept
List<Layer> expectedLayers =
Arrays.asList(mockReferenceLayer, mockDigestOnlyLayer, mockUnwrittenLayer, mockCachedLayer);

ImageLayers<Layer> imageLayers =
ImageLayers.builder()
.removeDuplicates()
.add(mockCachedLayer)
.add(mockReferenceLayer)
.add(mockDigestOnlyLayer)
Expand Down
2 changes: 2 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- Keep duplicate layers to match container history ([#1017](https://github.com/GoogleContainerTools/jib/pull/1017))

## 0.9.10

### Added
Expand Down
2 changes: 2 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- Keep duplicate layers to match container history ([#1017](https://github.com/GoogleContainerTools/jib/pull/1017))

## 0.9.10

### Added
Expand Down