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 16, 2021
1 parent e4d2f9d commit c5963b6
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 40 deletions.
41 changes: 41 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/kamelet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,44 @@ Or add the coordinates to your existing project:
----

Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.

== Usage

=== Pre-load Kamelets at build-time

This extension allows to pre-load a set of Kamelets at build time using the `quarkus.camel.kamelet.identifiers` property.

=== Using the Kamelet Catalog

A set of pre-made Kamelets can be found on the https://camel.apache.org/camel-kamelets/latest[Kamelet Catalog].
To use the Kamelet from the catalog you need to copy their yaml definition (that you can find https://github.com/apache/camel-kamelets/[in the camel-kamelet repo]) on your project in the classpath. Alternatively you can add the `camel-quarkus-kamelets-catalog` artifact to your `pom.xml`:

[source,xml]
----
<dependency>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets-catalog</artifactId>
</dependency>
----

This artifact add all the kamelets available in the catalog to your Camel Quarkus application for build time processing. If you include it with the scope `provided` the artifact should not be part of the runtime classpath, but at build time, all the kamelets listed via `quarkus.camel.kamelet.identifiers` property should be preloaded.


== Additional Camel Quarkus configuration

[width="100%",cols="80,5,15",options="header"]
|===
| Configuration property | Type | Default


|icon:lock[title=Fixed at build time] [[quarkus.camel.kamelet.identifiers]]`link:#quarkus.camel.kamelet.identifiers[quarkus.camel.kamelet.identifiers]`

List of kamelets identifiers to pre-load at build time.
Each individual identifier is used to set the related `org.apache.camel.model.RouteTemplateDefinition` id.
| `string`
|
|===

[.configuration-legend]
icon:lock[title=Fixed at build time] Configuration property fixed at build time. All other configuration properties are overridable at runtime.

4 changes: 4 additions & 0 deletions extensions/kamelet/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-kamelet</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-yaml-dsl</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,94 @@
*/
package org.apache.camel.quarkus.component.kamelet.deployment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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.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;

class KameletProcessor {

private static final String FEATURE = "camel-kamelet";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
KameletResolverBuildItem defaultResolver() {
return new KameletResolverBuildItem(new KameletResolver() {
@Override
public Optional<Resource> resolve(String id, CamelContext context) throws Exception {
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
return Optional.ofNullable(
ecc.getResourceLoader().resolveResource("/kamelets/" + id + ".kamelet.yaml"));
}
});
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelContextCustomizerBuildItem configureTemplates(
List<KameletResolverBuildItem> resolvers,
KameletConfiguration configuration,
KameletRecorder recorder) throws Exception {

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

CamelContext context = new DefaultCamelContext();
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);

for (String id : configuration.identifiers.orElse(Collections.emptyList())) {
for (KameletResolver resolver : kameletResolvers) {
final Optional<Resource> resource = resolver.resolve(id, ecc);
if (!resource.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() == 0) {
throw new IllegalStateException(
"No kamelet template was created for "
+ "kamelet:" + id + "might be the kamelet was malformed?");
} else if (routeBuilder.getRouteTemplateCollection().getRouteTemplates().size() > 1) {
throw new IllegalStateException(
"A kamelet is not supposed to create more than one route ("
+ "kamelet:" + id + ","
+ "routes: " + routeBuilder.getRouteTemplateCollection().getRouteTemplates().size()
+ ")");
}

definitions.add(routeBuilder.getRouteTemplateCollection().getRouteTemplates().get(0));
}
}
}

return new CamelContextCustomizerBuildItem(recorder.createTemplateLoaderCustomizer(definitions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.quarkus.component.kamelet.deployment;

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<Resource> resolve(String id, CamelContext context) throws Exception;

@Override
default int getOrder() {
return Ordered.LOWEST;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.quarkus.component.kamelet.deployment;

import io.quarkus.builder.item.MultiBuildItem;

/**
* 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 in the classpath.
*/
public final class KameletResolverBuildItem extends MultiBuildItem {
private final KameletResolver resolver;

public KameletResolverBuildItem(KameletResolver resolver) {
this.resolver = resolver;
}

public KameletResolver getResolver() {
return this.resolver;
}
}
18 changes: 18 additions & 0 deletions extensions/kamelet/runtime/src/main/doc/usage.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== Pre-load Kamelets at build-time

This extension allows to pre-load a set of Kamelets at build time using the `quarkus.camel.kamelet.identifiers` property.

=== Using the Kamelet Catalog

A set of pre-made Kamelets can be found on the https://camel.apache.org/camel-kamelets/latest[Kamelet Catalog].
To use the Kamelet from the catalog you need to copy their yaml definition (that you can find https://github.com/apache/camel-kamelets/[in the camel-kamelet repo]) on your project in the classpath. Alternatively you can add the `camel-quarkus-kamelets-catalog` artifact to your `pom.xml`:

[source,xml]
----
<dependency>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets-catalog</artifactId>
</dependency>
----

This artifact add all the kamelets available in the catalog to your Camel Quarkus application for build time processing. If you include it with the scope `provided` the artifact should not be part of the runtime classpath, but at build time, all the kamelets listed via `quarkus.camel.kamelet.identifiers` property should be preloaded.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.quarkus.component.kamelet;

import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.kamelet", phase = ConfigPhase.BUILD_TIME)
public class KameletConfiguration {
/**
* List of kamelets identifiers to pre-load at build time.
* <p>
* Each individual identifier is used to set the related {@link org.apache.camel.model.RouteTemplateDefinition} id.
*/
@ConfigItem
public Optional<List<String>> identifiers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.quarkus.component.kamelet;

import java.util.List;

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.spi.CamelContextCustomizer;

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

return new RuntimeValue<>(new CamelContextCustomizer() {
@Override
public void configure(CamelContext context) {
try {
context.getExtension(Model.class).addRouteTemplateDefinitions(definitions);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}
17 changes: 17 additions & 0 deletions integration-tests/kamelet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand All @@ -77,6 +81,19 @@
</dependency>

<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-bean-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct-deployment</artifactId>
Expand Down
Loading

0 comments on commit c5963b6

Please sign in to comment.