Skip to content

Commit

Permalink
Use the MongoDB Java driver's DNS SPI to enable mongo+srv:// connecti…
Browse files Browse the repository at this point in the history
…on strings in native mode

fixes #26387
  • Loading branch information
evanchooly committed Jul 25, 2022
1 parent b2574a8 commit c4d6bdc
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.mongodb.client.model.changestream.UpdateDescription;
import com.mongodb.event.CommandListener;
import com.mongodb.event.ConnectionPoolListener;
import com.mongodb.spi.dns.DnsClientProvider;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
Expand All @@ -47,11 +48,11 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.annotations.Weak;
import io.quarkus.deployment.builditem.AdditionalIndexedClassesBuildItem;
import io.quarkus.deployment.builditem.AllowJNDIBuildItem;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.SslNativeConfigBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
Expand All @@ -64,6 +65,8 @@
import io.quarkus.mongodb.runtime.MongoClients;
import io.quarkus.mongodb.runtime.MongoServiceBindingConverter;
import io.quarkus.mongodb.runtime.MongodbConfig;
import io.quarkus.mongodb.runtime.dns.MongoDnsClient;
import io.quarkus.mongodb.runtime.dns.MongoDnsClientProvider;
import io.quarkus.runtime.metrics.MetricsFactory;
import io.quarkus.smallrye.health.deployment.spi.HealthBuildItem;

