[JCLOUDS-1117] fix for advanced Docker configuration - the Config.Builder should stay unchanged if used

This commit is contained in:
Josef Cacek 2016-05-24 14:09:06 +02:00 committed by Ignasi Barrera
parent 4ef28251c5
commit d4cf774389
2 changed files with 51 additions and 37 deletions

View File

@ -89,9 +89,11 @@ public class DockerComputeServiceAdapter implements
String loginUserPassword = template.getImage().getDefaultCredentials().getOptionalPassword().or("password");
DockerTemplateOptions templateOptions = DockerTemplateOptions.class.cast(options);
Config containerConfig = null;
Config.Builder containerConfigBuilder = templateOptions.getConfigBuilder();
if (containerConfigBuilder == null) {
containerConfigBuilder = Config.builder();
containerConfigBuilder = Config.builder().image(imageId);
containerConfigBuilder.entrypoint(templateOptions.getEntrypoint());
containerConfigBuilder.cmd(templateOptions.getCommands());
@ -110,14 +112,14 @@ public class DockerComputeServiceAdapter implements
}
HostConfig.Builder hostConfigBuilder = HostConfig.builder()
.publishAllPorts(true)
.privileged(templateOptions.getPrivileged());
.publishAllPorts(true)
.privileged(templateOptions.getPrivileged());
if (!templateOptions.getPortBindings().isEmpty()) {
Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
for (Map.Entry<Integer, Integer> entry : templateOptions.getPortBindings().entrySet()) {
portBindings.put(entry.getValue() + "/tcp",
Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0", "HostPort", Integer.toString(entry.getKey()))));
Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0", "HostPort", Integer.toString(entry.getKey()))));
}
hostConfigBuilder.portBindings(portBindings);
}
@ -147,41 +149,43 @@ public class DockerComputeServiceAdapter implements
hostConfigBuilder.networkMode(templateOptions.getNetworkMode());
containerConfigBuilder.hostConfig(hostConfigBuilder.build());
}
containerConfigBuilder.image(imageId);
// add the inbound ports into exposed ports map
Config containerConfig = containerConfigBuilder.build();
Map<String, Object> exposedPorts = Maps.newHashMap();
if (containerConfig.exposedPorts() == null) {
exposedPorts.putAll(containerConfig.exposedPorts());
}
for (int inboundPort : templateOptions.getInboundPorts()) {
String portKey = inboundPort + "/tcp";
if (!exposedPorts.containsKey(portKey)) {
exposedPorts.put(portKey, Maps.newHashMap());
// add the inbound ports into exposed ports map
containerConfig = containerConfigBuilder.build();
Map<String, Object> exposedPorts = Maps.newHashMap();
if (containerConfig.exposedPorts() == null) {
exposedPorts.putAll(containerConfig.exposedPorts());
}
}
containerConfigBuilder.exposedPorts(exposedPorts);
// build once more after setting inboundPorts
containerConfig = containerConfigBuilder.build();
// finally update port bindings
Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
Map<String, List<Map<String, String>>> existingBindings = containerConfig.hostConfig().portBindings();
if (existingBindings != null) {
portBindings.putAll(existingBindings);
}
for (String exposedPort : containerConfig.exposedPorts().keySet()) {
if (!portBindings.containsKey(exposedPort)) {
portBindings.put(exposedPort, Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0")));
for (int inboundPort : templateOptions.getInboundPorts()) {
String portKey = inboundPort + "/tcp";
if (!exposedPorts.containsKey(portKey)) {
exposedPorts.put(portKey, Maps.newHashMap());
}
}
containerConfigBuilder.exposedPorts(exposedPorts);
// build once more after setting inboundPorts
containerConfig = containerConfigBuilder.build();
// finally update port bindings
Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
Map<String, List<Map<String, String>>> existingBindings = containerConfig.hostConfig().portBindings();
if (existingBindings != null) {
portBindings.putAll(existingBindings);
}
for (String exposedPort : containerConfig.exposedPorts().keySet()) {
if (!portBindings.containsKey(exposedPort)) {
portBindings.put(exposedPort, Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0")));
}
}
hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig());
hostConfigBuilder.portBindings(portBindings);
containerConfigBuilder.hostConfig(hostConfigBuilder.build());
} else {
containerConfigBuilder.image(imageId);
}
HostConfig.Builder hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig());
hostConfigBuilder.portBindings(portBindings);
containerConfigBuilder.hostConfig(hostConfigBuilder.build());
containerConfig = containerConfigBuilder.build();
logger.debug(">> creating new container with containerConfig(%s)", containerConfig);

View File

@ -26,6 +26,7 @@ import static org.testng.Assert.assertNotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.ExecResponse;
@ -49,8 +50,11 @@ import org.testng.annotations.Test;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
@ -61,6 +65,7 @@ import com.google.inject.Module;
public class SshToCustomPortLiveTest extends BaseComputeServiceContextLiveTest {
private static final int SSH_PORT = 8822;
private static final int SSH_PORT_BRIDGE = 18822;
private static final String IMAGE_REPOSITORY = "jclouds/testrepo";
private static final String IMAGE_TAG_1 = "testtag";
private static final String IMAGE_TAG_2 = "second";
@ -130,12 +135,17 @@ public class SshToCustomPortLiveTest extends BaseComputeServiceContextLiveTest {
@Test(dependsOnMethods = "testImageCreated")
public void testAdvancedConfig() throws RunNodesException {
final String portId = SSH_PORT + "/tcp";
final DockerTemplateOptions options = DockerTemplateOptions.Builder
.configBuilder(
Config.builder().env(ImmutableList.<String> of("SSH_PORT=" + SSH_PORT, "ROOT_PASSWORD=jcloudsRulez"))
.hostConfig(HostConfig.builder().networkMode("host").build())
.exposedPorts(ImmutableMap.<String, Object> of(portId, Maps.newHashMap()))
.hostConfig(HostConfig.builder().networkMode("bridge")
.portBindings(ImmutableMap.<String, List<Map<String, String>>> of(portId,
Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostPort", String.valueOf(SSH_PORT_BRIDGE)))))
.build())
.image("test-if-this-value-is-correctly-overriden"))
.overrideLoginUser("root").overrideLoginPassword("jcloudsRulez").blockOnPort(SSH_PORT, 30);
.overrideLoginUser("root").overrideLoginPassword("jcloudsRulez").blockOnPort(SSH_PORT_BRIDGE, 30);
final Template template = view.getComputeService().templateBuilder().imageId(image.id()).options(options).build();