Skip to content

Commit

Permalink
Filter out non aliases in network links for dependencies #586
Browse files Browse the repository at this point in the history
External container ids should not be wait for.
  • Loading branch information
rhuss committed Oct 14, 2016
1 parent 6f338bb commit 5b1006d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
56 changes: 42 additions & 14 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce
final PomLabel pomLabel = getPomLabel();

try {

// Queu of images to start
final Queue<ImageConfiguration> imagesWaitingToStart = new ArrayDeque<>();
// All aliases which are provided in the image configuration:
final Set<String> imageAliases = new HashSet<>();

for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, getResolvedImages())) {
final ImageConfiguration imageConfig = (ImageConfiguration) resolvable;
Expand All @@ -127,31 +129,32 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce

String imageName = imageConfig.getName();
checkImageWithAutoPull(hub, imageName,
getConfiguredRegistry(imageConfig, pullRegistry), imageConfig.getBuildConfiguration() == null);
getConfiguredRegistry(imageConfig, pullRegistry), imageConfig.getBuildConfiguration() == null);

RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
NetworkConfig config = runConfig.getNetworkingConfig();
if (autoCreateCustomNetworks && config.isCustomNetwork()) {
runService.createCustomNetworkIfNotExistant(config.getCustomNetwork());
}
imagesWaitingToStart.add(imageConfig);
if (imageConfig.getAlias() != null) {
imageAliases.add(imageConfig.getAlias());
}
}

final Set<String> startedContainers = new HashSet<>();

final ExecutorService executorService;
if (startParallel) {
executorService = Executors.newCachedThreadPool();
} else {
executorService = MoreExecutors.newDirectExecutorService();
}
final ExecutorService executorService = getExecutorService();
final ExecutorCompletionService<StartedContainerImage> startingContainers = new ExecutorCompletionService<>(executorService);

while (!imagesWaitingToStart.isEmpty()) {
final List<ImageConfiguration> startableImages = new ArrayList<>();

// Check for all images which can be already started
for (ImageConfiguration imageWaitingToStart : imagesWaitingToStart) {
if (startedContainers.containsAll(imageWaitingToStart.getDependencies())) {
List<String> allDependencies = imageWaitingToStart.getDependencies();
List<String> aliasDependencies = filterOutNonAliases(imageAliases, allDependencies);
if (startedContainers.containsAll(aliasDependencies)) {
startableImages.add(imageWaitingToStart);
}
}
Expand Down Expand Up @@ -188,18 +191,23 @@ public StartedContainerImage call() throws Exception {
final Future<StartedContainerImage> imageStartResult = startingContainers.take();
try {
final StartedContainerImage startedContainerImage = imageStartResult.get();

final String containerId = startedContainerImage.containerId;
final ImageConfiguration imageConfig = startedContainerImage.imageConfig;
final RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
final PortMapping portMapping = runService.getPortMapping(runConfig, projProperties);

// Add the alias to the list of started containers when it is set. When it's
// not set it cant be used in the dependency resolution anyway, so we are ignoring
// it here.
if (imageConfig.getAlias() != null) {
startedContainers.add(imageConfig.getAlias());
}

startedContainers.add(startedContainerImage.imageConfig.getAlias());
final RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
final PortMapping portMapping = runService.getPortMapping(runConfig, projProperties);

// Write out properties and expose them as project properties, too
portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile());
// Expose container info as properties
exposeContainerProps(hub.getQueryService(), containerId, imageConfig);

} catch (ExecutionException e) {
try {
if (e.getCause() instanceof RuntimeException) {
Expand Down Expand Up @@ -253,6 +261,26 @@ public StartedContainerImage call() throws Exception {
}
}

private List<String> filterOutNonAliases(Set<String> imageAliases, List<String> dependencies) {
List<String> ret = new ArrayList<>();
for (String alias : dependencies) {
if (imageAliases.contains(alias)) {
ret.add(alias);
}
}
return ret;
}

private ExecutorService getExecutorService() {
final ExecutorService executorService;
if (startParallel) {
executorService = Executors.newCachedThreadPool();
} else {
executorService = MoreExecutors.newDirectExecutorService();
}
return executorService;
}

// ========================================================================================================

private void waitIfRequested(ServiceHub hub, ImageConfiguration imageConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ private void addContainerNetwork(RunImageConfiguration runConfig, List<String> r
}

private void addDependsOn(RunImageConfiguration runConfig, List<String> ret) {
// Only used in required networks.
// Only used in custom networks.
if (runConfig.getDependsOn() != null && runConfig.getNetworkingConfig().isCustomNetwork()) {
for (String[] link : EnvUtil.splitOnLastColon(runConfig.getDependsOn())) {
ret.add(link[0]);
for (String link : runConfig.getDependsOn()) {
ret.add(link);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static java.util.concurrent.TimeUnit.*;

/**
* Utility class for various (loosely) environment related tasks.
* Utility class for various (loosely related) environment related tasks.
*
* @author roland
* @since 04.04.14
Expand Down

0 comments on commit 5b1006d

Please sign in to comment.