Skip to content

Commit

Permalink
Refactor amqp-client devservices to use a common container locator [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
patrox committed Jun 26, 2021
1 parent e55f0c6 commit 455e51e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devservices-common</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package io.quarkus.smallrye.reactivemessaging.amqp.deployment;

import java.io.Closeable;
import java.util.List;
import java.util.Objects;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerPort;

import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.IsDockerWorking;
import io.quarkus.deployment.IsNormal;
Expand All @@ -25,6 +20,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.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;

Expand All @@ -44,6 +40,8 @@ public class DevServicesAmqpProcessor {
private static final String DEV_SERVICE_LABEL = "quarkus-dev-service-amqp";

private static final int AMQP_PORT = 5672;

private static final ContainerLocator amqpContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL, AMQP_PORT);
private static final String AMQP_HOST_PROP = "amqp-host";
private static final String AMQP_PORT_PROP = "amqp-port";
private static final String AMQP_USER_PROP = "amqp-user";
Expand Down Expand Up @@ -154,28 +152,6 @@ private void shutdownBroker() {
}
}

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 AmqpBroker startAmqpBroker(AmqpDevServiceCfg config, LaunchModeBuildItem launchMode) {
if (!config.devServicesEnabled) {
// explicitly disabled
Expand All @@ -202,19 +178,11 @@ private AmqpBroker startAmqpBroker(AmqpDevServiceCfg config, LaunchModeBuildItem

if (config.shared && launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT) {
// Detect if there is a broker already started using container labels.
Container container = lookup(config.serviceName);
if (container != null) {
ContainerPort port = getMappedPort(container, AMQP_PORT);
if (port != null) {
Integer p = port.getPublicPort();
String url = port.getIp() + ":" + p;
log.infof("Dev Services for AMQP container found: %s (%s). "
+ "Connecting to: %s.",
container.getId(),
container.getImage(), url);
return new AmqpBroker(port.getIp(), p, "admin", "admin", null);
}
}

final String url = amqpContainerLocator.locateContainer(config.serviceName);

// FIXME: url -> host, port, username, password
return new AmqpBroker(url, 0, "admin", "admin", null);
}

// Starting the broker
Expand Down Expand Up @@ -304,7 +272,6 @@ public AmqpDevServiceCfg(AmqpDevServicesBuildTimeConfig devServicesConfig) {
this.extra = devServicesConfig.extraArgs;
this.shared = devServicesConfig.shared;
this.serviceName = devServicesConfig.serviceName;
;
}

@Override
Expand Down

0 comments on commit 455e51e

Please sign in to comment.