Skip to content

Commit

Permalink
CAMEL-21625: camel-jbang - camel k8s run may not cleanup properly on …
Browse files Browse the repository at this point in the history
…openshift
  • Loading branch information
tdiesler committed Jan 16, 2025
1 parent bfb2fab commit 2e4d8b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.vertx.VertxHttpClientFactory;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystemOptions;
import org.apache.camel.dsl.jbang.core.commands.CommandHelper;
import org.apache.camel.dsl.jbang.core.common.YamlHelper;
import org.apache.camel.util.FileUtil;
Expand Down Expand Up @@ -154,21 +150,6 @@ public static void setKubernetesClient(KubernetesClient kubernetesClient) {
KubernetesHelper.kubernetesClient = kubernetesClient;
}

// KubernetesClientVertx can no longer be used in ShutdownHook
// https://issues.apache.org/jira/browse/CAMEL-21621
public static KubernetesClient createKubernetesClientForShutdownHook() {
System.setProperty("vertx.disableDnsResolver", "true");
var vertx = Vertx.vertx((new VertxOptions())
.setFileSystemOptions((new FileSystemOptions())
.setFileCachingEnabled(false)
.setClassPathResolvingEnabled(false))
.setUseDaemonThread(true));
var client = new KubernetesClientBuilder()
.withHttpClientFactory(new VertxHttpClientFactory(vertx))
.build();
return client;
}

/**
* Dump given domain model object as YAML. Uses Json conversion to generic map as intermediate step. This makes sure
* to properly write Json additional properties.
Expand Down Expand Up @@ -220,13 +201,12 @@ public static File getKubernetesManifest(String clusterType, File workingDir) {
}

public static File getKubernetesManifest(String clusterType, File workingDir, String extension) {
ClusterType cs = ClusterType
.valueOf(Optional.ofNullable(clusterType).map(String::toUpperCase).orElse(ClusterType.KUBERNETES.name()));
String manifestFile = switch (cs) {
case KIND, MINIKUBE -> "kubernetes";
case OPENSHIFT -> "_openshift";
default -> cs.name().toLowerCase();
};
String manifestFile;
if (ClusterType.KIND.isEqualTo(clusterType) || ClusterType.MINIKUBE.isEqualTo(clusterType)) {
manifestFile = "kubernetes";
} else {
manifestFile = Optional.ofNullable(clusterType).map(String::toLowerCase).orElse("kubernetes");
}
return new File(workingDir, "%s.%s".formatted(manifestFile, extension));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
import java.util.concurrent.TimeUnit;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.vertx.VertxHttpClientFactory;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystemOptions;
import org.apache.camel.CamelContext;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.CommandHelper;
Expand Down Expand Up @@ -506,7 +512,7 @@ private void installShutdownHook(String projectName, String workingDir) {
deleteCommand.clusterType = clusterType;
deleteCommand.workingDir = workingDir;
deleteCommand.name = projectName;
try (var client = KubernetesHelper.createKubernetesClientForShutdownHook()) {
try (var client = createKubernetesClientForShutdownHook()) {
KubernetesHelper.setKubernetesClient(client);
deleteCommand.doCall();
CommandHelper.cleanExportDir(deleteCommand.workingDir, false);
Expand All @@ -518,6 +524,21 @@ private void installShutdownHook(String projectName, String workingDir) {
Runtime.getRuntime().addShutdownHook(devModeShutdownTask);
}

// KubernetesClientVertx can no longer be used in ShutdownHook
// https://issues.apache.org/jira/browse/CAMEL-21621
private KubernetesClient createKubernetesClientForShutdownHook() {
System.setProperty("vertx.disableDnsResolver", "true");
var vertx = Vertx.vertx((new VertxOptions())
.setFileSystemOptions((new FileSystemOptions())
.setFileCachingEnabled(false)
.setClassPathResolvingEnabled(false))
.setUseDaemonThread(true));
var client = new KubernetesClientBuilder()
.withHttpClientFactory(new VertxHttpClientFactory(vertx))
.build();
return client;
}

private Integer buildProject(String workingDir) throws IOException, InterruptedException {
printer().println("Building Camel application ...");

Expand Down

0 comments on commit 2e4d8b6

Please # to comment.