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

[KOGITO-9145] Create an implementation of the Service Discovery catal… #3166

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog-deployment</artifactId>
<name>Kogito Add-On MicroProfile Config Service Catalog - Deployment</name>

<dependencies>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-openshift-client-deployment</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${version.io.quarkus}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
20 changes: 20 additions & 0 deletions quarkus/addons/microprofile-config-service-catalog/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-quarkus-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog-parent</artifactId>
<name>Kogito Add-On MicroProfile Config Service Catalog - Parent</name>
<packaging>pom</packaging>

<modules>
<module>deployment</module>
<module>runtime</module>
Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to register these modules in the kogito-bom. Take a look at https://github.com/kiegroup/kogito-runtimes/blob/e37b961eb5fec25b4545a7937b5f067fee331ade/kogito-bom/pom.xml#L318 to use the Fabric8 implementation as a reference.

</modules>
</project>
136 changes: 136 additions & 0 deletions quarkus/addons/microprofile-config-service-catalog/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>kogito-addons-quarkus-microprofile-config-service-catalog</artifactId>
<name>Kogito Add-On MicroProfile Config Service Catalog</name>
<properties>
<!-- Other properties -->
<version.io.quarkus>2.16.8.Final</version.io.quarkus>
<!-- Other properties -->
</properties>
<dependencies>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-kubernetes-service-catalog</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-openshift-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-addons-kubernetes-service-catalog</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-kubernetes-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-openshift-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
<version>${version.io.quarkus}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>extension-descriptor</goal>
</goals>
<configuration>
<deployment>${project.groupId}:${project.artifactId}-deployment:${project.version}
</deployment>
<capabilities>
<provides>org.kie.kogito.addons.quarkus.microprofile.config.service.catalog</provides>
</capabilities>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${version.io.quarkus}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.addons.quarkus.microprofile.config.service.catalog;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalog;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalogKey;

@ApplicationScoped
public class MicroProfileConfigServiceCatalog implements KubernetesServiceCatalog {
Copy link
Contributor

@hbelmiro hbelmiro Aug 8, 2023

Choose a reason for hiding this comment

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

  • You don't need to cache the values in the services attribute. Just get them from config directly.

  • You can inject Config instead of getting the instance from ConfigProvider.

I would rewrite this class as:

@ApplicationScoped
public class MicroProfileConfigServiceCatalog implements KubernetesServiceCatalog {

    @Inject
    Config config;

    @Override
    public Optional<URI> getServiceAddress(KubernetesServiceCatalogKey key) {
        return config.getOptionalValue(key.getProtocol().getValue() + ":" + key.getCoordinates(), String.class)
                .map(value -> {
                    try {
                        return new URI(value);
                    } catch (URISyntaxException e) {
                        // TODO: handle
                        throw new RuntimeException(e);
                    }
                });
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hbelmiro Thank you for the suggestions. I will test it after making necessary changes.


private final Map<KubernetesServiceCatalogKey, URI> services = new ConcurrentHashMap<>();

@Override
public Optional<URI> getServiceAddress(KubernetesServiceCatalogKey key) {
URI serviceUrl = services.get(key);

if (serviceUrl == null) {
serviceUrl = fetchServiceAddressFromConfig(key);
services.put(key, serviceUrl);
}

return Optional.ofNullable(serviceUrl);
}

private URI fetchServiceAddressFromConfig(KubernetesServiceCatalogKey key) {
try {
Config config = ConfigProvider.getConfig();
String configValue = config.getValue(key.getProtocol().getValue() + ":" + key.getCoordinates(), String.class);
return new URI(configValue);
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.addons.quarkus.microprofile.config.service.catalog;

import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalog;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalogProvider;

public class MicroProfileConfigServiceCatalogProvider implements KubernetesServiceCatalogProvider {
Copy link
Contributor

Choose a reason for hiding this comment

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


@Override
public KubernetesServiceCatalog create() {
return new MicroProfileConfigServiceCatalog();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.addons.quarkus.microprofile.config.service.catalog;

import javax.inject.Inject;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalogTest;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import io.quarkus.test.junit.QuarkusTest;

import static org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol.KNATIVE;
import static org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol.KUBERNETES;
import static org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol.OPENSHIFT;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@QuarkusTest
@ExtendWith(MockitoExtension.class)
public class MicroProfileConfigServiceCatalogTest extends KubernetesServiceCatalogTest {

private static final String KNATIVE_URI = "http://serverless-workflow-greeting-quarkus.test.10.99.154.147.sslip.io";
private static final String KUBERNETES_URI = "http://serverless-workflow-greeting-quarkus-kubernetes.test.10.99.154.147.sslip.io";
private static final String OPENSHIFT_URI = "http://serverless-workflow-greeting-quarkus-openshift.test.10.99.154.147.sslip.io";

@Inject
private Config config;

@BeforeEach
void setUp() {
config = mock(Config.class);
try (MockedStatic<ConfigProvider> mockConfigProvider = Mockito.mockStatic(ConfigProvider.class)) {
mockConfigProvider.when(ConfigProvider::getConfig).thenReturn(config);
when(config.getValue(eq(KNATIVE + ":" + getNamespace() + "/" + getKnativeServiceName()), eq(String.class)))
.thenReturn(KNATIVE_URI);
when(config.getValue(eq(KUBERNETES + ":" + getNamespace() + "/" + getKubernetesServiceName()), eq(String.class)))
.thenReturn(KUBERNETES_URI);
when(config.getValue(eq(OPENSHIFT + ":" + getNamespace() + "/" + getKubernetesServiceName()), eq(String.class)))
.thenReturn(OPENSHIFT_URI);

}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to mock anything. Quarkus will inject the config from application.properties.


@Inject
MicroProfileConfigServiceCatalogTest(MicroProfileConfigServiceCatalog configServiceCatalog) {
super(configServiceCatalog);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright 2023 Red Hat, Inc. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

knative:test/serverless-workflow-greeting-quarkus = http://serverless-workflow-greeting-quarkus.test.10.99.154.147.sslip.io
kubernetes:test/serverless-workflow-greeting-quarkus-kubernetes = http://serverless-workflow-greeting-quarkus-kubernetes.test.10.99.154.147.sslip.io
openshift:test/serverless-workflow-greeting-quarkus-openshift = http://serverless-workflow-greeting-quarkus-openshift.test.10.99.154.147.sslip.io
Copy link
Contributor

Choose a reason for hiding this comment

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

Two points to consider:

  1. We need to escape the "/" and ":" characters. So, knative:test/serverless-workflow-greeting-quarkus would become knative\:test\/serverless-workflow-greeting-quarkus
  2. I think we need a prefix for these properties, like:
org.kie.kogito.addons.microprofile-config-service-catalog.catalog.knative\:test\/serverless-workflow-greeting-quarkus = http://serverless-workflow-greeting-quarkus.test.10.99.154.147.sslip.io

But they might be too long for env. @ricardozanini WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

It's too long, but we won't use envs, but config props. I recommend:

org.kie.kogito.addons.discovery.knative\:test\/serverless-workflow-greeting-quarkus = http://serverless-workflow-greeting-quarkus.test.10.99.154.147.sslip.io

@hbelmiro @VaniHaripriya ^

Loading
Loading