Skip to content
Open
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 @@ -54,13 +54,8 @@
@Singleton
public class JibLayerFilterExtension implements JibMavenPluginExtension<Configuration> {

private Map<PathMatcher, String> pathMatchers = new LinkedHashMap<>();

@VisibleForTesting @Inject ProjectDependenciesResolver dependencyResolver;

// (layer name, layer builder) map for new layers of configured <toLayer>
@VisibleForTesting Map<String, FileEntriesLayer.Builder> newToLayers = new LinkedHashMap<>();

@Override
public Optional<Class<Configuration>> getExtraConfigType() {
return Optional.of(Configuration.class);
Expand All @@ -80,7 +75,12 @@ public ContainerBuildPlan extendContainerBuildPlan(
return buildPlan;
}

preparePathMatchersAndLayerBuilders(buildPlan, config.get());
Map<PathMatcher, String> pathMatchers = new LinkedHashMap<>();

// (layer name, layer builder) map for new layers of configured <toLayer>
Map<String, FileEntriesLayer.Builder> newToLayers = new LinkedHashMap<>();

preparePathMatchersAndLayerBuilders(buildPlan, config.get(), pathMatchers, newToLayers);

ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
newPlanBuilder.setLayers(Collections.emptyList());
Expand All @@ -92,7 +92,8 @@ public ContainerBuildPlan extendContainerBuildPlan(
List<FileEntry> filesToKeep = new ArrayList<>();

for (FileEntry entry : layer.getEntries()) {
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
Optional<String> finalLayerName =
determineFinalLayerName(entry, layer.getName(), pathMatchers);
// Either keep, move, or delete this FileEntry.
if (finalLayerName.isPresent()) {
if (finalLayerName.get().equals(layer.getName())) {
Expand Down Expand Up @@ -235,11 +236,14 @@ private void logMissingParentDependencies(
}

private void preparePathMatchersAndLayerBuilders(
ContainerBuildPlan buildPlan, Configuration config) throws JibPluginExtensionException {
ContainerBuildPlan buildPlan,
Configuration config,
Map<PathMatcher, String> pathMatchers,
Map<String, FileEntriesLayer.Builder> newToLayers)
throws JibPluginExtensionException {
List<String> originalLayerNames =
buildPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList());

newToLayers.clear(); // ensure empty (in case previously built module already populated it)
for (Configuration.Filter filter : config.getFilters()) {
String toLayerName = filter.getToLayer();
if (!toLayerName.isEmpty() && originalLayerNames.contains(toLayerName)) {
Expand Down Expand Up @@ -271,7 +275,8 @@ private void preparePathMatchersAndLayerBuilders(
* @return final layer name into which {@code fileEntry} should move. May be same as {@code
* originalLayerName}. {@link Optional#empty()} indicates deletion.
*/
private Optional<String> determineFinalLayerName(FileEntry fileEntry, String originalLayerName) {
private Optional<String> determineFinalLayerName(
FileEntry fileEntry, String originalLayerName, Map<PathMatcher, String> pathMatchers) {
Optional<String> finalLayerName = Optional.of(originalLayerName);

for (Map.Entry<PathMatcher, String> mapEntry : pathMatchers.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel;
import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -236,9 +235,6 @@ public void testExtendContainerBuildPlan_sameLayerNameInMultipleFilters()
ContainerBuildPlan newPlan =
extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);

ArrayList<String> layerNames = new ArrayList<>(extension.newToLayers.keySet());
assertEquals(Arrays.asList("foo", "same layer name", "bar", "baz"), layerNames);

assertEquals(4, newPlan.getLayers().size());
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
Expand Down