Skip to content

Commit

Permalink
Fix Elasticsearch DevService network for @QuarkusIntegrationTest for …
Browse files Browse the repository at this point in the history
…docker-image-building

(cherry picked from commit 8a2ab24)
  • Loading branch information
jgardo authored and gsmet committed Oct 29, 2024
1 parent 9900b1b commit 82eb6f4
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
// Starting the server
final Supplier<DevServicesResultBuildItem.RunningDevService> defaultElasticsearchSupplier = () -> {

GenericContainer<?> container = resolvedDistribution.equals(Distribution.ELASTIC)
CreatedContainer createdContainer = resolvedDistribution.equals(Distribution.ELASTIC)
? createElasticsearchContainer(config, resolvedImageName, useSharedNetwork)
: createOpensearchContainer(config, resolvedImageName, useSharedNetwork);
GenericContainer<?> container = createdContainer.genericContainer();

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

container.start();

var httpHost = container.getHost() + ":" + container.getMappedPort(ELASTICSEARCH_PORT);
if (createdContainer.hostName() != null) {
httpHost = createdContainer.hostName() + ":" + ELASTICSEARCH_PORT;
}
return new DevServicesResultBuildItem.RunningDevService(Feature.ELASTICSEARCH_REST_CLIENT_COMMON.getName(),
container.getContainerId(),
new ContainerShutdownCloseable(container, "Elasticsearch"),
buildPropertiesMap(buildItemConfig,
container.getHost() + ":" + container.getMappedPort(ELASTICSEARCH_PORT)));
buildPropertiesMap(buildItemConfig, httpHost));
};

return maybeContainerAddress
Expand All @@ -225,12 +230,13 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
.orElseGet(defaultElasticsearchSupplier);
}

private GenericContainer<?> createElasticsearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
private CreatedContainer createElasticsearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
DockerImageName resolvedImageName, boolean useSharedNetwork) {
ElasticsearchContainer container = new ElasticsearchContainer(
resolvedImageName.asCompatibleSubstituteFor("docker.elastic.co/elasticsearch/elasticsearch"));
String hostName = null;
if (useSharedNetwork) {
ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_ELASTICSEARCH);
hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_ELASTICSEARCH);
}

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

return new CreatedContainer(container, hostName);
}

private GenericContainer<?> createOpensearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
private CreatedContainer createOpensearchContainer(ElasticsearchDevServicesBuildTimeConfig config,
DockerImageName resolvedImageName, boolean useSharedNetwork) {
OpensearchContainer container = new OpensearchContainer(
resolvedImageName.asCompatibleSubstituteFor("opensearchproject/opensearch"));
String hostName = null;
if (useSharedNetwork) {
ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_OPENSEARCH);
hostName = ConfigureUtil.configureSharedNetwork(container, DEV_SERVICE_OPENSEARCH);
}

container.addEnv("bootstrap.memory_lock", "true");
Expand All @@ -264,7 +272,11 @@ private GenericContainer<?> createOpensearchContainer(ElasticsearchDevServicesBu
// Considering dev services are transient and not intended for production by nature,
// we'll just set some hardcoded password.
container.addEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", "NotActua11y$trongPa$$word");
return container;

return new CreatedContainer(container, hostName);
}

private record CreatedContainer(GenericContainer<?> genericContainer, String hostName) {
}

private DockerImageName resolveImageName(ElasticsearchDevServicesBuildTimeConfig config,
Expand Down

0 comments on commit 82eb6f4

Please sign in to comment.