Skip to content

Commit

Permalink
fix(#1280): Improve Camel JBang Kubernetes plugin support
Browse files Browse the repository at this point in the history
- Polish Camel JBang Kubernetes run command
- Add unit tests for Camel JBang plugin operations (add)
- Add unit tests for Camel JBang Kubernetes commands (run, delete, verify)
  • Loading branch information
christophd committed Feb 13, 2025
1 parent 760c970 commit bcfd8a4
Show file tree
Hide file tree
Showing 31 changed files with 1,532 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.citrusframework.actions.AbstractTestAction;
import org.citrusframework.camel.jbang.CamelJBang;
import org.citrusframework.camel.jbang.CamelJBangTestActor;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;

/**
* Abstract action to access Camel JBang tooling. Action provides common Camel JBang settings such as explicit Camel version.
Expand All @@ -29,13 +31,14 @@ public abstract class AbstractCamelJBangAction extends AbstractTestAction {
private final String camelVersion;
private final String kameletsVersion;

private final CamelJBang camelJBang = CamelJBang.camel();
private final CamelJBang camelJBang;

protected AbstractCamelJBangAction(String name, Builder<?, ?> builder) {
super(name, builder);

this.camelVersion = builder.camelVersion;
this.kameletsVersion = builder.kameletsVersion;
this.camelJBang = builder.camelJBang;

if (camelVersion != null) {
camelJBang.camelApp().withSystemProperty("camel.jbang.version", camelVersion);
Expand Down Expand Up @@ -65,11 +68,14 @@ public String getKameletsVersion() {
/**
* Action builder.
*/
public static abstract class Builder<T extends AbstractCamelJBangAction, B extends Builder<T, B>> extends AbstractTestActionBuilder<T, B> {
public static abstract class Builder<T extends AbstractCamelJBangAction, B extends Builder<T, B>> extends AbstractTestActionBuilder<T, B> implements ReferenceResolverAware {

protected CamelJBang camelJBang;
protected String camelVersion;
protected String kameletsVersion;

protected ReferenceResolver referenceResolver;

public Builder() {
actor(new CamelJBangTestActor());
}
Expand All @@ -94,5 +100,31 @@ public B kameletsVersion(String kameletsVersion) {
return self;
}

public B withReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
return self;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
}

@Override
public final T build() {
if (referenceResolver != null && referenceResolver.isResolvable(CamelJBang.class)) {
this.camelJBang = referenceResolver.resolve(CamelJBang.class);
} else {
camelJBang = CamelJBang.camel();
}

return doBuild();
}

/**
* Subclasses need to implement to create the
* @return
*/
protected abstract T doBuild();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AddCamelPluginAction extends AbstractCamelJBangAction {
* Default constructor.
*/
public AddCamelPluginAction(AddCamelPluginAction.Builder builder) {
super("plugin", builder);
super("plugin-add", builder);
this.name = builder.name;
this.args = builder.args;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public Builder withArgs(String... args) {
}

@Override
public AddCamelPluginAction build() {
public AddCamelPluginAction doBuild() {
return new AddCamelPluginAction(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package org.citrusframework.camel.actions;

import java.util.ArrayList;
import java.util.List;

import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.jbang.ProcessAndOutput;
import org.citrusframework.spi.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import static org.citrusframework.jbang.JBangSupport.OK_EXIT_CODE;

/**
* Delete kubernetes resources deployed from a Camel project or Camel integration with Camel JBang tooling.
Expand Down Expand Up @@ -63,42 +67,44 @@ public class CamelKubernetesDeleteAction extends AbstractCamelJBangAction {
* Default constructor.
*/
public CamelKubernetesDeleteAction(Builder builder) {
super("delete-kubernetes", builder);
super("kubernetes-delete-integration", builder);

this.integrationResource = builder.integrationResource;
this.integrationName = builder.integrationName;
this.clusterType = builder.clusterType;
this.workingDir = builder.workingDir;
this.namespace = builder.namespace;

}

@Override
public void doExecute(TestContext context) {
logger.info("Deleting integration deployed from a Camel Kubernetes project ...");
List<String> fullArgs = new ArrayList<>();
fullArgs.add("delete");
if (integrationResource != null) {
fullArgs.add(integrationResource.getFile().toPath().toAbsolutePath().toString());
}
List<String> commandArgs = new ArrayList<>();
if (integrationName != null) {
fullArgs.add("--name");
fullArgs.add(integrationName);
commandArgs.add("--name");
commandArgs.add(integrationName);
}
if (clusterType != null) {
fullArgs.add("--cluster-type");
fullArgs.add(clusterType);
commandArgs.add("--cluster-type");
commandArgs.add(clusterType);
}
if (workingDir != null) {
fullArgs.add("--working-dir");
fullArgs.add(workingDir);
commandArgs.add("--working-dir");
commandArgs.add(workingDir);
}
if (namespace != null) {
fullArgs.add("--namespace");
fullArgs.add(namespace);
commandArgs.add("--namespace");
commandArgs.add(namespace);
}
camelJBang().camelApp().run("kubernetes", fullArgs.toArray(String[]::new));

camelJBang().camelApp().workingDir(integrationResource.getFile().toPath().toAbsolutePath().getParent());

ProcessAndOutput pao = camelJBang().kubernetes().delete(integrationResource.getFile().getName(), commandArgs.toArray(String[]::new));
logger.info(pao.getOutput());
int exitValue = pao.getProcess().exitValue();
if (exitValue != OK_EXIT_CODE) {
throw new CitrusRuntimeException(String.format("Failed to delete Camel integration from Kubernetes - exit code %s", exitValue));
}
}

public Resource getIntegrationResource() {
Expand Down Expand Up @@ -189,7 +195,7 @@ public Builder namespace(String namespace) {
}

@Override
public CamelKubernetesDeleteAction build() {
public CamelKubernetesDeleteAction doBuild() {
return new CamelKubernetesDeleteAction(this);
}
}
Expand Down
Loading

0 comments on commit bcfd8a4

Please sign in to comment.