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());
}
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");
}
}
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());
}
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");
}
}
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);
}
}
Aggregations