From 6f99bbc9ff34670b871fdf7db0201629f35fc400 Mon Sep 17 00:00:00 2001 From: Jakub Gardo Date: Fri, 1 Nov 2024 22:59:11 +0100 Subject: [PATCH 1/2] Fix Pulsar DevService network for @QuarkusIntegrationTest for docker-image-building --- .../PulsarDevServicesProcessor.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java index 3aa71a410c64b..507b0a3ea9e75 100644 --- a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java +++ b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java @@ -3,6 +3,7 @@ import java.io.Closeable; import java.time.Duration; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -21,12 +22,14 @@ import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem; import io.quarkus.deployment.builditem.DevServicesResultBuildItem; import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService; +import io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem; import io.quarkus.deployment.builditem.DockerStatusBuildItem; import io.quarkus.deployment.builditem.LaunchModeBuildItem; import io.quarkus.deployment.console.ConsoleInstalledBuildItem; import io.quarkus.deployment.console.StartupLogCompressor; import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig; import io.quarkus.deployment.logging.LoggingSetupBuildItem; +import io.quarkus.devservices.common.ConfigureUtil; import io.quarkus.devservices.common.ContainerLocator; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.configuration.ConfigUtils; @@ -48,6 +51,7 @@ public class PulsarDevServicesProcessor { private static final ContainerLocator pulsarContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL, PulsarContainer.BROKER_PORT); + private static final String DEV_SERVICE_PULSAR = "pulsar"; private static final String PULSAR_CLIENT_SERVICE_URL = "pulsar.client.serviceUrl"; private static final String PULSAR_ADMIN_SERVICE_URL = "pulsar.admin.serviceUrl"; static volatile RunningDevService devService; @@ -59,12 +63,15 @@ public DevServicesResultBuildItem startPulsarDevService( DockerStatusBuildItem dockerStatusBuildItem, LaunchModeBuildItem launchMode, PulsarBuildTimeConfig pulsarClientBuildTimeConfig, + List devServicesSharedNetworkBuildItem, Optional consoleInstalledBuildItem, CuratedApplicationShutdownBuildItem closeBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig devServicesConfig) { PulsarDevServiceCfg configuration = getConfiguration(pulsarClientBuildTimeConfig); + boolean useSharedNetwork = DevServicesSharedNetworkBuildItem.isSharedNetworkRequired(devServicesConfig, + devServicesSharedNetworkBuildItem); if (devService != null) { boolean shouldShutdownTheBroker = !configuration.equals(cfg); @@ -80,7 +87,7 @@ public DevServicesResultBuildItem startPulsarDevService( loggingSetupBuildItem); try { RunningDevService newDevService = startPulsarContainer(dockerStatusBuildItem, configuration, launchMode, - devServicesConfig.timeout); + useSharedNetwork, devServicesConfig.timeout); if (newDevService != null) { devService = newDevService; Map config = devService.getConfig(); @@ -138,7 +145,8 @@ private void shutdownBroker() { } private RunningDevService startPulsarContainer(DockerStatusBuildItem dockerStatusBuildItem, PulsarDevServiceCfg config, - LaunchModeBuildItem launchMode, Optional timeout) { + LaunchModeBuildItem launchMode, + boolean useSharedNetwork, Optional timeout) { if (!config.devServicesEnabled) { // explicitly disabled log.debug("Not starting Dev Services for Pulsar, as it has been disabled in the config."); @@ -175,10 +183,21 @@ private RunningDevService startPulsarContainer(DockerStatusBuildItem dockerStatu container.withPort(config.fixedExposedPort); } timeout.ifPresent(container::withStartupTimeout); + String hostName = null; + if (useSharedNetwork) { + hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_PULSAR); + } container.start(); - return getRunningService(container.getContainerId(), container::close, container.getPulsarBrokerUrl(), - container.getHttpServiceUrl()); + var pulsarBrokerUrl = container.getPulsarBrokerUrl(); + var httpHostServiceUrl = container.getHttpServiceUrl(); + if (useSharedNetwork) { + pulsarBrokerUrl = getServiceUrl(hostName, PulsarContainer.BROKER_PORT); + httpHostServiceUrl = getHttpServiceUrl(hostName, PulsarContainer.BROKER_HTTP_PORT); + } + + return getRunningService(container.getContainerId(), container::close, pulsarBrokerUrl, + httpHostServiceUrl); }; return pulsarContainerLocator.locateContainer(config.serviceName, config.shared, launchMode.getLaunchMode()) From 321efede8ae69343774b7ce7e57eb1bf76332384 Mon Sep 17 00:00:00 2001 From: Jakub Gardo Date: Fri, 8 Nov 2024 23:32:44 +0100 Subject: [PATCH 2/2] Move logic of resolving PulsarBrokerUrl and HttpServiceUrl into PulsarContainer --- .../pulsar/deployment/PulsarContainer.java | 32 +++++++++++++++++-- .../PulsarDevServicesProcessor.java | 17 +++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarContainer.java b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarContainer.java index c21a5ac9f7263..fd3a4a94738e9 100644 --- a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarContainer.java +++ b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarContainer.java @@ -1,5 +1,7 @@ package io.quarkus.smallrye.reactivemessaging.pulsar.deployment; +import static io.quarkus.smallrye.reactivemessaging.pulsar.deployment.PulsarDevServicesProcessor.DEV_SERVICE_PULSAR; + import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Collections; @@ -11,6 +13,8 @@ import com.github.dockerjava.api.command.InspectContainerResponse; +import io.quarkus.devservices.common.ConfigureUtil; + public class PulsarContainer extends GenericContainer { public static final DockerImageName PULSAR_IMAGE = DockerImageName.parse("apachepulsar/pulsar:3.2.4"); @@ -20,6 +24,9 @@ public class PulsarContainer extends GenericContainer { public static final int BROKER_PORT = 6650; public static final int BROKER_HTTP_PORT = 8080; + private boolean useSharedNetwork; + private String hostName; + public PulsarContainer() { this(PULSAR_IMAGE); } @@ -51,6 +58,13 @@ protected void containerIsStarting(InspectContainerResponse containerInfo, boole STARTER_SCRIPT); } + public PulsarContainer withSharedNetwork() { + useSharedNetwork = true; + hostName = ConfigureUtil.configureSharedNetwork(this, DEV_SERVICE_PULSAR); + + return self(); + } + public PulsarContainer withPort(final int fixedPort) { if (fixedPort <= 0) { throw new IllegalArgumentException("The fixed port must be greater than 0"); @@ -60,10 +74,24 @@ public PulsarContainer withPort(final int fixedPort) { } public String getPulsarBrokerUrl() { - return String.format("pulsar://%s:%s", this.getHost(), this.getMappedPort(BROKER_PORT)); + if (useSharedNetwork) { + return getServiceUrl(this.hostName, PulsarContainer.BROKER_PORT); + } + return getServiceUrl(this.getHost(), this.getMappedPort(BROKER_PORT)); + } + + private String getServiceUrl(String host, int port) { + return String.format("pulsar://%s:%d", host, port); } public String getHttpServiceUrl() { - return String.format("http://%s:%s", this.getHost(), this.getMappedPort(BROKER_HTTP_PORT)); + if (useSharedNetwork) { + return getHttpServiceUrl(this.hostName, PulsarContainer.BROKER_HTTP_PORT); + } + return getHttpServiceUrl(this.getHost(), this.getMappedPort(BROKER_HTTP_PORT)); + } + + private String getHttpServiceUrl(String host, int port) { + return String.format("http://%s:%d", host, port); } } diff --git a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java index 507b0a3ea9e75..1c1184e534af0 100644 --- a/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java +++ b/extensions/smallrye-reactive-messaging-pulsar/deployment/src/main/java/io/quarkus/smallrye/reactivemessaging/pulsar/deployment/PulsarDevServicesProcessor.java @@ -29,7 +29,6 @@ import io.quarkus.deployment.console.StartupLogCompressor; import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig; import io.quarkus.deployment.logging.LoggingSetupBuildItem; -import io.quarkus.devservices.common.ConfigureUtil; import io.quarkus.devservices.common.ContainerLocator; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.configuration.ConfigUtils; @@ -51,9 +50,9 @@ public class PulsarDevServicesProcessor { private static final ContainerLocator pulsarContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL, PulsarContainer.BROKER_PORT); - private static final String DEV_SERVICE_PULSAR = "pulsar"; private static final String PULSAR_CLIENT_SERVICE_URL = "pulsar.client.serviceUrl"; private static final String PULSAR_ADMIN_SERVICE_URL = "pulsar.admin.serviceUrl"; + static final String DEV_SERVICE_PULSAR = "pulsar"; static volatile RunningDevService devService; static volatile PulsarDevServiceCfg cfg; static volatile boolean first = true; @@ -183,21 +182,13 @@ private RunningDevService startPulsarContainer(DockerStatusBuildItem dockerStatu container.withPort(config.fixedExposedPort); } timeout.ifPresent(container::withStartupTimeout); - String hostName = null; if (useSharedNetwork) { - hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_PULSAR); + container.withSharedNetwork(); } container.start(); - var pulsarBrokerUrl = container.getPulsarBrokerUrl(); - var httpHostServiceUrl = container.getHttpServiceUrl(); - if (useSharedNetwork) { - pulsarBrokerUrl = getServiceUrl(hostName, PulsarContainer.BROKER_PORT); - httpHostServiceUrl = getHttpServiceUrl(hostName, PulsarContainer.BROKER_HTTP_PORT); - } - - return getRunningService(container.getContainerId(), container::close, pulsarBrokerUrl, - httpHostServiceUrl); + return getRunningService(container.getContainerId(), container::close, container.getPulsarBrokerUrl(), + container.getHttpServiceUrl()); }; return pulsarContainerLocator.locateContainer(config.serviceName, config.shared, launchMode.getLaunchMode())