From 06058e1c88f0fce037800afc5ddee16d6e7e1b42 Mon Sep 17 00:00:00 2001 From: Andrej Vano Date: Tue, 18 Jul 2023 16:08:25 +0200 Subject: [PATCH] [Integration] Expose configured port in CQ and CK --- .../tnb/product/ck/application/CamelKApp.java | 11 ++++- .../cq/application/OpenshiftQuarkusApp.java | 47 ++++++++++++++++++- .../generator/IntegrationGenerator.java | 4 +- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/fuse-products/src/main/java/software/tnb/product/ck/application/CamelKApp.java b/fuse-products/src/main/java/software/tnb/product/ck/application/CamelKApp.java index 3d0078fb9..d7de0eaa7 100644 --- a/fuse-products/src/main/java/software/tnb/product/ck/application/CamelKApp.java +++ b/fuse-products/src/main/java/software/tnb/product/ck/application/CamelKApp.java @@ -60,6 +60,7 @@ import io.fabric8.camelk.v1alpha1.KameletBindingList; import io.fabric8.knative.client.KnativeClient; import io.fabric8.knative.eventing.v1.Trigger; +import io.fabric8.knative.serving.v1.Route; import io.fabric8.knative.serving.v1.Service; import io.fabric8.kubernetes.api.model.Condition; import io.fabric8.kubernetes.api.model.ConfigMap; @@ -117,8 +118,14 @@ public void start() { createIntegrationResources((AbstractIntegrationBuilder) integrationSource); } - endpoint = new Endpoint(() -> OpenshiftClient.get().adapt(KnativeClient.class).routes() - .withName(name).get().getStatus().getUrl()); + endpoint = new Endpoint(() -> { + Route knativeRoute = OpenshiftClient.get().adapt(KnativeClient.class).routes().withName(name).get(); + if (knativeRoute != null) { + return knativeRoute.getStatus().getUrl(); + } else { + return "http://" + OpenshiftClient.get().routes().withName(name).get().getSpec().getHost(); + } + }); Predicate podSelector = p -> p.getMetadata().getLabels().containsKey("camel.apache.org/integration") && name.equals(p.getMetadata().getLabels().get("camel.apache.org/integration")); diff --git a/fuse-products/src/main/java/software/tnb/product/cq/application/OpenshiftQuarkusApp.java b/fuse-products/src/main/java/software/tnb/product/cq/application/OpenshiftQuarkusApp.java index 0ca490fe3..53b8faffb 100644 --- a/fuse-products/src/main/java/software/tnb/product/cq/application/OpenshiftQuarkusApp.java +++ b/fuse-products/src/main/java/software/tnb/product/cq/application/OpenshiftQuarkusApp.java @@ -31,7 +31,11 @@ import cz.xtf.core.openshift.helpers.ResourceFunctions; import io.fabric8.kubernetes.api.model.DeletionPropagation; +import io.fabric8.kubernetes.api.model.IntOrString; import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.api.model.ServiceBuilder; +import io.fabric8.openshift.api.model.RouteBuilder; +import io.fabric8.openshift.api.model.RoutePort; public class OpenshiftQuarkusApp extends QuarkusApp { private static final Logger LOG = LoggerFactory.getLogger(OpenshiftCamelQuarkus.class); @@ -56,8 +60,45 @@ public void start() { } Maven.invoke(builder.build()); - endpoint = new Endpoint(() -> OpenshiftClient.get().routes().withName(name).get() != null ? "http://" + OpenshiftClient.get().routes() - .withName(name).get().getSpec().getHost() : null); + // @formatter:off + if (OpenshiftClient.get().services().withName(name).get() == null) { + // create the service and route manually if it is not created by quarkus + OpenshiftClient.get().services().createOrReplace(new ServiceBuilder() + .withNewMetadata() + .withName(name) + .addToLabels("app.kubernetes.io/name", name) + .endMetadata() + .withNewSpec() + .addToSelector("app.kubernetes.io/name", name) + .addNewPort() + .withName("port") + .withPort(integrationBuilder.getPort()) + .withProtocol("TCP") + .endPort() + .endSpec() + .build() + ); + } + if (OpenshiftClient.get().routes().withName(name).get() == null) { + OpenshiftClient.get().routes().createOrReplace(new RouteBuilder() + .withNewMetadata() + .withName(name) + .addToLabels("app.kubernetes.io/name", name) + .endMetadata() + .withNewSpec() + .withPort(new RoutePort(new IntOrString(integrationBuilder.getPort()))) + .withNewTo() + .withKind("Service") + .withName(name) + .withWeight(100) + .endTo() + .endSpec() + .build() + ); + } + // @formatter:on + + endpoint = new Endpoint(() -> "http://" + OpenshiftClient.get().routes().withName(name).get().getSpec().getHost()); Predicate podSelector = p -> p.getMetadata().getLabels().containsKey("app.kubernetes.io/name") && name.equals(p.getMetadata().getLabels().get("app.kubernetes.io/name")); @@ -104,6 +145,8 @@ public void stop() { ((OpenshiftLog) getLog()).save(started); } LOG.info("Undeploying integration resources"); + OpenshiftClient.get().routes().withName(name).delete(); + OpenshiftClient.get().services().withName(name).delete(); Path openshiftResources = TestConfiguration.appLocation().resolve(name).resolve("target").resolve("kubernetes/openshift.yml"); try (InputStream is = IOUtils.toInputStream(Files.readString(openshiftResources), "UTF-8")) { diff --git a/fuse-products/src/main/java/software/tnb/product/integration/generator/IntegrationGenerator.java b/fuse-products/src/main/java/software/tnb/product/integration/generator/IntegrationGenerator.java index a8ed437fe..2b7aa3919 100644 --- a/fuse-products/src/main/java/software/tnb/product/integration/generator/IntegrationGenerator.java +++ b/fuse-products/src/main/java/software/tnb/product/integration/generator/IntegrationGenerator.java @@ -172,7 +172,9 @@ private static void processCustomizers(AbstractIntegrationBuilder integration new DependenciesToModelineCustomizer(), new InlineCustomizer(), new ComponentCustomizer(), - new CamelMainCustomizer() + new CamelMainCustomizer(), + // for camel-k expose the configured port using trait, so that app.getEndpoint() can be used + new TraitCustomizer("container", Map.of("port", integrationBuilder.getPort(), "servicePort", integrationBuilder.getPort())) ); if (TestConfiguration.streamLogs()) {