Search in sources :

Example 1 with Endpoint

use of org.kie.kogito.addons.k8s.Endpoint in project kogito-runtimes by kiegroup.

the class EndpointDiscoveryTest method verifyKubernetesIntegration.

@Test
void verifyKubernetesIntegration() {
    final EndpointDiscovery mockedEndpointDiscovery = new EndpointDiscoveryConfig().endpointDiscovery(kubernetesClient);
    final Optional<Endpoint> endpoint = mockedEndpointDiscovery.findEndpoint("test", "test");
    // we haven't created anything
    assertTrue(endpoint.isEmpty());
}
Also used : Endpoint(org.kie.kogito.addons.k8s.Endpoint) EndpointDiscovery(org.kie.kogito.addons.k8s.EndpointDiscovery) Test(org.junit.jupiter.api.Test)

Example 2 with Endpoint

use of org.kie.kogito.addons.k8s.Endpoint in project kogito-runtimes by kiegroup.

the class KnativeRouteEndpointDiscoveryTest method testQueryByLabels.

@Test
public void testQueryByLabels() {
    final KnativeRouteEndpointDiscovery endpointDiscovery = new KnativeRouteEndpointDiscovery(null);
    endpointDiscovery.setKnativeClient(knativeClient);
    final Map<String, String> labels = Collections.singletonMap("app", "serverlessapp");
    // configure mock
    final RouteStatus status = new RouteStatus();
    status.setUrl("http://192.168.2.32");
    final Route route = new RouteBuilder().withNewMetadata().withLabels(labels).withName("ksvc2").withNamespace("test").and().withStatus(status).build();
    knativeClient.routes().create(route);
    final List<Endpoint> endpoint = endpointDiscovery.findEndpoint("test", labels);
    assertFalse(endpoint.isEmpty());
    try {
        new URL(endpoint.get(0).getUrl());
    } catch (MalformedURLException e) {
        // verbose
        fail("The generated URL " + endpoint.get(0).getUrl() + " is invalid");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RouteBuilder(io.fabric8.knative.serving.v1.RouteBuilder) Endpoint(org.kie.kogito.addons.k8s.Endpoint) RouteStatus(io.fabric8.knative.serving.v1.RouteStatus) KnativeRouteEndpointDiscovery(org.kie.kogito.addons.k8s.KnativeRouteEndpointDiscovery) Route(io.fabric8.knative.serving.v1.Route) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 3 with Endpoint

use of org.kie.kogito.addons.k8s.Endpoint in project kogito-runtimes by kiegroup.

the class KnativeRouteEndpointDiscoveryTest method testRouteWithoutStatus.

@Test
public void testRouteWithoutStatus() {
    final KnativeRouteEndpointDiscovery endpointDiscovery = new KnativeRouteEndpointDiscovery(null);
    endpointDiscovery.setKnativeClient(knativeClient);
    // configure mock
    final Route route = new RouteBuilder().withNewMetadata().withName("ksvc3").withNamespace("test").and().build();
    knativeClient.routes().create(route);
    final Optional<Endpoint> endpoint = endpointDiscovery.findEndpoint("test", "ksvc3");
    assertTrue(endpoint.isEmpty());
}
Also used : RouteBuilder(io.fabric8.knative.serving.v1.RouteBuilder) Endpoint(org.kie.kogito.addons.k8s.Endpoint) KnativeRouteEndpointDiscovery(org.kie.kogito.addons.k8s.KnativeRouteEndpointDiscovery) Route(io.fabric8.knative.serving.v1.Route) Test(org.junit.jupiter.api.Test)

Example 4 with Endpoint

use of org.kie.kogito.addons.k8s.Endpoint in project kogito-runtimes by kiegroup.

the class KnativeRouteEndpointDiscoveryTest method testBaseCase.

@Test
public void testBaseCase() {
    final KnativeRouteEndpointDiscovery endpointDiscovery = new KnativeRouteEndpointDiscovery(null);
    endpointDiscovery.setKnativeClient(knativeClient);
    // configure mock
    final RouteStatus status = new RouteStatus();
    status.setUrl("http://192.168.2.32");
    final Route route = new RouteBuilder().withNewMetadata().withName("ksvc1").withNamespace("test").and().withStatus(status).build();
    knativeClient.routes().create(route);
    final Optional<Endpoint> endpoint = endpointDiscovery.findEndpoint("test", "ksvc1");
    assertTrue(endpoint.isPresent());
    try {
        new URL(endpoint.get().getUrl());
    } catch (MalformedURLException e) {
        // verbose
        fail("The generated URL " + endpoint.get().getUrl() + " is invalid");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RouteBuilder(io.fabric8.knative.serving.v1.RouteBuilder) Endpoint(org.kie.kogito.addons.k8s.Endpoint) RouteStatus(io.fabric8.knative.serving.v1.RouteStatus) KnativeRouteEndpointDiscovery(org.kie.kogito.addons.k8s.KnativeRouteEndpointDiscovery) Route(io.fabric8.knative.serving.v1.Route) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 5 with Endpoint

use of org.kie.kogito.addons.k8s.Endpoint in project kogito-runtimes by kiegroup.

the class AbstractDiscoveredEndpointCaller method discoverAndCall.

/**
 * Central entry point for a WorkItemHandler to discover an endpoint based on its labels and call a Kogito REST service.
 *
 * @param workItem The given workitem for the current process
 * @param namespace Where the service is located
 * @param workItemServiceKey the key from the workitem parameter that holds the service label to search for. It will be used to build the final URL.
 *        For example: `my-service` can be the label key of the Service and also the path of the target endpoint
 * @param httpMethod the HTTP method to make the request
 * @return the result of the REST call
 */
public Map<String, Object> discoverAndCall(WorkItem workItem, String namespace, String workItemServiceKey, String httpMethod) {
    final Map<String, Object> data = new HashMap<>(workItem.getParameters());
    final String service = (String) data.remove(workItemServiceKey);
    final List<Endpoint> endpoint = this.getEndpointDiscovery().findEndpoint(namespace, Collections.singletonMap(service, null));
    if (endpoint.isEmpty()) {
        throw new IllegalArgumentException("Kubernetes service with label " + service + " not found in the namespace " + namespace);
    }
    if (endpoint.size() > 1) {
        LOGGER.warn("Found more than one endpoint using labels {}:null. Returning the first one in the list. Try to be more specific in the query search.", service);
    }
    LOGGER.debug("Found endpoint for service {} in namespace {} with URL {}", service, namespace, endpoint.get(0).getUrl());
    INTERNAL_FIELDS.forEach(data::remove);
    final Request request = createRequest(String.format("%s/%s", endpoint.get(0).getUrl(), service), createRequestPayload(data), httpMethod);
    try (Response response = this.httpClient.newCall(request).execute()) {
        return createResultsFromResponse(response, request.url().toString());
    } catch (IOException e) {
        throw new EndpointCallerException(e);
    }
}
Also used : Response(okhttp3.Response) Endpoint(org.kie.kogito.addons.k8s.Endpoint) HashMap(java.util.HashMap) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

Endpoint (org.kie.kogito.addons.k8s.Endpoint)5 Test (org.junit.jupiter.api.Test)4 Route (io.fabric8.knative.serving.v1.Route)3 RouteBuilder (io.fabric8.knative.serving.v1.RouteBuilder)3 KnativeRouteEndpointDiscovery (org.kie.kogito.addons.k8s.KnativeRouteEndpointDiscovery)3 RouteStatus (io.fabric8.knative.serving.v1.RouteStatus)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 EndpointDiscovery (org.kie.kogito.addons.k8s.EndpointDiscovery)1