Skip to content

Commit 8a2ab24

Browse files
committed
Fix Elasticsearch DevService network for @QuarkusIntegrationTest for docker-image-building
1 parent 123ea8e commit 8a2ab24

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

extensions/elasticsearch-rest-client-common/deployment/src/main/java/io/quarkus/elasticsearch/restclient/common/deployment/DevServicesElasticsearchProcessor.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
192192
// Starting the server
193193
final Supplier<DevServicesResultBuildItem.RunningDevService> defaultElasticsearchSupplier = () -> {
194194

195-
GenericContainer<?> container = resolvedDistribution.equals(Distribution.ELASTIC)
195+
CreatedContainer createdContainer = resolvedDistribution.equals(Distribution.ELASTIC)
196196
? createElasticsearchContainer(config, resolvedImageName, useSharedNetwork)
197197
: createOpensearchContainer(config, resolvedImageName, useSharedNetwork);
198+
GenericContainer<?> container = createdContainer.genericContainer();
198199

199200
if (config.serviceName != null) {
200201
container.withLabel(DEV_SERVICE_LABEL, config.serviceName);
@@ -209,11 +210,15 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
209210
container.withReuse(config.reuse);
210211

211212
container.start();
213+
214+
var httpHost = container.getHost() + ":" + container.getMappedPort(ELASTICSEARCH_PORT);
215+
if (createdContainer.hostName() != null) {
216+
httpHost = createdContainer.hostName() + ":" + ELASTICSEARCH_PORT;
217+
}
212218
return new DevServicesResultBuildItem.RunningDevService(Feature.ELASTICSEARCH_REST_CLIENT_COMMON.getName(),
213219
container.getContainerId(),
214220
new ContainerShutdownCloseable(container, "Elasticsearch"),
215-
buildPropertiesMap(buildItemConfig,
216-
container.getHost() + ":" + container.getMappedPort(ELASTICSEARCH_PORT)));
221+
buildPropertiesMap(buildItemConfig, httpHost));
217222
};
218223

219224
return maybeContainerAddress
@@ -225,12 +230,13 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
225230
.orElseGet(defaultElasticsearchSupplier);
226231
}
227232

228-
private GenericContainer<?> createElasticsearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
233+
private CreatedContainer createElasticsearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
229234
DockerImageName resolvedImageName, boolean useSharedNetwork) {
230235
ElasticsearchContainer container = new ElasticsearchContainer(
231236
resolvedImageName.asCompatibleSubstituteFor("docker.elastic.co/elasticsearch/elasticsearch"));
237+
String hostName = null;
232238
if (useSharedNetwork) {
233-
ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_ELASTICSEARCH);
239+
hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_ELASTICSEARCH);
234240
}
235241

236242
// Disable security as else we would need to configure it correctly to avoid tons of WARNING in the log
@@ -241,15 +247,17 @@ private GenericContainer<?> createElasticsearchContainer(ElasticsearchDevService
241247
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cluster.html#disk-based-shard-allocation
242248
container.addEnv("cluster.routing.allocation.disk.threshold_enabled", "false");
243249
container.addEnv("ES_JAVA_OPTS", config.javaOpts);
244-
return container;
250+
251+
return new CreatedContainer(container, hostName);
245252
}
246253

247-
private GenericContainer<?> createOpensearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
254+
private CreatedContainer createOpensearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
248255
DockerImageName resolvedImageName, boolean useSharedNetwork) {
249256
OpensearchContainer container = new OpensearchContainer(
250257
resolvedImageName.asCompatibleSubstituteFor("opensearchproject/opensearch"));
258+
String hostName = null;
251259
if (useSharedNetwork) {
252-
ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_OPENSEARCH);
260+
hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_OPENSEARCH);
253261
}
254262

255263
container.addEnv("bootstrap.memory_lock", "true");
@@ -264,7 +272,11 @@ private GenericContainer<?> createOpensearchContainer(ElasticsearchDevServicesBu
264272
// Considering dev services are transient and not intended for production by nature,
265273
// we'll just set some hardcoded password.
266274
container.addEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", "NotActua11y$trongPa$$word");
267-
return container;
275+
276+
return new CreatedContainer(container, hostName);
277+
}
278+
279+
private record CreatedContainer(GenericContainer<?> genericContainer, String hostName) {
268280
}
269281

270282
private DockerImageName resolveImageName(ElasticsearchDevServicesBuildTimeConfig config,

0 commit comments

Comments
 (0)