From 3a0909368cc10e41ef9f6a444cbb6e70bd499a37 Mon Sep 17 00:00:00 2001 From: Sandip Gahlot Date: Wed, 28 Jul 2021 10:11:36 -0400 Subject: [PATCH 1/2] DBAAS-42: Added support for MongoDB service binding by reading the given mandatory/optional properties (+ tests) and making the correct connection string DBAAS-42: Using correct property for quarkus mongodb connection string. Removed MockitoExtension --- extensions/mongodb-client/deployment/pom.xml | 2 +- .../deployment/MongoClientProcessor.java | 15 +- extensions/mongodb-client/runtime/pom.xml | 19 +- .../runtime/MongoServiceBindingConverter.java | 140 ++++++++++++ ...ce.binding.runtime.ServiceBindingConverter | 1 + .../MongoServiceBindingConverterTest.java | 213 ++++++++++++++++++ .../org.mockito.plugins.MockMaker | 1 + .../service-binding/all-props/database | 1 + .../resources/service-binding/all-props/host | 1 + .../service-binding/all-props/password | 1 + .../resources/service-binding/all-props/port | 1 + .../service-binding/all-props/provider | 1 + .../resources/service-binding/all-props/type | 1 + .../service-binding/all-props/username | 1 + .../service-binding/no-port/database | 1 + .../resources/service-binding/no-port/host | 1 + .../service-binding/no-port/password | 1 + .../service-binding/no-port/provider | 1 + .../resources/service-binding/no-port/type | 1 + .../service-binding/no-port/username | 1 + .../service-binding/no-user/database | 1 + .../resources/service-binding/no-user/host | 1 + .../resources/service-binding/no-user/port | 1 + .../service-binding/no-user/provider | 1 + .../resources/service-binding/no-user/type | 1 + 25 files changed, 406 insertions(+), 3 deletions(-) create mode 100644 extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverter.java create mode 100644 extensions/mongodb-client/runtime/src/main/resources/META-INF/services/io.quarkus.kubernetes.service.binding.runtime.ServiceBindingConverter create mode 100644 extensions/mongodb-client/runtime/src/test/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverterTest.java create mode 100644 extensions/mongodb-client/runtime/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/database create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/host create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/password create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/port create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/provider create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/type create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/username create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/database create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/host create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/password create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/provider create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/type create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/username create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/database create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/host create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/port create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/provider create mode 100644 extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/type diff --git a/extensions/mongodb-client/deployment/pom.xml b/extensions/mongodb-client/deployment/pom.xml index 9def469e33a9f..926c3f95a13c1 100644 --- a/extensions/mongodb-client/deployment/pom.xml +++ b/extensions/mongodb-client/deployment/pom.xml @@ -123,4 +123,4 @@ - + \ No newline at end of file diff --git a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java index 3eb4fd9546eb4..700c5173ce642 100644 --- a/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java +++ b/extensions/mongodb-client/deployment/src/main/java/io/quarkus/mongodb/deployment/MongoClientProcessor.java @@ -51,6 +51,7 @@ import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.deployment.builditem.SslNativeConfigBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem; import io.quarkus.mongodb.MongoClientName; import io.quarkus.mongodb.reactive.ReactiveMongoClient; @@ -58,6 +59,7 @@ import io.quarkus.mongodb.runtime.MongoClientRecorder; import io.quarkus.mongodb.runtime.MongoClientSupport; import io.quarkus.mongodb.runtime.MongoClients; +import io.quarkus.mongodb.runtime.MongoServiceBindingConverter; import io.quarkus.mongodb.runtime.MongodbConfig; import io.quarkus.runtime.metrics.MetricsFactory; import io.quarkus.smallrye.health.deployment.spi.HealthBuildItem; @@ -69,6 +71,8 @@ public class MongoClientProcessor { private static final DotName MONGO_CLIENT = DotName.createSimple(MongoClient.class.getName()); private static final DotName REACTIVE_MONGO_CLIENT = DotName.createSimple(ReactiveMongoClient.class.getName()); + private static final String SERVICE_BINDING_INTERFACE_NAME = "io.quarkus.kubernetes.service.binding.runtime.ServiceBindingConverter"; + @BuildStep AdditionalIndexedClassesBuildItem includeBsonTypesToIndex() { return new AdditionalIndexedClassesBuildItem( @@ -371,4 +375,13 @@ HealthBuildItem addHealthCheck(MongoClientBuildTimeConfig buildTimeConfig) { return new HealthBuildItem("io.quarkus.mongodb.health.MongoHealthCheck", buildTimeConfig.healthEnabled); } -} + + @BuildStep + void registerServiceBinding(Capabilities capabilities, BuildProducer buildProducer) { + if (capabilities.isPresent(Capability.KUBERNETES_SERVICE_BINDING)) { + buildProducer.produce( + new ServiceProviderBuildItem(SERVICE_BINDING_INTERFACE_NAME, + MongoServiceBindingConverter.class.getName())); + } + } +} \ No newline at end of file diff --git a/extensions/mongodb-client/runtime/pom.xml b/extensions/mongodb-client/runtime/pom.xml index 06c4edf05a1ae..47534584f7d90 100644 --- a/extensions/mongodb-client/runtime/pom.xml +++ b/extensions/mongodb-client/runtime/pom.xml @@ -44,6 +44,11 @@ org.mongodb mongodb-crypt + + io.quarkus + quarkus-kubernetes-service-binding + true + @@ -100,6 +105,18 @@ awaitility test + + + org.mockito + mockito-core + test + + + org.mockito + mockito-junit-jupiter + test + + + + io.quarkus + quarkus-kubernetes-service-binding + io.quarkus quarkus-junit5 @@ -69,6 +74,19 @@ + + io.quarkus + quarkus-kubernetes-service-binding-deployment + ${project.version} + pom + test + + + * + * + + + @@ -84,6 +102,57 @@ + + maven-surefire-plugin + + + + ${project.basedir}/src/test/resources/k8s-sb + + + + + + + + native + + + native + + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + ${project.build.directory}/${project.build.finalName}-runner + + + + ${project.basedir}/src/test/resources/k8s-sb + + + + + + + + + + + native + + + diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java index 05417efbf3b55..8fcbb28da9270 100644 --- a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java @@ -18,11 +18,13 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @QuarkusTest @DisabledOnOs(OS.WINDOWS) +@QuarkusTestResource(MongoTestResource.class) class MongoDbRestDataPanacheTest { private Author dostoevsky; diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java new file mode 100644 index 0000000000000..f086d7a74df25 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java @@ -0,0 +1,84 @@ +package io.quarkus.it.mongodb.rest.data.panache; + +import java.util.Collections; +import java.util.Map; + +import org.jboss.logging.Logger; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.IMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class MongoTestResource implements QuarkusTestResourceLifecycleManager { + + private static final Logger LOGGER = Logger.getLogger(MongoTestResource.class); + private static MongodExecutable MONGO; + + @Override + public Map start() { + try { + //JDK bug workaround + //https://github.com/quarkusio/quarkus/issues/14424 + //force class init to prevent possible deadlock when done by mongo threads + Class.forName("sun.net.ext.ExtendedSocketOptions", true, ClassLoader.getSystemClassLoader()); + } catch (ClassNotFoundException e) { + } + try { + Version.Main version = Version.Main.V4_0; + int port = 37017; + LOGGER.infof("Starting Mongo %s on port %s", version, port); + IMongodConfig config = new MongodConfigBuilder() + .version(version) + .net(new Net(port, Network.localhostIsIPv6())) + .build(); + MONGO = getMongodExecutable(config); + try { + MONGO.start(); + } catch (Exception e) { + //every so often mongo fails to start on CI runs + //see if this helps + Thread.sleep(1000); + MONGO.start(); + } + + // don't return any properties, since we want Service Binding to configure Quarkus + return Collections.emptyMap(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private MongodExecutable getMongodExecutable(IMongodConfig config) { + try { + return doGetExecutable(config); + } catch (Exception e) { + // sometimes the download process can timeout so just sleep and try again + try { + Thread.sleep(1000); + } catch (InterruptedException ignored) { + + } + return doGetExecutable(config); + } + } + + private MongodExecutable doGetExecutable(IMongodConfig config) { + return MongodStarter.getDefaultInstance().prepare(config); + } + + private void initData() { + + } + + @Override + public void stop() { + if (MONGO != null) { + MONGO.stop(); + } + } +} diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database new file mode 100644 index 0000000000000..9daeafb9864cf --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database @@ -0,0 +1 @@ +test diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host new file mode 100644 index 0000000000000..2fbb50c4a8dc7 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host @@ -0,0 +1 @@ +localhost diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port new file mode 100644 index 0000000000000..72ae75d500d06 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port @@ -0,0 +1 @@ +37017 diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider new file mode 100644 index 0000000000000..98c8f47051d4b --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider @@ -0,0 +1 @@ +atlas \ No newline at end of file diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type new file mode 100644 index 0000000000000..e73baecce656b --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type @@ -0,0 +1 @@ +mongodb \ No newline at end of file