Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for service registry service binding #21618

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/apicurio-registry-avro/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-avro-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.quarkus.apicurio.registry.avro;

import java.io.IOException;

import io.apicurio.rest.client.spi.ApicurioHttpClientProvider;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -8,6 +11,7 @@
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.vertx.deployment.VertxBuildItem;

public class ApicurioRegistryAvroProcessor {
Expand All @@ -24,25 +28,51 @@ public void apicurioRegistryAvro(BuildProducer<ReflectiveClassBuildItem> reflect
"io.apicurio.registry.serde.avro.AvroKafkaDeserializer",
"io.apicurio.registry.serde.avro.AvroKafkaSerializer"));

reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, false,
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true,
"io.apicurio.registry.serde.strategy.SimpleTopicIdStrategy",
"io.apicurio.registry.serde.strategy.TopicIdStrategy",
"io.apicurio.registry.serde.avro.DefaultAvroDatumProvider",
"io.apicurio.registry.serde.avro.ReflectAvroDatumProvider",
"io.apicurio.registry.serde.avro.strategy.RecordIdStrategy",
"io.apicurio.registry.serde.avro.strategy.TopicRecordIdStrategy"));

reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, false,
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true,
"io.apicurio.registry.serde.DefaultSchemaResolver",
"io.apicurio.registry.serde.DefaultIdHandler",
"io.apicurio.registry.serde.Legacy4ByteIdHandler",
"io.apicurio.registry.serde.fallback.DefaultFallbackArtifactProvider",
"io.apicurio.registry.serde.headers.DefaultHeadersHandler"));

reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true,
"io.apicurio.rest.client.auth.exception.NotAuthorizedException",
"io.apicurio.rest.client.auth.exception.ForbiddenException",
"io.apicurio.rest.client.auth.exception.AuthException",
"io.apicurio.rest.client.auth.exception.AuthErrorHandler",
"io.apicurio.rest.client.auth.request.TokenRequestsProvider",
"io.apicurio.rest.client.request.Request",
"io.apicurio.rest.client.auth.AccessTokenResponse",
"io.apicurio.rest.client.auth.Auth",
"io.apicurio.rest.client.auth.BasicAuth",
"io.apicurio.rest.client.auth.OidcAuth"));
}

