Skip to content

Commit

Permalink
Refactor redis-client devservices to use 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 a182f35 commit c7c7b6e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
5 changes: 5 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@
<artifactId>quarkus-devservices-mssql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devservices-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elasticsearch-rest-client-common</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions extensions/devservices/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-devservices-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-devservices-common</artifactId>
<name>Quarkus - DevServices - Common</name>

<dependencies>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.quarkus.redis.client.deployment;
package io.quarkus.devservices.common;

import java.util.List;

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;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;

public class ContainerLocator {

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

private final String devServiceLabel;
private final int port;
Expand All @@ -20,7 +20,6 @@ public ContainerLocator(String devServiceLabel, int port) {
this.port = port;
}

@Nullable
private Container lookup(String expectedLabelValue) {
List<Container> containers = DockerClientFactory.lazyClient().listContainersCmd().exec();
for (Container container : containers) {
Expand All @@ -32,7 +31,6 @@ private Container lookup(String expectedLabelValue) {
return null;
}

@Nullable
private ContainerPort getMappedPort(Container container, int port) {
for (ContainerPort p : container.getPorts()) {
Integer mapped = p.getPrivatePort();
Expand All @@ -44,15 +42,14 @@ private ContainerPort getMappedPort(Container container, int port) {
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.",
+ "Connecting to: %s.",
container.getId(),
container.getImage(), url);
return url;
Expand Down
1 change: 1 addition & 0 deletions extensions/devservices/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>derby</module>
<module>mssql</module>
<module>db2</module>
<module>common</module>
</modules>

</project>
4 changes: 4 additions & 0 deletions extensions/redis-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devservices-common</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-redis-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.devservices.common.ContainerLocator;
import io.quarkus.redis.client.deployment.RedisBuildTimeConfig.DevServiceConfiguration;
import io.quarkus.redis.client.runtime.RedisClientUtil;
import io.quarkus.redis.client.runtime.RedisConfig;
Expand Down Expand Up @@ -147,7 +148,8 @@ private StartResult startContainer(String connectionName, DevServicesConfig devS
return new StartResult(url, null);
}

FixedPortRedisContainer redisContainer = new FixedPortRedisContainer(dockerImageName, devServicesConfig.port, launchMode == DEVELOPMENT ? devServicesConfig.serviceName : 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,
Expand Down

0 comments on commit c7c7b6e

Please sign in to comment.