Skip to content

Commit

Permalink
Introduce a common container locator
Browse files Browse the repository at this point in the history
  • Loading branch information
patrox committed Jun 26, 2021
1 parent 2491dcd commit a182f35
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.redis.client.deployment;

import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;
import io.smallrye.common.constraint.Nullable;
import org.jboss.logging.Logger;
import org.testcontainers.DockerClientFactory;

import java.util.List;

public class ContainerLocator {

private static final Logger log = Logger.getLogger(DevServicesProcessor.class);

private final String devServiceLabel;
private final int port;

public ContainerLocator(String devServiceLabel, int port) {
this.devServiceLabel = devServiceLabel;
this.port = port;
}

@Nullable
private Container lookup(String expectedLabelValue) {
List<Container> containers = DockerClientFactory.lazyClient().listContainersCmd().exec();
for (Container container : containers) {
String s = container.getLabels().get(devServiceLabel);
if (expectedLabelValue.equalsIgnoreCase(s)) {
return container;
}
}
return null;
}

@Nullable
private ContainerPort getMappedPort(Container container, int port) {
for (ContainerPort p : container.getPorts()) {
Integer mapped = p.getPrivatePort();
Integer publicPort = p.getPublicPort();
if (mapped != null && mapped == port && publicPort != null) {
return p;
}
}
return null;
}

@Nullable
public String locateContainer(String serviceName) {
Container container = lookup(serviceName);
if (container != null) {
ContainerPort containerPort = getMappedPort(container, port);
if (containerPort != null) {
String url = containerPort.getIp() + ":" + containerPort.getPublicPort();
log.infof("Dev Services container locator found: %s (%s). "
+ "Connecting to: %s.",
container.getId(),
container.getImage(), url);
return url;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import java.util.Map.Entry;
import java.util.OptionalInt;

import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;
import org.jboss.logging.Logger;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

Expand Down Expand Up @@ -46,6 +43,8 @@ public class DevServicesProcessor {
*/
private static final String DEV_SERVICE_LABEL = "quarkus-dev-service-redis";

private static final ContainerLocator redisContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL, REDIS_EXPOSED_PORT);

private static final String QUARKUS = "quarkus.";
private static final String DOT = ".";
private static volatile List<Closeable> closeables;
Expand Down Expand Up @@ -101,58 +100,28 @@ public void startRedisContainers(LaunchModeBuildItem launchMode,

if (first) {
first = false;
Runnable closeTask = new Runnable() {
@Override
public void run() {
if (closeables != null) {
for (Closeable closeable : closeables) {
try {
closeable.close();
} catch (Throwable t) {
log.error("Failed to stop database", t);
}
Runnable closeTask = () -> {
if (closeables != null) {
for (Closeable closeable : closeables) {
try {
closeable.close();
} catch (Throwable t) {
log.error("Failed to stop database", t);
}
}
first = true;
closeables = null;
capturedDevServicesConfiguration = null;
}
first = true;
closeables = null;
capturedDevServicesConfiguration = null;
};
QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
((QuarkusClassLoader) cl.parent()).addCloseTask(closeTask);
Thread closeHookThread = new Thread(closeTask, "Redis container shutdown thread");
Runtime.getRuntime().addShutdownHook(closeHookThread);
((QuarkusClassLoader) cl.parent()).addCloseTask(new Runnable() {
@Override
public void run() {
Runtime.getRuntime().removeShutdownHook(closeHookThread);
}
});
((QuarkusClassLoader) cl.parent()).addCloseTask(() -> Runtime.getRuntime().removeShutdownHook(closeHookThread));
}
}

private static Container lookup(String expectedLabelValue) {
List<Container> containers = DockerClientFactory.lazyClient().listContainersCmd().exec();
for (Container container : containers) {
String s = container.getLabels().get(DEV_SERVICE_LABEL);
if (expectedLabelValue.equalsIgnoreCase(s)) {
return container;
}
}
return null;
}

private static ContainerPort getMappedPort(Container container, int port) {
for (ContainerPort p : container.getPorts()) {
Integer mapped = p.getPrivatePort();
Integer publicPort = p.getPublicPort();
if (mapped != null && mapped == port && publicPort != null) {
return p;
}
}
return null;
}

private StartResult startContainer(String connectionName, DevServicesConfig devServicesConfig, LaunchMode launchMode) {
if (!devServicesConfig.enabled) {
// explicitly disabled
Expand All @@ -174,30 +143,15 @@ private StartResult startContainer(String connectionName, DevServicesConfig devS
.asCompatibleSubstituteFor(REDIS_6_ALPINE);

if (devServicesConfig.shared && launchMode == DEVELOPMENT) {
Container container = lookup(devServicesConfig.serviceName);
if (container != null) {
ContainerPort port = getMappedPort(container, REDIS_EXPOSED_PORT);
if (port != null) {
String url = port.getIp() + ":" + port.getPublicPort();
log.infof("Dev Services for Redis container found: %s (%s). "
+ "Connecting to: %s.",
container.getId(),
container.getImage(), url);
return new StartResult(url, null);
}
}
final String url = redisContainerLocator.locateContainer(devServicesConfig.serviceName);
return new StartResult(url, null);
}

FixedPortRedisContainer redisContainer = new FixedPortRedisContainer(dockerImageName, devServicesConfig.port, launchMode == DEVELOPMENT ? devServicesConfig.serviceName : null);
redisContainer.start();
String redisHost = REDIS_SCHEME + redisContainer.getHost() + ":" + redisContainer.getPort();
return new StartResult(redisHost,
new Closeable() {
@Override
public void close() {
redisContainer.close();
}
});
redisContainer::close);
}

private String getConfigPrefix(String connectionName) {
Expand Down

0 comments on commit a182f35

Please sign in to comment.