Skip to content

Commit

Permalink
Fix fabric8io#554: Add support for multiple copy layers using multipl…
Browse files Browse the repository at this point in the history
…e assemblies

Signed-off-by: Brian O'Keefe <zer0keefie@gmail.com>
  • Loading branch information
zer0keefie committed Oct 7, 2020
1 parent 6db9a23 commit 76f68b3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public enum ConfigKey {

ALIAS,
ARGS(ValueCombinePolicy.Merge),
ASSEMBLIES,
ASSEMBLY_BASEDIR("assembly.baseDir"),
ASSEMBLY_DESCRIPTOR("assembly.descriptor"),
ASSEMBLY_DESCRIPTOR_REF("assembly.descriptorRef"),
ASSEMBLY_EXPORT_BASEDIR("assembly.exportBaseDir"),
ASSEMBLY_IGNORE_PERMISSIONS("assembly.ignorePermissions"),
ASSEMBLY_NAME("assembly.name"),
ASSEMBLY_PERMISSIONS("assembly.permissions"),
ASSEMBLY_DOCKER_FILE_DIR("assembly.dockerFileDir"),
ASSEMBLY_USER("assembly.user"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,29 @@ private NetworkConfig extractNetworkConfig(NetworkConfig config, ValueProvider v
}

private List<AssemblyConfiguration> extractAssemblies(List<AssemblyConfiguration> config, ValueProvider valueProvider) {
List<ValueProvider> assemblyConfigProviders = valueProvider.getNestedList(ASSEMBLIES);
List<AssemblyConfiguration> assemblies = new ArrayList<>();
if (config != null) {
for (AssemblyConfiguration cfg : config) {
assemblies.add(extractAssembly(cfg, valueProvider));

int count = Math.max(assemblyConfigProviders.size(), config == null ? 0 : config.size());

for (int i = 0; i < count; i++) {
AssemblyConfiguration fromConfig = config == null || i >= config.size() ? null : config.get(i);

if (i >= assemblyConfigProviders.size()) {
assemblies.add(fromConfig);
} else {
ValueProvider provider = assemblyConfigProviders.get(i);
assemblies.add(extractAssembly(fromConfig, provider));
}
}

return assemblies;
}

@SuppressWarnings("deprecation")
private AssemblyConfiguration extractAssembly(AssemblyConfiguration config, ValueProvider valueProvider) {
return new AssemblyConfiguration.Builder()
.name(valueProvider.getString(ASSEMBLY_NAME, config == null ? null : config.getName()))
.targetDir(valueProvider.getString(ASSEMBLY_BASEDIR, config == null ? null : config.getTargetDir()))
.descriptor(valueProvider.getString(ASSEMBLY_DESCRIPTOR, config == null ? null : config.getDescriptor()))
.descriptorRef(valueProvider.getString(ASSEMBLY_DESCRIPTOR_REF, config == null ? null : config.getDescriptorRef()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.fabric8.maven.docker.config.handler.property;

import java.util.*;
import java.util.stream.Collectors;

import io.fabric8.maven.docker.util.EnvUtil;

Expand Down Expand Up @@ -43,6 +44,7 @@ public class ValueProvider {
private LongValueExtractor longValueExtractor;
private BooleanValueExtractor booleanValueExtractor;
private DoubleValueExtractor doubleValueExtractor;
private NestedValueExtractor nestedValueExtractor;

/**
* Initiates ValueProvider which is to work with data from the given properties.
Expand All @@ -66,6 +68,7 @@ public ValueProvider(String prefix, Properties properties, PropertyMode property
longValueExtractor = new LongValueExtractor();
booleanValueExtractor = new BooleanValueExtractor();
doubleValueExtractor = new DoubleValueExtractor();
nestedValueExtractor = new NestedValueExtractor();
}

public String getString(ConfigKey key, String fromConfig) {
Expand Down Expand Up @@ -119,6 +122,11 @@ protected T withPrefix(String prefix, ConfigKey key, Properties properties) {
return arbitraryExtractor.getFromPreferredSource(prefix, key, fromConfig);
}

public List<ValueProvider> getNestedList(ConfigKey key) {
List<ValueProvider> nested = nestedValueExtractor.getFromPreferredSource(prefix, key, null);
return nested == null ? Collections.emptyList() : nested;
}

/**
* Helper base class for picking values out of the the Properties class and/or config value.
*
Expand Down Expand Up @@ -307,4 +315,30 @@ protected Map<String, String> merge(ConfigKey key, List<Map<String, String>> val
return merged;
}
}

private class NestedValueExtractor extends ValueExtractor<List<ValueProvider>> {

@Override
protected List<ValueProvider> withPrefix(String prefix, ConfigKey key, Properties properties) {
Map<String, String> props = extractFromPropertiesAsMap(key.asPropertyKey(prefix), properties);
Properties p = new Properties();
p.putAll(props);

if (props == null) {
return null;
}
Map<Integer, ValueProvider> res = new TreeMap<>();
for(Map.Entry<String, String> entry : props.entrySet()) {
String pfx = entry.getKey().substring(0, entry.getKey().indexOf('.'));
try {
int n = Integer.parseInt(pfx);
res.computeIfAbsent(n, k -> new ValueProvider(pfx + '.' + prefix, p, propertyMode));
} catch (NumberFormatException ex) {
// Skip this entry
}
}

return new ArrayList<>(res.values());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,28 @@ public void testAssembly() throws Exception {
assertTrue(config.isIgnorePermissions());
}

@Test
public void testMultipleAssemblies() {
List<ImageConfiguration> configs = resolveImage(imageConfiguration, props(getTestMultipleAssemblyData()));
assertEquals(1, configs.size());

List<AssemblyConfiguration> assemblies = configs.get(0).getBuildConfiguration().getAssemblyConfigurations();
assertEquals(2, assemblies.size());

AssemblyConfiguration config = assemblies.get(0);
assertEquals("user", config.getUser());
assertEquals("project", config.getDescriptorRef());
assertFalse(config.exportTargetDir());
assertTrue(config.isIgnorePermissions());

config = assemblies.get(1);
assertEquals("user", config.getUser());
assertEquals("artifact", config.getDescriptorRef());
assertEquals("art", config.getName());
assertFalse(config.exportTargetDir());
assertTrue(config.isIgnorePermissions());
}

@Test
public void testNoCleanup() throws Exception {
String[] testData = new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.CLEANUP), "none", k(ConfigKey.FROM), "base" };
Expand Down Expand Up @@ -1079,6 +1101,24 @@ private String[] getTestAssemblyData() {
};
}

private String[] getTestMultipleAssemblyData() {
return new String[] {
k(ConfigKey.FROM), "busybox",
k(ConfigKey.ASSEMBLIES) + ".1." + k(ConfigKey.ASSEMBLY_BASEDIR), "/basedir",
k(ConfigKey.ASSEMBLIES) + ".1." + k(ConfigKey.ASSEMBLY_DESCRIPTOR_REF), "project",
k(ConfigKey.ASSEMBLIES) + ".1." + k(ConfigKey.ASSEMBLY_EXPORT_BASEDIR), "false",
k(ConfigKey.ASSEMBLIES) + ".1." + k(ConfigKey.ASSEMBLY_IGNORE_PERMISSIONS), "true",
k(ConfigKey.ASSEMBLIES) + ".1." + k(ConfigKey.ASSEMBLY_USER), "user",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_BASEDIR), "/basedir",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_DESCRIPTOR_REF), "artifact",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_EXPORT_BASEDIR), "false",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_IGNORE_PERMISSIONS), "true",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_USER), "user",
k(ConfigKey.ASSEMBLIES) + ".2." + k(ConfigKey.ASSEMBLY_NAME), "art",
k(ConfigKey.NAME), "image",
};
}

private String[] getTestData() {
return new String[] {
k(ConfigKey.ALIAS), "alias",
Expand Down

0 comments on commit 76f68b3

Please sign in to comment.