@BuildStep
void registerSPIClient(BuildProducer<ServiceProviderBuildItem> services) throws IOException {

services.produce(
new ServiceProviderBuildItem(ApicurioHttpClientProvider.class.getName(),
"io.apicurio.rest.client.VertxHttpClientProvider"));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void apicurioRegistryClient(VertxBuildItem vertx, ApicurioRegistryClient client) {
client.setup(vertx.getVertx());
}

@BuildStep
ExtensionSslNativeSupportBuildItem enableSslInNative() {
return new ExtensionSslNativeSupportBuildItem(Feature.APICURIO_REGISTRY_AVRO);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.apicurio.registry.avro.binding;

import io.quarkus.apicurio.registry.binding.ServiceRegistryBindingConverter;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;

class ServiceRegistryBindingExtensionProcessor {

@BuildStep
void registerServiceBinding(Capabilities capabilities,
BuildProducer<ServiceProviderBuildItem> serviceProvider) {
if (capabilities.isPresent(Capability.KUBERNETES_SERVICE_BINDING)) {
serviceProvider.produce(
new ServiceProviderBuildItem("io.quarkus.apicurio.registry.binding.ServiceRegistryBindingConverter",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to that PR, but maybe we should have an SPI in the service binding extension with a dedicated build item (which will also about the capability check)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean here. Can you elaborate a bit please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine that instead to do:

 if (capabilities.isPresent(Capability.KUBERNETES_SERVICE_BINDING)) {
            serviceProvider.produce(
                    new ServiceProviderBuildItem("io.quarkus.apicurio.registry.binding.ServiceRegistryBindingConverter", ServiceRegistryBindingConverter.class.getName()));

You do:

producer.produce(new ServiceBindingConverter(ServiceRegistryBindingConverter.class));

Then the SBO processor would consumer this new build item and register the SPI provider.

Of course to avoid a dependency on the SBO extension deployment model, we would need to create an SPI module.

ServiceRegistryBindingConverter.class.getName()));
}
}
}
12 changes: 11 additions & 1 deletion extensions/apicurio-registry-avro/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
<groupId>io.apicurio</groupId>
<artifactId>apicurio-common-rest-client-vertx</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
secondsun marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>quarkus-kubernetes-service-binding</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
Expand All @@ -41,6 +46,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
Comment on lines +50 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this now needed for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.quarkus.apicurio.registry.binding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.kubernetes.service.binding.runtime.ServiceBinding;
import io.quarkus.kubernetes.service.binding.runtime.ServiceBindingConfigSource;
import io.quarkus.kubernetes.service.binding.runtime.ServiceBindingConverter;

public class ServiceRegistryBindingConverter implements ServiceBindingConverter {

private static Logger LOG = Logger.getLogger(ServiceRegistryBindingConverter.class.getName());

private static final String INCOMING_PREFIX = "mp.messaging.incoming.";
private static final String OUTGOING_PREFIX = "mp.messaging.outgoing.";

@Override
public Optional<ServiceBindingConfigSource> convert(List<ServiceBinding> serviceBindings) {
var matchingByType = ServiceBinding.singleMatchingByType("serviceregistry", serviceBindings);
Config config = ConfigProvider.getConfig();
if (matchingByType.isEmpty()) {
return Optional.empty();
}

var binding = matchingByType.get();

List<String> channels = extractChannels(config);

Map<String, String> properties = new HashMap<>();

String registryUrl = binding.getProperties().get("registryUrl");
if (registryUrl == null) {
registryUrl = binding.getProperties().get("registryurl");
}
if (registryUrl != null) {
properties.put("kafka.apicurio.registry.url", registryUrl);
}

for (String channel : channels) {

String prefix = channel;

String oauthTokenUrl = binding.getProperties().get("oauthTokenUrl");
if (oauthTokenUrl == null) {
oauthTokenUrl = binding.getProperties().get("oauthtokenurl");
}
if (oauthTokenUrl != null) {
properties.put(prefix + "apicurio.auth.service.token.endpoint", oauthTokenUrl);
}

String clientId = binding.getProperties().get("clientId");
if (clientId == null) {
clientId = binding.getProperties().get("clientid");
}
if (clientId != null) {
properties.put(prefix + "apicurio.auth.client.id", clientId);
}

String clientSecret = binding.getProperties().get("clientSecret");
if (clientSecret == null) {
clientSecret = binding.getProperties().get("clientsecret");
}
if (clientSecret != null) {
properties.put(prefix + "apicurio.auth.client.secret", clientSecret);
}
if (registryUrl != null) {
properties.put(prefix + "apicurio.registry.url", registryUrl);
}
}

return Optional.of(new ServiceBindingConfigSource("serviceregistry-k8s-service-binding-source", properties));
}

private List<String> extractChannels(Config configIn) {

var list = new ArrayList<String>();

for (String propertyName : configIn.getPropertyNames()) {
secondsun marked this conversation as resolved.
Show resolved Hide resolved
if (propertyName.startsWith(INCOMING_PREFIX)) {
var channelAndProp = StringUtils.substringAfter(propertyName, INCOMING_PREFIX);
//remove property
var channelName = StringUtils.substringBefore(channelAndProp, ".");
if (!StringUtils.isBlank(channelName))
list.add(INCOMING_PREFIX + channelName + ".");
} else if (propertyName.startsWith(OUTGOING_PREFIX)) {
var channelAndProp = StringUtils.substringAfter(propertyName, OUTGOING_PREFIX);
//remove property
var channelName = StringUtils.substringBefore(channelAndProp, ".");
if (!StringUtils.isBlank(channelName))
list.add(OUTGOING_PREFIX + channelName + ".");
}
}
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.apicurio.registry.binding.ServiceRegistryBindingConverter