Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
import org.apache.camel.tooling.maven.MavenGav;
import org.apache.camel.util.CamelCaseOrderedProperties;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.io.FileUtils;

class ExportCamelMain extends Export {
Expand Down Expand Up @@ -165,9 +167,12 @@ private void createMavenPom(File settings, File profile, File pom, Set<String> d
String context = IOHelper.loadText(is);
IOHelper.close(is);

if (camelVersion == null) {
CamelCatalog catalog = new DefaultCamelCatalog();
camelVersion = catalog.getCatalogVersion();
CamelCatalog catalog = new DefaultCamelCatalog();
if (ObjectHelper.isEmpty(camelVersion)) {
camelVersion = catalog.getLoadedVersion();
}
if (ObjectHelper.isEmpty(camelVersion)) {
camelVersion = VersionHelper.extractCamelVersion();
}

context = context.replaceAll("\\{\\{ \\.GroupId }}", ids[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.camel.util.CamelCaseOrderedProperties;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.io.FileUtils;

class ExportSpringBoot extends Export {
Expand Down Expand Up @@ -158,6 +159,12 @@ private void createMavenPom(File settings, File profile, File pom, Set<String> d
String repos = getMavenRepositories(settings, prop, camelSpringBootVersion);

CamelCatalog catalog = CatalogLoader.loadSpringBootCatalog(repos, camelSpringBootVersion);
if (ObjectHelper.isEmpty(camelVersion)) {
camelVersion = catalog.getLoadedVersion();
}
if (ObjectHelper.isEmpty(camelVersion)) {
camelVersion = VersionHelper.extractCamelVersion();
}

// First try to load a specialized template from the catalog, if the catalog does not provide it
// fallback to the template defined in camel-jbang-core
Expand All @@ -169,8 +176,6 @@ private void createMavenPom(File settings, File profile, File pom, Set<String> d
context = readResourceTemplate("templates/" + pomTemplateName);
}

String camelVersion = catalog.getLoadedVersion();

context = context.replaceAll("\\{\\{ \\.GroupId }}", ids[0]);
context = context.replaceAll("\\{\\{ \\.ArtifactId }}", ids[1]);
context = context.replaceAll("\\{\\{ \\.Version }}", ids[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ protected NonNamespaceOperation<Pod, PodList, PodResource> pods() {
/**
* Gets Kubernetes client. In case custom kubeConfig option is set initializes the client with the config otherwise
* uses default client.
*
* @return
*/
protected KubernetesClient client() {
if (kubernetesClient == null) {
Expand All @@ -100,8 +98,6 @@ protected KubernetesClient client() {

/**
* Sets the Kubernetes client.
*
* @param kubernetesClient
*/
public KubernetesBaseCommand withClient(KubernetesClient kubernetesClient) {
this.kubernetesClient = kubernetesClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,13 @@ public class KubernetesRun extends KubernetesBaseCommand {
description = "The target cluster type. Special configurations may be applied to different cluster types such as Kind or Minikube.")
String clusterType;

@CommandLine.Option(names = { "--image-build" },
defaultValue = "true",
description = "Weather to build container image as part of the run.")
@CommandLine.Option(names = { "--image-build" }, defaultValue = "true",
description = "Whether to build container image as part of the run.")
boolean imageBuild = true;

@CommandLine.Option(names = { "--image-push" },
defaultValue = "true",
description = "Weather to push image to given image registry as part of the run.")
boolean imagePush = true;
@CommandLine.Option(names = { "--image-push" }, defaultValue = "false",
description = "Whether to push image to given image registry as part of the run.")
boolean imagePush = false;

@CommandLine.Option(names = { "--image-platforms" },
description = "List of target platforms. Each platform is defined using the pattern.")
Expand Down Expand Up @@ -242,6 +240,11 @@ public KubernetesRun(CamelJBangMain main) {
super(main);
}

public KubernetesRun(CamelJBangMain main, String[] files) {
super(main);
filePaths = files;
}

public Integer doCall() throws Exception {
String projectName = getProjectName();

Expand All @@ -266,11 +269,8 @@ public Integer doCall() throws Exception {
}

if (output != null) {
if (RuntimeType.quarkus == runtime) {
exit = buildQuarkus(workingDir);
} else if (RuntimeType.springBoot == runtime) {
exit = buildSpringBoot(workingDir);
}

exit = buildProject(workingDir);

if (exit != 0) {
printer().println("Project build failed!");
Expand All @@ -295,11 +295,7 @@ public Integer doCall() throws Exception {
return 0;
}

if (RuntimeType.quarkus == runtime) {
exit = deployQuarkus(workingDir);
} else if (RuntimeType.springBoot == runtime) {
exit = deploySpringBoot(workingDir);
}
exit = deployProject(workingDir);

if (exit != 0) {
printer().println("Deployment to %s failed!".formatted(Optional.ofNullable(clusterType)
Expand All @@ -309,7 +305,7 @@ public Integer doCall() throws Exception {

if (dev) {
DefaultCamelContext reloadContext = new DefaultCamelContext(false);
configureFileWatch(reloadContext, workingDir);
configureFileWatch(reloadContext, export, workingDir);
reloadContext.start();

if (cleanup) {
Expand All @@ -318,6 +314,14 @@ public Integer doCall() throws Exception {
}

if (dev || wait || logs) {

String kubectlCmd = "kubectl get pod";
if (namespace != null) {
kubectlCmd += " -n %s".formatted(namespace);
}
kubectlCmd += " -l %s=%s".formatted(BaseTrait.INTEGRATION_LABEL, projectName);
printer().println(kubectlCmd);

client(Pod.class).withLabel(BaseTrait.INTEGRATION_LABEL, projectName)
.waitUntilCondition(it -> "Running".equals(it.getStatus().getPhase()), 10, TimeUnit.MINUTES);
}
Expand Down Expand Up @@ -407,10 +411,10 @@ private void installShutdownInterceptor(String projectName, String workingDir) {
Runtime.getRuntime().addShutdownHook(task);
}

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

// Run Quarkus build via Maven
// Run build via Maven
String mvnw = "/mvnw";
if (FileUtil.isWindows()) {
mvnw = "/mvnw.cmd";
Expand All @@ -437,11 +441,11 @@ private Integer buildQuarkus(String workingDir) throws IOException, InterruptedE
return 0;
}

private Integer deployQuarkus(String workingDir) throws IOException, InterruptedException {
private Integer deployProject(String workingDir) throws IOException, InterruptedException {
printer().println("Deploying to %s ...".formatted(Optional.ofNullable(clusterType)
.map(StringHelper::capitalize).orElse("Kubernetes")));

// Run Quarkus build via Maven
// Run build via Maven
String mvnw = "/mvnw";
if (FileUtil.isWindows()) {
mvnw = "/mvnw.cmd";
Expand All @@ -454,29 +458,46 @@ private Integer deployQuarkus(String workingDir) throws IOException, Interrupted
args.add("--file");
args.add(workingDir);

if (imagePlatforms != null) {
args.add("-Dquarkus.jib.platforms=%s".formatted(imagePlatforms));
}
if (runtime == RuntimeType.quarkus) {

if (imageBuild) {
args.add("-Dquarkus.container-image.build=true");
}
if (imagePlatforms != null) {
args.add("-Dquarkus.jib.platforms=%s".formatted(imagePlatforms));
}

if (imagePush) {
args.add("-Dquarkus.container-image.push=true");
}
if (imageBuild) {
args.add("-Dquarkus.container-image.build=true");
}

if (imagePush) {
args.add("-Dquarkus.container-image.push=true");
}

if (ClusterType.OPENSHIFT.isEqualTo(clusterType)) {
args.add("-Dquarkus.openshift.deploy=true");
} else {
args.add("-Dquarkus.kubernetes.deploy=true");
}

args.add("package");

if (ClusterType.OPENSHIFT.isEqualTo(clusterType)) {
args.add("-Dquarkus.openshift.deploy=true");
} else {
args.add("-Dquarkus.kubernetes.deploy=true");
}

if (namespace != null) {
args.add("-Dquarkus.kubernetes.namespace=%s".formatted(namespace));
if (!imageBuild) {
args.add("-Djkube.skip.build=true");
}

if (imagePush) {
args.add("-Djkube.%s.push=true".formatted(imageBuilder));
}

if (namespace != null) {
args.add("-Djkube.namespace=%s".formatted(namespace));
}

args.add("package");
args.add("k8s:deploy");
}

args.add("package");
pb.command(args.toArray(String[]::new));

pb.inheritIO(); // run in foreground (with IO so logs are visible)
Expand All @@ -491,22 +512,7 @@ private Integer deployQuarkus(String workingDir) throws IOException, Interrupted
return 0;
}

private Integer buildSpringBoot(String workingDir) {
printer().println("Building Spring Boot application ...");

// TODO: implement SpringBoot project build
return 0;
}

private Integer deploySpringBoot(String workingDir) {
printer().println("Deploying to %s ...".formatted(Optional.ofNullable(clusterType)
.map(StringHelper::capitalize).orElse("Kubernetes")));

// TODO: implement SpringBoot Kubernetes deployment
return 0;
}

private void configureFileWatch(DefaultCamelContext camelContext, String workingDir)
private void configureFileWatch(DefaultCamelContext camelContext, KubernetesExport export, String workingDir)
throws Exception {
String watchDir = ".";
FileFilter filter = null;
Expand All @@ -525,17 +531,9 @@ private void configureFileWatch(DefaultCamelContext camelContext, String working
= new FileWatcherResourceReloadStrategy(watchDir);
reloadStrategy.setResourceReload((name, resource) -> {
printer().printf("Reloading project due to file change: %s%n", FileUtil.stripPath(name));
KubernetesExport export = configureExport(workingDir);
int refresh = export.export();
if (refresh == 0) {
if (RuntimeType.quarkus == runtime) {
deployQuarkus(workingDir);
} else if (RuntimeType.springBoot == runtime) {
deploySpringBoot(workingDir);
}
} else {
// print export command output with error details
printer().printf("Reloading project failed - export failed with code: %d%n", refresh);
deployProject(workingDir);
}
});
if (filter != null) {
Expand Down Expand Up @@ -563,5 +561,4 @@ private String getProjectName() {
throw new RuntimeCamelException(
"Failed to resolve project name - please provide --gav, --image option or at least one source file");
}

}
Loading