Skip to content

Commit

Permalink
fix apache#2733 : Native support for kamelet.yaml discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
valdar committed Jun 15, 2021
1 parent 9251933 commit b71f831
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 141 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/contributor-guide/index.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[contributor-guide]]
ntributor-guide]]
= Contributor guide
:page-aliases: contributor-guide.adoc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,29 @@
*/
package org.apache.camel.quarkus.component.kamelet.deployment;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.quarkus.component.kamelet.KameletConfiguration;
import org.apache.camel.quarkus.component.kamelet.KameletRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.ResourceHelper;

class KameletProcessor {
private static final String FEATURE = "camel-kamelet";
Expand All @@ -58,10 +52,10 @@ FeatureBuildItem feature() {
KameletResolverBuildItem defaultResolver() {
return new KameletResolverBuildItem(new KameletResolver() {
@Override
public Optional<InputStream> resolve(String id) throws Exception {
public Optional<Resource> resolve(String id, CamelContext context) throws Exception {
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
return Optional.ofNullable(
Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/kamelets/" + id + ".kamelet.yaml"));
ecc.getResourceLoader().resolveResource("/kamelets/" + id + ".kamelet.yaml"));
}
});
}
Expand All @@ -73,65 +67,35 @@ CamelContextCustomizerBuildItem configureTemplates(
KameletConfiguration configuration,
KameletRecorder recorder) throws Exception {

ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

List<RouteTemplateDefinition> definitions = new ArrayList<>();
List<KameletResolver> kameletResolvers = resolvers.stream()
.map(KameletResolverBuildItem::getResolver)
.sorted(Comparator.comparingInt(Ordered::getOrder))
.collect(Collectors.toList());

try (YamlRoutesBuilderLoader ybl = new YamlRoutesBuilderLoader()) {
ybl.setCamelContext(new DefaultCamelContext());
ybl.start();
CamelContext context = new DefaultCamelContext();
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);

for (String name : configuration.names.orElse(Collections.emptyList())) {
for (String name : configuration.names.orElse(Collections.emptyList())) {
for (KameletResolver resolver : kameletResolvers) {
final Optional<Resource> resource = resolver.resolve(name, ecc);
if (!resource.isPresent()) {
continue;
}

for (KameletResolver resolver : kameletResolvers) {
final Optional<InputStream> is = resolver.resolve(name);
if (!is.isPresent()) {
continue;
Collection<RoutesBuilder> rbs = ecc.getRoutesLoader().findRoutesBuilders(resource.get());
for (RoutesBuilder rb : rbs) {
RouteBuilder routeBuilder = (RouteBuilder) rb;
routeBuilder.configure();
if (routeBuilder.getRouteTemplateCollection().getRouteTemplates().size() != 1) {
throw new IllegalStateException(
"A kamelet is not supposed to create more than one route ("
+ "kamelet:" + name + ","
+ "routes: " + routeBuilder.getRouteTemplateCollection().getRouteTemplates().size()
+ ")");
}

try {
final ObjectNode definition = (ObjectNode) mapper.readTree(is.get());
final JsonNode properties = definition.requiredAt("/spec/definition/properties");
final JsonNode flow = mapper.createArrayNode().add(definition.requiredAt("/spec/flow"));
final Resource res = ResourceHelper.fromBytes(name + ".yaml", mapper.writeValueAsBytes(flow));

RouteTemplateDefinition rt = new RouteTemplateDefinition();
rt.setId(name);

Iterator<Map.Entry<String, JsonNode>> it = properties.fields();
while (it.hasNext()) {
final Map.Entry<String, JsonNode> property = it.next();
final String key = property.getKey();
final JsonNode def = property.getValue().at("/default");

if (def.isMissingNode()) {
rt.templateParameter(key);
} else {
rt.templateParameter(key, def.asText());
}
}

RouteBuilder rb = (RouteBuilder) ybl.loadRoutesBuilder(res);
rb.configure();
if (rb.getRouteCollection().getRoutes().size() != 1) {
throw new IllegalStateException(
"A kamelet is not supposed to create more than one route ("
+ "kamelet:" + name + ","
+ "routes: " + rb.getRouteCollection().getRoutes().size()
+ ")");
}

rt.setRoute(rb.getRouteCollection().getRoutes().get(0));

definitions.add(rt);
} finally {
is.get().close();
}
definitions.add(routeBuilder.getRouteTemplateCollection().getRouteTemplates().get(0));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.apache.camel.quarkus.component.kamelet.deployment;

import java.io.InputStream;
import java.util.Optional;

import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.spi.Resource;

public interface KameletResolver extends Ordered {
Optional<InputStream> resolve(String id) throws Exception;
Optional<Resource> resolve(String id, CamelContext context) throws Exception;

@Override
default int getOrder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Build item used by kamelet providers to plug their own way of resolving kamelets giving a name. This could be
* leveraged by a future camel-quarkus-kamelet-catalog extension to resolve kamelets as they may have a different naming
* structure or location int the classpath.
* structure or location in the classpath.
*/
public final class KameletResolverBuildItem extends MultiBuildItem {
private final KameletResolver resolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.runtime.annotations.RelaxedValidation;
import org.apache.camel.CamelContext;
import org.apache.camel.model.Model;
import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.quarkus.core.CamelContextCustomizer;
import org.apache.camel.spi.CamelContextCustomizer;

@Recorder
public class KameletRecorder {
public RuntimeValue<CamelContextCustomizer> createTemplateLoaderCustomizer(List<RouteTemplateDefinition> definitions) {
public RuntimeValue<CamelContextCustomizer> createTemplateLoaderCustomizer(
@RelaxedValidation List<RouteTemplateDefinition> definitions) {

return new RuntimeValue<>(new CamelContextCustomizer() {
@Override
public void customize(CamelContext context) {
public void configure(CamelContext context) {
try {
context.getExtension(Model.class).addRouteTemplateDefinitions(definitions);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,9 @@ public String invoke(@PathParam("name") String name, String message) throws Exce
return fluentProducerTemplate.toF("kamelet:%s", name).withBody(message).request(String.class);
}

@Path("/auto-discovery")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String autoDiscovery(String message) {
return fluentProducerTemplate.toF("kamelet:auto-discovery?message=%s", message).request(String.class);
}

@Path("/list")
@GET
@Produces(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public JsonArray list() {
JsonArrayBuilder builder = Json.createArrayBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
package org.apache.camel.quarkus.component.kamelet.it;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.RoutesBuilderLoaderSupport;

@ApplicationScoped
public class KameletRoutes extends RouteBuilder {
Expand Down Expand Up @@ -75,25 +71,4 @@ public void process(Exchange exchange) {
exchange.getMessage().setBody(exchange.getMessage().getBody(String.class) + "-suffix");
}
}

@Named("routes-builder-loader-kamelet.yaml")
RoutesBuilderLoaderSupport routesBuilderLoaderKameletYaml() {
return new RoutesBuilderLoaderSupport() {
@Override
public String getSupportedExtension() {
return "kamelet.yaml";
}

@Override
public RoutesBuilder loadRoutesBuilder(Resource resource) {
return new RouteBuilder() {
@Override
public void configure() {
routeTemplate("auto-discovery").templateParameter("message").from("kamelet:source").setBody()
.constant("Auto-discovered {{message}}");
}
};
}
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
name: injector-source-v1alpha1
name: injector
labels:
camel.apache.org/kamelet.type: "source"
camel.apache.org/kamelet.name: "injector"
Expand All @@ -42,7 +42,7 @@ spec:
flow:
from:
uri: timer
properties:
parameters:
timerName: "{{routeId}}"
period: "{{delay}}"
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
name: logger
labels:
camel.apache.org/kamelet.type: "sink"
camel.apache.org/kamelet.name: "log"
Expand Down Expand Up @@ -50,7 +51,7 @@ spec:
steps:
- to:
uri: "log"
properties:
parameters:
loggerName: "{{loggerName}}"
showAll: "{{showAll}}"
multiline: "{{multiLine}}"
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package org.apache.camel.quarkus.component.kamelet.it;

import java.util.ArrayList;
import java.util.Map;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
class KameletTest {
Expand Down Expand Up @@ -86,24 +90,17 @@ public void testInvoke() {
.body(is("Kamelet2-suffix"));
}

@Test
public void testAutoDiscovery() {
RestAssured.given()
.contentType(ContentType.TEXT)
.body("Kamelet")
.post("/kamelet/auto-discovery")
.then()
.statusCode(200)
.body(is("Auto-discovered Kamelet"));
}

@Test
public void testDiscovered() {
RestAssured.given()
.contentType(ContentType.TEXT)
.get("/kamelet/list")
.then()
.statusCode(200)
.body(hasItem("injector"), hasItem("logger"));

Response resp = RestAssured.given()
.contentType(ContentType.JSON)
.when().get("/kamelet/list");
resp.then().statusCode(200);

ArrayList<Map<String, ?>> jsonAsArrayList = resp.body()
.jsonPath().get("");
assertTrue(jsonAsArrayList.contains("injector"));
assertTrue(jsonAsArrayList.contains("logger"));
}
}

0 comments on commit b71f831

Please sign in to comment.