Expand All @@ -76,12 +79,6 @@ public class MongoClientProcessor {

private static final String SERVICE_BINDING_INTERFACE_NAME = "io.quarkus.kubernetes.service.binding.runtime.ServiceBindingConverter";

@BuildStep
AllowJNDIBuildItem enableJndi() {
//unfortunately mongo+srv protocol needs JNDI
return new AllowJNDIBuildItem();
}

@BuildStep
AdditionalIndexedClassesBuildItem includeBsonTypesToIndex() {
return new AdditionalIndexedClassesBuildItem(
Expand All @@ -99,6 +96,18 @@ AdditionalIndexedClassesBuildItem includeBsonTypesToIndex() {
"org.bson.types.Symbol");
}

@BuildStep
AdditionalIndexedClassesBuildItem includeDnsTypesToIndex() {
return new AdditionalIndexedClassesBuildItem(
MongoDnsClientProvider.class.getName(),
MongoDnsClient.class.getName());
}

@BuildStep
public void registerDnsProvider(BuildProducer<NativeImageResourceBuildItem> nativeProducer) {
nativeProducer.produce(new NativeImageResourceBuildItem("META-INF/services/" + DnsClientProvider.class.getName()));
}

@BuildStep
CodecProviderBuildItem collectCodecProviders(CombinedIndexBuildItem indexBuildItem) {
Collection<ClassInfo> codecProviderClasses = indexBuildItem.getIndex()
Expand Down
5 changes: 5 additions & 0 deletions extensions/mongodb-client/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.mongodb.runtime;

import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.DNS_LOG_ACTIVITY;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.DNS_LOOKUP_TIMEOUT;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.DNS_SERVER;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.DNS_SERVER_PORT;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.NATIVE_DNS_LOG_ACTIVITY;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.NATIVE_DNS_LOOKUP_TIMEOUT;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.NATIVE_DNS_SERVER;
import static io.quarkus.mongodb.runtime.dns.MongoDnsClient.NATIVE_DNS_SERVER_PORT;

import java.util.Map;

import io.smallrye.config.RelocateConfigSourceInterceptor;

public class MongoConfigSourceInterceptor extends RelocateConfigSourceInterceptor {
public MongoConfigSourceInterceptor() {
super(Map.of(
DNS_LOG_ACTIVITY, NATIVE_DNS_LOG_ACTIVITY,
DNS_LOOKUP_TIMEOUT, NATIVE_DNS_LOOKUP_TIMEOUT,
DNS_SERVER, NATIVE_DNS_SERVER,
DNS_SERVER_PORT, NATIVE_DNS_SERVER_PORT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@

@ConfigRoot(name = MongodbConfig.CONFIG_NAME, phase = ConfigPhase.RUN_TIME)
public class MongodbConfig {

public static final String CONFIG_NAME = "mongodb";
@Deprecated
public static final String NATIVE_DNS_LOG_ACTIVITY = "native.dns.log-activity";
public static final String DNS_LOG_ACTIVITY = "dns.log-activity";
@Deprecated
public static final String NATIVE_DNS_SERVER_HOST = "native.dns.server-host";
public static final String DNS_SERVER_HOST = "dns.server-host";
@Deprecated
public static final String NATIVE_DNS_SERVER_PORT = "native.dns.server-port";
public static final String DNS_SERVER_PORT = "dns.server-port";
@Deprecated
public static final String NATIVE_DNS_LOOKUP_TIMEOUT = "native.dns.lookup-timeout";
public static final String DNS_LOOKUP_TIMEOUT = "dns.lookup-timeout";

/**
* The default mongo client connection.
Expand Down Expand Up @@ -52,36 +63,75 @@ public class MongodbConfig {
*
* <strong>IMPORTANT:</strong> The resolution may be different in JVM mode using the default (JNDI-based) DNS resolver,
* and in native mode. This feature is experimental.
*
* @deprecated This resolver is always used
*/
@Deprecated
@ConfigItem(name = "native.dns.use-vertx-dns-resolver", defaultValue = "false")
public boolean useVertxDnsResolverInNativeMode;

/**
* If {@code native.dns.use-vertx-dns-resolver} is set to {@code true}, this property configures the DNS server.
* If the server is not set, it tries to read the first {@code nameserver} from {@code /etc/resolv.conf} (if the
* file exists), otherwise fallback to the default.
*
* @deprecated this property has been deprecated in favor of {@link #dnsServer}
*/
@ConfigItem(name = "native.dns.server-host")
@Deprecated
@ConfigItem(name = NATIVE_DNS_SERVER_HOST)
public Optional<String> dnsServerInNativeMode;

/**
* This property configures the DNS server. If the server is not set, it tries to read the first {@code nameserver} from
* {@code /etc /resolv.conf} (if the file exists), otherwise fallback to the default.
*/
@ConfigItem(name = DNS_SERVER_HOST)
public Optional<String> dnsServer;

/**
* If {@code native.dns.use-vertx-dns-resolver} is set to {@code true}, this property configures the DNS server port.
* If not set, uses the system DNS resolver.
*
* @deprecated this property has been deprecated in favor of {@link #dnsServerPort}
*/
@ConfigItem(name = "native.dns.server-port", defaultValue = "53")
@Deprecated
@ConfigItem(name = NATIVE_DNS_SERVER_PORT, defaultValue = "53")
public OptionalInt dnsServerPortInNativeMode;
/**
* This property configures the DNS server port.
*/
@ConfigItem(name = DNS_SERVER_PORT, defaultValue = "53")
public OptionalInt dnsServerPort;

/**
* If {@code native.dns.use-vertx-dns-resolver} is set to {@code true}, this property configures the DNS lookup timeout
* duration.
*
* @deprecated this property has been deprecated in favor of {@link #dnsLookupTimeout}
*/
@ConfigItem(name = "native.dns.lookup-timeout", defaultValue = "5s")
@Deprecated
@ConfigItem(name = NATIVE_DNS_LOOKUP_TIMEOUT, defaultValue = "5s")
public Duration dnsLookupTimeoutInNativeMode;

/**
* If {@code native.dns.use-vertx-dns-resolver} is set to {@code true}, this property configures the DNS lookup timeout
* duration.
*/
@ConfigItem(name = DNS_LOOKUP_TIMEOUT, defaultValue = "5s")
public Duration dnsLookupTimeout;

/**
* If {@code native.dns.use-vertx-dns-resolver} is set to {@code true}, this property enables the logging ot the
* DNS lookup. It can be useful to understand why the lookup fails.
*
* @deprecated this property has been deprecated in favor of {@link #dnsLookupLogActivity}
*/
@ConfigItem(name = "native.dns.log-activity", defaultValue = "false")
@Deprecated
@ConfigItem(name = NATIVE_DNS_LOG_ACTIVITY, defaultValue = "false")
public Optional<Boolean> dnsLookupLogActivityInNativeMode;

/**
* This property enables the logging ot the DNS lookup. It can be useful to understand why the lookup fails.
*/
@ConfigItem(name = DNS_LOG_ACTIVITY, defaultValue = "false")
public Optional<Boolean> dnsLookupLogActivity;
}

This file was deleted.

Loading

0 comments on commit c4d6bdc

Please sign in to comment.