This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.8.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.8.x by this push: new 17288d73634 CAMEL-21369: camel-jbang kubernetes - add quarkus extensions configuration for Route trait (#16140) 17288d73634 is described below commit 17288d73634c7174562a9557c5ff404d0b75fa03 Author: Gaƫlle Fournier <gaelle.fournier.w...@gmail.com> AuthorDate: Sat Nov 2 08:11:58 2024 +0100 CAMEL-21369: camel-jbang kubernetes - add quarkus extensions configuration for Route trait (#16140) --- .../dsl/jbang/core/commands/k/IntegrationRun.java | 3 +- .../core/commands/kubernetes/KubernetesExport.java | 2 +- .../core/commands/kubernetes/traits/BaseTrait.java | 7 +++ .../commands/kubernetes/traits/RouteTrait.java | 54 ++++++++++++++++++++++ .../core/commands/kubernetes/traits/Trait.java | 34 ++++++++++++++ .../commands/kubernetes/traits/TraitCatalog.java | 6 ++- .../commands/kubernetes/traits/TraitContext.java | 4 ++ .../commands/kubernetes/KubernetesExportTest.java | 14 ++++++ 8 files changed, 121 insertions(+), 3 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationRun.java b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationRun.java index 1bae50309a7..64979ee556e 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationRun.java +++ b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationRun.java @@ -39,6 +39,7 @@ import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.TraitHelper; import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.TraitProfile; import org.apache.camel.dsl.jbang.core.common.JSonHelper; import org.apache.camel.dsl.jbang.core.common.Printer; +import org.apache.camel.dsl.jbang.core.common.RuntimeType; import org.apache.camel.dsl.jbang.core.common.Source; import org.apache.camel.dsl.jbang.core.common.SourceHelper; import org.apache.camel.dsl.jbang.core.common.SourceScheme; @@ -317,7 +318,7 @@ public class IntegrationRun extends KubernetesBaseCommand { TraitHelper.configureContainerImage(traitsSpec, image, "quay.io", null, integration.getMetadata().getName(), "1.0-SNAPSHOT"); - new TraitCatalog().apply(traitsSpec, context, traitProfile); + new TraitCatalog().apply(traitsSpec, context, traitProfile, RuntimeType.quarkus); printer().println( context.buildItems().stream().map(KubernetesHelper::dumpYaml).collect(Collectors.joining("---"))); diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java index ef7b0b91962..6f5829e924e 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java @@ -356,7 +356,7 @@ public class KubernetesExport extends Export { printer().println("Building Kubernetes manifest ..."); } - new TraitCatalog().apply(traitsSpec, context, traitProfile); + new TraitCatalog().apply(traitsSpec, context, traitProfile, runtime); var kubeFragments = context.buildItems().stream().map(KubernetesHelper::toJsonMap).toList(); diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/BaseTrait.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/BaseTrait.java index 3e1aadf09de..91d6e524ae3 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/BaseTrait.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/BaseTrait.java @@ -17,6 +17,9 @@ package org.apache.camel.dsl.jbang.core.commands.kubernetes.traits; +import org.apache.camel.dsl.jbang.core.common.RuntimeType; +import org.apache.camel.v1.integrationspec.Traits; + public abstract class BaseTrait implements Trait { public static final String KUBERNETES_NAME_LABEL = "app.kubernetes.io/name"; @@ -46,4 +49,8 @@ public abstract class BaseTrait implements Trait { public boolean accept(TraitProfile profile) { return true; } + + @Override + public void applyRuntimeSpecificProperties(Traits traitConfig, TraitContext context, RuntimeType runtimeType) { + } } diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/RouteTrait.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/RouteTrait.java index 1ceac8eb60e..6f0d171030d 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/RouteTrait.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/RouteTrait.java @@ -20,12 +20,16 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import io.fabric8.kubernetes.api.model.IntOrString; import io.fabric8.kubernetes.api.model.IntOrStringBuilder; import io.fabric8.openshift.api.model.RouteBuilder; import io.fabric8.openshift.api.model.TLSConfigBuilder; +import org.apache.camel.dsl.jbang.core.common.RuntimeType; import org.apache.camel.util.IOHelper; import org.apache.camel.util.StringHelper; import org.apache.camel.v1.integrationspec.Traits; @@ -114,6 +118,56 @@ public class RouteTrait extends BaseTrait { } + @Override + public void applyRuntimeSpecificProperties(Traits traitConfig, TraitContext context, RuntimeType runtimeType) { + List<String> routeProperties = new ArrayList<>(); + if (runtimeType == RuntimeType.quarkus) { + Route routeTrait = Optional.ofNullable(traitConfig.getRoute()).orElseGet(Route::new); + Container containerTrait = Optional.ofNullable(traitConfig.getContainer()).orElseGet(Container::new); + + routeProperties.add("quarkus.openshift.route.expose=true"); + if (routeTrait.getAnnotations() != null) { + routeTrait.getAnnotations().forEach((name, value) -> routeProperties + .add("quarkus.openshift.route.annotations.\"%s\"=%s".formatted(name, value))); + } + + if (routeTrait.getHost() != null) { + routeProperties.add("quarkus.openshift.route.host=%s".formatted(routeTrait.getHost())); + } + routeProperties.add("quarkus.openshift.route.target-port=%s" + .formatted(Optional.ofNullable(containerTrait.getServicePortName()).orElse(DEFAULT_CONTAINER_PORT_NAME))); + + if (routeTrait.getTlsTermination() != null) { + routeProperties + .add("quarkus.openshift.route.tls.termination=%s".formatted(routeTrait.getTlsTermination().getValue())); + } + if (routeTrait.getTlsCertificate() != null) { + routeProperties.add( + "quarkus.openshift.route.tls.certificate=%s" + .formatted(getContent(routeTrait.getTlsCertificate()).replaceAll("\n", "\\\\n"))); + } + if (routeTrait.getTlsKey() != null) { + routeProperties.add("quarkus.openshift.route.tls.key=%s" + .formatted(getContent(routeTrait.getTlsKey()).replaceAll("\n", "\\\\n"))); + } + if (routeTrait.getTlsCACertificate() != null) { + routeProperties + .add("quarkus.openshift.route.tls.ca-certificate=%s" + .formatted(getContent(routeTrait.getTlsCACertificate()).replaceAll("\n", "\\\\n"))); + } + if (routeTrait.getTlsDestinationCACertificate() != null) { + routeProperties.add("quarkus.openshift.route.tls.destination-ca-certificate=%s" + .formatted(getContent(routeTrait.getTlsDestinationCACertificate()).replaceAll("\n", "\\\\n"))); + } + if (routeTrait.getTlsInsecureEdgeTerminationPolicy() != null) { + routeProperties.add("quarkus.openshift.route.tls.insecure-edge-termination-policy=%s" + .formatted(routeTrait.getTlsInsecureEdgeTerminationPolicy())); + } + } + context.addOrAppendConfigurationResource("application.properties", + routeProperties.stream().collect(Collectors.joining(System.lineSeparator()))); + } + @Override public boolean accept(TraitProfile profile) { return TraitProfile.OPENSHIFT == profile; diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/Trait.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/Trait.java index 25f86a14262..d99ce74ed31 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/Trait.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/Trait.java @@ -17,18 +17,52 @@ package org.apache.camel.dsl.jbang.core.commands.kubernetes.traits; +import org.apache.camel.dsl.jbang.core.common.RuntimeType; import org.apache.camel.v1.integrationspec.Traits; public interface Trait extends Comparable<Trait> { + /** + * Evaluate if Trait configuration is ready to be applyed + * + * @param traitConfig trait configuration + * @param context command traits context + * @return true if the trait configuration can be applied to context + */ boolean configure(Traits traitConfig, TraitContext context); + /** + * Apply trait configuration to context + * + * @param traitConfig trait configuration + * @param context command traits context + */ void apply(Traits traitConfig, TraitContext context); + /** + * Priority order for trait application. + * + * @return order + */ int order(); + /** + * Evaluate if trait can be applied to trait profile + * + * @param profile trait profile + * @return true if applicable + */ boolean accept(TraitProfile profile); + /** + * Add runtime properties to command trait context to be added to generated project properties + * + * @param traitConfig trait configuration + * @param context command traits context + * @param runtimeType + */ + void applyRuntimeSpecificProperties(Traits traitConfig, TraitContext context, RuntimeType runtimeType); + @Override default int compareTo(Trait o) { return Integer.compare(order(), o.order()); diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitCatalog.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitCatalog.java index 97037f066a3..c882f9a0a8b 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitCatalog.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitCatalog.java @@ -24,6 +24,7 @@ import java.util.stream.Collectors; import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.knative.KnativeServiceTrait; import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.knative.KnativeTrait; +import org.apache.camel.dsl.jbang.core.common.RuntimeType; import org.apache.camel.v1.integrationspec.Traits; /** @@ -69,18 +70,21 @@ public class TraitCatalog { * @param traitsSpec the trait configuration spec. * @param context the trait context. * @param traitProfile the optional trait profile to select traits. + * @param runtimeType the runtime. */ - public void apply(Traits traitsSpec, TraitContext context, String traitProfile) { + public void apply(Traits traitsSpec, TraitContext context, String traitProfile, RuntimeType runtimeType) { if (traitProfile != null) { new TraitCatalog().traitsForProfile(TraitProfile.valueOf(traitProfile.toUpperCase(Locale.US))).forEach(t -> { if (t.configure(traitsSpec, context)) { t.apply(traitsSpec, context); + t.applyRuntimeSpecificProperties(traitsSpec, context, runtimeType); } }); } else { new TraitCatalog().allTraits().forEach(t -> { if (t.configure(traitsSpec, context)) { t.apply(traitsSpec, context); + t.applyRuntimeSpecificProperties(traitsSpec, context, runtimeType); } }); } diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitContext.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitContext.java index e77ceaf784d..dbb5c50a1fb 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitContext.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/TraitContext.java @@ -275,6 +275,10 @@ public class TraitContext { this.configurationResources.put(name, content); } + public void addOrAppendConfigurationResource(String name, String content) { + this.configurationResources.merge(name, content, (content1, content2) -> content1 + System.lineSeparator() + content2); + } + public void doWithConfigurationResources(BiConsumer<String, String> consumer) { configurationResources.forEach(consumer); } diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExportTest.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExportTest.java index 41465a27e01..43fcf690144 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExportTest.java +++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExportTest.java @@ -290,6 +290,20 @@ class KubernetesExportTest extends KubernetesBaseTest { Assertions.assertEquals("route-service", route.getSpec().getTo().getName()); Assertions.assertTrue(certificate.startsWith(route.getSpec().getTls().getCertificate())); Assertions.assertTrue(key.startsWith(route.getSpec().getTls().getKey())); + + if (RuntimeType.quarkus.equals(rt)) { + Properties applicationProperties = getApplicationProperties(workingDir); + Assertions.assertEquals("true", applicationProperties.get("quarkus.openshift.route.expose")); + Assertions.assertEquals("example.com", + applicationProperties.get("quarkus.openshift.route.host")); + Assertions.assertEquals("http", + applicationProperties.get("quarkus.openshift.route.target-port")); + Assertions.assertEquals("edge", + applicationProperties.get("quarkus.openshift.route.tls.termination")); + Assertions.assertTrue(certificate.startsWith( + applicationProperties.get("quarkus.openshift.route.tls.certificate").toString())); + Assertions.assertTrue(key.startsWith(applicationProperties.get("quarkus.openshift.route.tls.key").toString())); + } } @ParameterizedTest