Skip to content

Commit

Permalink
Merge pull request #45701 from mcruzdev/feature/config-es-rest-client…
Browse files Browse the repository at this point in the history
…-common

Convert elasticsearch-rest-client-common modules to use @ConfigMapping
  • Loading branch information
gsmet authored Jan 19, 2025
2 parents 6488f90 + ba4f41b commit 83c5afc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public DevServicesResultBuildItem startElasticsearchDevService(
try {
boolean useSharedNetwork = DevServicesSharedNetworkBuildItem.isSharedNetworkRequired(devServicesConfig,
devServicesSharedNetworkBuildItem);
devService = startElasticsearchDevServices(dockerStatusBuildItem, configuration.devservices, buildItemsConfig,
devService = startElasticsearchDevServices(dockerStatusBuildItem, configuration.devservices(), buildItemsConfig,
launchMode, useSharedNetwork, devServicesConfig.timeout());
if (devService == null) {
compressor.closeAndDumpCaptured();
Expand Down Expand Up @@ -161,7 +161,7 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
ElasticsearchDevServicesBuildTimeConfig config,
DevservicesElasticsearchBuildItemsConfiguration buildItemConfig,
LaunchModeBuildItem launchMode, boolean useSharedNetwork, Optional<Duration> timeout) throws BuildException {
if (!config.enabled.orElse(true)) {
if (!config.enabled().orElse(true)) {
// explicitly disabled
log.debug("Not starting Dev Services for Elasticsearch, as it has been disabled in the config.");
return null;
Expand All @@ -185,8 +185,8 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
DockerImageName resolvedImageName = resolveImageName(config, resolvedDistribution);

final Optional<ContainerAddress> maybeContainerAddress = elasticsearchContainerLocator.locateContainer(
config.serviceName,
config.shared,
config.serviceName(),
config.shared(),
launchMode.getLaunchMode());

// Starting the server
Expand All @@ -197,17 +197,17 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearchDevServic
: createOpensearchContainer(config, resolvedImageName, useSharedNetwork);
GenericContainer<?> container = createdContainer.genericContainer();

if (config.serviceName != null) {
container.withLabel(DEV_SERVICE_LABEL, config.serviceName);
if (config.serviceName() != null) {
container.withLabel(DEV_SERVICE_LABEL, config.serviceName());
}
if (config.port.isPresent()) {
container.setPortBindings(List.of(config.port.get() + ":" + ELASTICSEARCH_PORT));
if (config.port().isPresent()) {
container.setPortBindings(List.of(config.port().get() + ":" + ELASTICSEARCH_PORT));
}
timeout.ifPresent(container::withStartupTimeout);

container.withEnv(config.containerEnv);
container.withEnv(config.containerEnv());

container.withReuse(config.reuse);
container.withReuse(config.reuse());

container.start();

Expand Down Expand Up @@ -246,7 +246,7 @@ private CreatedContainer createElasticsearchContainer(ElasticsearchDevServicesBu
// and lead to problems on large disks with little space left.
// 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);
container.addEnv("ES_JAVA_OPTS", config.javaOpts());

return new CreatedContainer(container, hostName);
}
Expand All @@ -267,7 +267,7 @@ private CreatedContainer createOpensearchContainer(ElasticsearchDevServicesBuild
// which will never happen since we only have one node.
// See https://opensearch.org/docs/latest/api-reference/cluster-api/cluster-settings/
container.addEnv("cluster.routing.allocation.disk.threshold_enabled", "false");
container.addEnv("OPENSEARCH_JAVA_OPTS", config.javaOpts);
container.addEnv("OPENSEARCH_JAVA_OPTS", config.javaOpts());
// OpenSearch 2.12 and later requires an admin password, or it won't start.
// Considering dev services are transient and not intended for production by nature,
// we'll just set some hardcoded password.
Expand All @@ -281,7 +281,7 @@ private record CreatedContainer(GenericContainer<?> genericContainer, String hos

private DockerImageName resolveImageName(ElasticsearchDevServicesBuildTimeConfig config,
Distribution resolvedDistribution) {
return DockerImageName.parse(config.imageName.orElseGet(() -> ConfigureUtil.getDefaultImageNameFor(
return DockerImageName.parse(config.imageName().orElseGet(() -> ConfigureUtil.getDefaultImageNameFor(
Distribution.ELASTIC.equals(resolvedDistribution)
? DEV_SERVICE_ELASTICSEARCH
: DEV_SERVICE_OPENSEARCH)));
Expand All @@ -290,12 +290,12 @@ private DockerImageName resolveImageName(ElasticsearchDevServicesBuildTimeConfig
private Distribution resolveDistribution(ElasticsearchDevServicesBuildTimeConfig config,
DevservicesElasticsearchBuildItemsConfiguration buildItemConfig) throws BuildException {
// First, let's see if it was explicitly configured:
if (config.distribution.isPresent()) {
return config.distribution.get();
if (config.distribution().isPresent()) {
return config.distribution().get();
}
// Now let's see if we can guess it from the image:
if (config.imageName.isPresent()) {
String imageNameRepository = DockerImageName.parse(config.imageName.get()).getRepository()
if (config.imageName().isPresent()) {
String imageNameRepository = DockerImageName.parse(config.imageName().get()).getRepository()
.toLowerCase(Locale.ROOT);
if (imageNameRepository.contains(DEV_SERVICE_OPENSEARCH)) {
return Distribution.OPENSEARCH;
Expand All @@ -306,7 +306,7 @@ private Distribution resolveDistribution(ElasticsearchDevServicesBuildTimeConfig
// no luck guessing so let's ask user to be more explicit:
throw new BuildException(
"Wasn't able to determine the distribution of the search service based on the provided image name ["
+ config.imageName.get()
+ config.imageName().get()
+ "]. Please specify the distribution explicitly.",
Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package io.quarkus.elasticsearch.restclient.common.deployment;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(name = "elasticsearch", phase = ConfigPhase.BUILD_TIME)
public class ElasticsearchCommonBuildTimeConfig {
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
@ConfigMapping(prefix = "quarkus.elasticsearch")
public interface ElasticsearchCommonBuildTimeConfig {

/**
* Dev Services
* <p>
* Dev Services allows Quarkus to automatically start Elasticsearch in dev and test mode.
*/
@ConfigItem
@ConfigDocSection(generated = true)
public ElasticsearchDevServicesBuildTimeConfig devservices;
ElasticsearchDevServicesBuildTimeConfig devservices();

@ConfigGroup
public static class ElasticsearchDevServicesBuildTimeConfig {
interface ElasticsearchDevServicesBuildTimeConfig {
/**
* Whether this Dev Service should start with the application in dev mode or tests.
*
Expand All @@ -33,16 +33,14 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
*
* @asciidoclet
*/
@ConfigItem
public Optional<Boolean> enabled = Optional.empty();
Optional<Boolean> enabled();

/**
* Optional fixed port the dev service will listen to.
* <p>
* If not defined, the port will be chosen randomly.
*/
@ConfigItem
public Optional<Integer> port;
Optional<Integer> port();

/**
* The Elasticsearch distribution to use.
Expand All @@ -53,8 +51,7 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
*
* @asciidoclet
*/
@ConfigItem
public Optional<Distribution> distribution;
Optional<Distribution> distribution();

/**
* The Elasticsearch container image to use.
Expand All @@ -66,16 +63,15 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
*
* @asciidoclet
*/
@ConfigItem
public Optional<String> imageName;
Optional<String> imageName();

/**
* The value for the ES_JAVA_OPTS env variable.
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "-Xms512m -Xmx1g")
public String javaOpts;
@WithDefault("-Xms512m -Xmx1g")
String javaOpts();

/**
* Whether the Elasticsearch server managed by Quarkus Dev Services is shared.
Expand All @@ -89,8 +85,8 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
* <p>
* Container sharing is only used in dev mode.
*/
@ConfigItem(defaultValue = "true")
public boolean shared;
@WithDefault("true")
boolean shared();

/**
* The value of the {@code quarkus-dev-service-elasticsearch} label attached to the started container.
Expand All @@ -103,15 +99,14 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
* <p>
* This property is used when you need multiple shared Elasticsearch servers.
*/
@ConfigItem(defaultValue = "elasticsearch")
public String serviceName;
@WithDefault("elasticsearch")
String serviceName();

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
@ConfigDocMapKey("environment-variable-name")
public Map<String, String> containerEnv;
Map<String, String> containerEnv();

/**
* Whether to keep Dev Service containers running *after a dev mode session or test suite execution*
Expand All @@ -135,33 +130,10 @@ public static class ElasticsearchDevServicesBuildTimeConfig {
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "true")
public boolean reuse;
@WithDefault("true")
boolean reuse();

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ElasticsearchDevServicesBuildTimeConfig that = (ElasticsearchDevServicesBuildTimeConfig) o;
return Objects.equals(shared, that.shared)
&& Objects.equals(enabled, that.enabled)
&& Objects.equals(port, that.port)
&& Objects.equals(distribution, that.distribution)
&& Objects.equals(imageName, that.imageName)
&& Objects.equals(javaOpts, that.javaOpts)
&& Objects.equals(serviceName, that.serviceName)
&& Objects.equals(containerEnv, that.containerEnv)
&& Objects.equals(reuse, that.reuse);
}

@Override
public int hashCode() {
return Objects.hash(enabled, port, distribution, imageName, javaOpts, shared, serviceName, containerEnv, reuse);
}

public enum Distribution {
enum Distribution {
ELASTIC,
OPENSEARCH
}
Expand Down
3 changes: 0 additions & 3 deletions extensions/elasticsearch-rest-client-common/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit 83c5afc

Please sign in to comment.