Skip to content

Commit

Permalink
fix(#1280): Improve Camel JBang Kubernetes plugin builder API
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Feb 13, 2025
1 parent 56a054a commit 760c970
Show file tree
Hide file tree
Showing 10 changed files with 519 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.citrusframework.camel.actions;

import org.citrusframework.context.TestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

import org.citrusframework.context.TestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Install a specific plugin to a Camel JBang tooling.
*/
Expand All @@ -46,26 +46,17 @@ public AddCamelPluginAction(AddCamelPluginAction.Builder builder) {
this.args = builder.args;
}


public String getName() {
return name;
}

@Override
public void doExecute(TestContext context) {
logger.info("Adding Camel plugin '%s' ...".formatted(name));
String pluginName = context.replaceDynamicContentInString(name);
List<String> installedPlugins = camelJBang().getPlugins();

if (!installedPlugins.contains(name)) {
List<String> fullArgs = new ArrayList<>();
fullArgs.add("add");
fullArgs.add(name);
if (args != null){
fullArgs.addAll(args);
}
camelJBang().camelApp().run("plugin", fullArgs.toArray(String[]::new));
if (!installedPlugins.contains(pluginName)) {
logger.info("Adding Camel plugin '%s' ...".formatted(pluginName));
camelJBang().addPlugin(pluginName, context.resolveDynamicValuesInList(args).toArray(String[]::new));
logger.info("Camel plugin '%s' successfully installed".formatted(pluginName));
} else {
logger.info("Adding Camel plugin '%s' skipped: already installed".formatted(name));
logger.info("Camel plugin '%s' already installed".formatted(pluginName));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,22 @@ public CamelStopIntegrationAction.Builder stop(String name) {
return builder;
}


/**
* Add a plugin to Camel JBang.
* Perform actions related to Camel JBang plugins.
* @return
*/
public AddCamelPluginAction.Builder addPlugin() {
AddCamelPluginAction.Builder builder = new AddCamelPluginAction.Builder();

public CamelPluginActionBuilder plugin() {
CamelPluginActionBuilder builder = new CamelPluginActionBuilder();
this.delegate = builder;
return builder;
}



/**
* Export a Camel project given Camel integration.
* Perform actions related to Camel JBang Kubernetes plugin.
* @return
*/
public CamelKubernetesRunIntegrationAction.Builder exportKubernetesPlugin() {
CamelKubernetesRunIntegrationAction.Builder builder = new CamelKubernetesRunIntegrationAction.Builder();

public CamelKubernetesPluginActionBuilder kubernetes() {
CamelKubernetesPluginActionBuilder builder = new CamelKubernetesPluginActionBuilder();
this.delegate = builder;
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.camel.actions;

import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.util.ObjectHelper;

public class CamelKubernetesPluginActionBuilder extends AbstractReferenceResolverAwareTestActionBuilder<AbstractCamelJBangAction> {

/**
* Build and deploy a Camel project to Kubernetes
*/
public CamelKubernetesRunIntegrationAction.Builder run() {
CamelKubernetesRunIntegrationAction.Builder builder = new CamelKubernetesRunIntegrationAction.Builder();
this.delegate = builder;
return builder;
}

/**
* Delete a deployed Camel application using the previous exported Kubernetes manifest.
*/
public CamelKubernetesDeleteAction.Builder delete() {
CamelKubernetesDeleteAction.Builder builder = new CamelKubernetesDeleteAction.Builder();
this.delegate = builder;
return builder;
}

/**
* Verify the Kubernetes pod status and logs of a deployed Camel integration.
*/
public CamelKubernetesVerifyAction.Builder verify() {
CamelKubernetesVerifyAction.Builder builder = new CamelKubernetesVerifyAction.Builder();
this.delegate = builder;
return builder;
}

/**
* Export a Camel project from given Camel integration.
*/
public CamelKubernetesRunIntegrationAction.Builder export() {
CamelKubernetesRunIntegrationAction.Builder builder = new CamelKubernetesRunIntegrationAction.Builder();
this.delegate = builder;
return builder;
}

/**
* Sets the bean reference resolver.
* @param referenceResolver
*/
public CamelKubernetesPluginActionBuilder withReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
return this;
}

@Override
public AbstractCamelJBangAction build() {
ObjectHelper.assertNotNull(delegate, "Missing delegate action to build");
return delegate.build();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.camel.actions;

import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.util.ObjectHelper;

public class CamelPluginActionBuilder extends AbstractReferenceResolverAwareTestActionBuilder<AbstractCamelJBangAction> {

/**
* Processor calling given Camel route as part of the message processing.
* @return
*/
public AddCamelPluginAction.Builder add() {
AddCamelPluginAction.Builder builder = new AddCamelPluginAction.Builder();
this.delegate = builder;
return builder;
}

/**
* Sets the bean reference resolver.
* @param referenceResolver
*/
public CamelPluginActionBuilder withReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
return this;
}

@Override
public AbstractCamelJBangAction build() {
ObjectHelper.assertNotNull(delegate, "Missing delegate action to build");
return delegate.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.citrusframework.jbang.JBangSupport.OK_EXIT_CODE;

/**
* Camel JBang app.
*/
Expand Down Expand Up @@ -134,7 +136,7 @@ public ProcessAndOutput run(String name, String file, List<String> resources, St
*/
public void stop(Long pid) {
ProcessAndOutput p = camelApp.run("stop", String.valueOf(pid)) ;
if (p.getProcess().exitValue() != JBangSupport.OK_EXIT_CODE) {
if (p.getProcess().exitValue() != OK_EXIT_CODE) {
throw new CitrusRuntimeException(String.format("Failed to stop Camel K integration - exit code %d", p.getProcess().exitValue()));
}
}
Expand Down Expand Up @@ -259,5 +261,17 @@ public List<String> getPlugins() {
}
}

public void addPlugin(String pluginName, String ... args) {
List<String> fullArgs = new ArrayList<>();
fullArgs.add("add");
fullArgs.add(pluginName);
fullArgs.addAll(Arrays.asList(args));

ProcessAndOutput pao = camelApp.run("plugin", fullArgs);
int exitValue = pao.getProcess().exitValue();
if (exitValue != OK_EXIT_CODE && exitValue != 1) {
throw new CitrusRuntimeException("Error while adding Camel JBang plugin. Exit code: " + exitValue);
}
}

}
Loading

0 comments on commit 760c970

Please sign in to comment.