Skip to content

Commit

Permalink
[Integration] Expose configured port in CQ and CK
Browse files Browse the repository at this point in the history
  • Loading branch information
avano authored and mmuzikar committed Jul 19, 2023
1 parent 5a1e61b commit 06058e1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Pod> podSelector = p -> p.getMetadata().getLabels().containsKey("camel.apache.org/integration")
&& name.equals(p.getMetadata().getLabels().get("camel.apache.org/integration"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Pod> podSelector = p -> p.getMetadata().getLabels().containsKey("app.kubernetes.io/name")
&& name.equals(p.getMetadata().getLabels().get("app.kubernetes.io/name"));
Expand Down Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 06058e1

Please sign in to comment.