This is an automated email from the ASF dual-hosted git repository.
cdeppisch pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 7fddc5562fd Improve JBang Kubernetes plugin logs command parameter
7fddc5562fd is described below
commit 7fddc5562fd998683ec36fe87720cfdb72ee95c5
Author: Christoph Deppisch <[email protected]>
AuthorDate: Fri Aug 9 09:58:31 2024 +0200
Improve JBang Kubernetes plugin logs command parameter
- Support file path as a command parameter and automatically derive the
integration name from the file name
- Support --name and --label arguments in Camel K plugin logs command
---
.../camel/dsl/jbang/core/commands/k/Bind.java | 2 +-
.../dsl/jbang/core/commands/k/IntegrationLogs.java | 36 +++++++++++++++-------
.../dsl/jbang/core/commands/k/IntegrationRun.java | 2 +-
.../jbang/core/commands/k/IntegrationLogsTest.java | 4 +--
.../core/commands/kubernetes/KubernetesDelete.java | 10 +++---
.../jbang/core/commands/kubernetes/PodLogs.java | 19 ++++++++++--
6 files changed, 50 insertions(+), 23 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/Bind.java
b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/Bind.java
index 59e3a18ebc2..e7405bf04dc 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/Bind.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/Bind.java
@@ -207,7 +207,7 @@ public class Bind extends KubernetesBaseCommand {
if (logs) {
IntegrationLogs logsCommand = new IntegrationLogs(getMain());
logsCommand.withClient(client());
- logsCommand.name = pipeResource.getMetadata().getName();
+ logsCommand.withName(pipeResource.getMetadata().getName());
logsCommand.doCall();
}
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogs.java
b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogs.java
index ed750868baa..fe8a1f60fde 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogs.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-k/src/main/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogs.java
@@ -19,33 +19,47 @@ package org.apache.camel.dsl.jbang.core.commands.k;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.kubernetes.KubernetesHelper;
import org.apache.camel.dsl.jbang.core.commands.kubernetes.PodLogs;
+import org.apache.camel.dsl.jbang.core.common.SourceScheme;
+import org.apache.camel.util.FileUtil;
import org.apache.camel.v1.Integration;
-import picocli.CommandLine;
import picocli.CommandLine.Command;
@Command(name = "logs", description = "Print the logs of an integration",
sortOptions = false)
public class IntegrationLogs extends PodLogs {
- @CommandLine.Parameters(description = "Integration name to grab logs
from.",
- paramLabel = "<name>")
- String name;
-
public IntegrationLogs(CamelJBangMain main) {
super(main);
}
public Integer doCall() throws Exception {
- String integrationName = KubernetesHelper.sanitize(name);
- Integration integration =
client(Integration.class).withName(integrationName).get();
-
- if (integration == null) {
- printer().printf("Integration %s not found%n", integrationName);
+ if (name == null && label == null && filePath == null) {
+ printer().println("Name or label selector must be set");
return 1;
}
- label = "%s=%s".formatted(CamelKCommand.INTEGRATION_LABEL,
integrationName);
+ if (label == null) {
+ String integrationName;
+ if (name != null) {
+ integrationName = KubernetesHelper.sanitize(name);
+ } else {
+ integrationName =
KubernetesHelper.sanitize(FileUtil.onlyName(SourceScheme.onlyName(filePath)));
+ }
+
+ Integration integration =
client(Integration.class).withName(integrationName).get();
+ if (integration == null) {
+ printer().printf("Integration %s not found%n",
integrationName);
+ return 1;
+ }
+
+ label = "%s=%s".formatted(CamelKCommand.INTEGRATION_LABEL,
integrationName);
+ }
+
container = CamelKCommand.INTEGRATION_CONTAINER_NAME;
return super.doCall();
}
+
+ public void withName(String name) {
+ this.name = name;
+ }
}
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 a15ed6c4034..1bae50309a7 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
@@ -354,7 +354,7 @@ public class IntegrationRun extends KubernetesBaseCommand {
if (logs) {
IntegrationLogs logsCommand = new IntegrationLogs(getMain());
logsCommand.withClient(client());
- logsCommand.name = integration.getMetadata().getName();
+ logsCommand.withName(integration.getMetadata().getName());
logsCommand.doCall();
}
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-k/src/test/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogsTest.java
b/dsl/camel-jbang/camel-jbang-plugin-k/src/test/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogsTest.java
index 45ad315e6c3..1a7ad4ae04c 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-k/src/test/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogsTest.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-k/src/test/java/org/apache/camel/dsl/jbang/core/commands/k/IntegrationLogsTest.java
@@ -31,7 +31,7 @@ class IntegrationLogsTest extends CamelKBaseTest {
@Test
public void shouldHandleIntegrationsNotFound() throws Exception {
IntegrationLogs command = createCommand();
- command.name = "mickey-mouse";
+ command.withName("mickey-mouse");
command.doCall();
Assertions.assertEquals("Integration mickey-mouse not found",
printer.getOutput());
@@ -56,7 +56,7 @@ class IntegrationLogsTest extends CamelKBaseTest {
IntegrationLogs command = createCommand();
- command.name = "routes";
+ command.withName("routes");
int exit = command.doCall();
Assertions.assertEquals(0, exit);
}
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesDelete.java
b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesDelete.java
index 22209d55d90..7832970685f 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesDelete.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesDelete.java
@@ -31,9 +31,9 @@ import picocli.CommandLine;
@CommandLine.Command(name = "delete", description = "Delete Camel application
from Kubernetes", sortOptions = false)
public class KubernetesDelete extends KubernetesBaseCommand {
- @CommandLine.Parameters(description = "The Camel file(s) to delete.",
- arity = "0..9", paramLabel = "<files>")
- String[] filePaths;
+ @CommandLine.Parameters(description = "The Camel file to delete.
Integration name is derived from the file name.",
+ arity = "0..1", paramLabel = "<file>")
+ String filePath;
@CommandLine.Option(names = { "--name" },
description = "The integration name. Use this when the
name should not get derived from the source file name.")
@@ -55,8 +55,8 @@ public class KubernetesDelete extends KubernetesBaseCommand {
String projectName;
if (name != null) {
projectName = KubernetesHelper.sanitize(name);
- } else if (filePaths != null && filePaths.length > 0) {
- projectName =
KubernetesHelper.sanitize(FileUtil.onlyName(SourceScheme.onlyName(filePaths[0])));
+ } else if (filePath != null) {
+ projectName =
KubernetesHelper.sanitize(FileUtil.onlyName(SourceScheme.onlyName(filePath)));
} else {
printer().println("Name or source file must be set");
return 1;
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/PodLogs.java
b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/PodLogs.java
index 3195add85fd..ec29274f7d0 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/PodLogs.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/PodLogs.java
@@ -26,12 +26,18 @@ import io.fabric8.kubernetes.client.dsl.LogWatch;
import io.fabric8.kubernetes.client.dsl.PodResource;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.BaseTrait;
+import org.apache.camel.dsl.jbang.core.common.SourceScheme;
+import org.apache.camel.util.FileUtil;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@Command(name = "logs", description = "Print the logs of a Kubernetes pod",
sortOptions = false)
public class PodLogs extends KubernetesBaseCommand {
+ @CommandLine.Parameters(description = "The Camel file to get logs from.
Integration name is derived from the file name.",
+ arity = "0..1", paramLabel = "<file>")
+ protected String filePath;
+
@CommandLine.Option(names = { "--name" },
description = "The integration name. Use this when the
name should not get derived from the source file name.")
protected String name;
@@ -56,13 +62,20 @@ public class PodLogs extends KubernetesBaseCommand {
}
public Integer doCall() throws Exception {
- if (name == null && label == null) {
+ if (name == null && label == null && filePath == null) {
printer().println("Name or label selector must be set");
return 1;
}
- if (name != null) {
- label = "%s=%s".formatted(BaseTrait.INTEGRATION_LABEL, name);
+ if (label == null) {
+ String projectName;
+ if (name != null) {
+ projectName = KubernetesHelper.sanitize(name);
+ } else {
+ projectName =
KubernetesHelper.sanitize(FileUtil.onlyName(SourceScheme.onlyName(filePath)));
+ }
+
+ label = "%s=%s".formatted(BaseTrait.INTEGRATION_LABEL,
projectName);
}
String[] parts = label.split("=", 2);