From 71689b911cc32a9661ac724bd5026bbed95ea870 Mon Sep 17 00:00:00 2001 From: Marat Gubaidullin Date: Thu, 12 Dec 2024 18:12:42 -0500 Subject: [PATCH] Fix --- .../camel/karavan/KaravanConstants.java | 2 ++ .../camel/karavan/api/DevModeResource.java | 10 +++---- .../karavan/docker/DockerForKaravan.java | 15 ++++++----- .../karavan/kubernetes/KubernetesService.java | 20 +++++++++----- .../camel/karavan/service/ProjectService.java | 27 ++++++++++--------- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/KaravanConstants.java b/karavan-app/src/main/java/org/apache/camel/karavan/KaravanConstants.java index 3b2a40d8269..052f19fdf62 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/KaravanConstants.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/KaravanConstants.java @@ -22,6 +22,8 @@ public class KaravanConstants { public static final String DEVMODE_IMAGE = "karavan.devmode.image"; public static final String DEVMODE_IMAGE_PULL_POLICY = "karavan.devmode.withImagePullPolicy"; public static final String ENV_VAR_VERBOSE_OPTION_NAME = "VERBOSE_OPTION"; + public static final String RUN_IN_COMPILE_MODE = "RUN_IN_COMPILE_MODE"; + public static final String RUN_IN_BUILD_MODE = "RUN_IN_BUILD_MODE"; public static final String ENV_VAR_VERBOSE_OPTION_VALUE = "--verbose"; public static final String LABEL_PART_OF = "app.kubernetes.io/part-of"; diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/DevModeResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/DevModeResource.java index 6a0ef84d1dd..c731ac8cfc8 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/api/DevModeResource.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/DevModeResource.java @@ -54,10 +54,10 @@ public class DevModeResource { @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) - @Path("/{verbose}") - public Response runProjectWithJBangOptions(Project project, @PathParam("verbose") boolean verbose) { + @Path("/{verbose}/{compile}") + public Response runProjectInDeveloperMode(Project project, @PathParam("verbose") boolean verbose, @PathParam("compile") boolean compile) { try { - String containerName = projectService.runProjectWithJBangOptions(project, verbose, Map.of(), Map.of()); + String containerName = projectService.runProjectInDeveloperMode(project, verbose, compile, Map.of(), Map.of()); if (containerName != null) { return Response.ok(containerName).build(); } else { @@ -72,8 +72,8 @@ public Response runProjectWithJBangOptions(Project project, @PathParam("verbose" @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) - public Response runProject(Project project) throws Exception { - return runProjectWithJBangOptions(project, false); + public Response runProjectInDeveloperMode(Project project) throws Exception { + return runProjectInDeveloperMode(project, false, false); } @GET diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerForKaravan.java b/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerForKaravan.java index 69fcc826fa2..34aeb7277d9 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerForKaravan.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerForKaravan.java @@ -22,10 +22,7 @@ import com.github.dockerjava.api.model.RestartPolicy; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.apache.camel.karavan.model.ContainerType; -import org.apache.camel.karavan.model.DockerComposeService; -import org.apache.camel.karavan.model.DockerComposeVolume; -import org.apache.camel.karavan.model.Project; +import org.apache.camel.karavan.model.*; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.jboss.logging.Logger; @@ -48,14 +45,14 @@ public class DockerForKaravan { @Inject DockerService dockerService; - public void runProjectInDevMode(String projectId, Boolean verbose, DockerComposeService composeService, + public void runProjectInDevMode(String projectId, Boolean verbose, Boolean compile, DockerComposeService composeService, Map files, String projectDevmodeImage, Map labels, Map envVars) throws Exception { - Container c = createDevmodeContainer(projectId, verbose, composeService, projectDevmodeImage, labels, envVars); + Container c = createDevmodeContainer(projectId, verbose, compile, composeService, projectDevmodeImage, labels, envVars); dockerService.runContainer(projectId); dockerService.copyFiles(c.getId(), "/karavan/code", files, true); } - protected Container createDevmodeContainer(String projectId, Boolean verbose, DockerComposeService compose, + protected Container createDevmodeContainer(String projectId, Boolean verbose, Boolean compile, DockerComposeService compose, String projectDevmodeImage, Map labels, Map envVars) throws InterruptedException { LOGGER.infof("DevMode starting for %s with verbose=%s", projectId, verbose); @@ -67,6 +64,9 @@ protected Container createDevmodeContainer(String projectId, Boolean verbose, Do if (verbose) { env.add(ENV_VAR_VERBOSE_OPTION_NAME + "=" + ENV_VAR_VERBOSE_OPTION_VALUE); } + if (compile) { + env.add(RUN_IN_COMPILE_MODE + "=true"); + } if (createM2.orElse(false)) { compose.getVolumes().add(new DockerComposeVolume(MountType.VOLUME.name(), projectId+ "-m2-repository", "/karavan/.m2/repository")); @@ -93,6 +93,7 @@ public void runBuildProject(Project project, String script, DockerComposeService if (createM2.orElse(false)) { compose.getVolumes().add(new DockerComposeVolume(MountType.VOLUME.name(), project.getProjectId() + "-build-m2-repository", "/karavan/.m2/repository")); } + compose.addEnvironment(RUN_IN_BUILD_MODE, "true"); Container c = createBuildContainer(containerName, project, compose.getEnvironmentList(), compose.getVolumes(), tag); dockerService.copyExecFile(c.getId(), "/karavan/builder", BUILD_SCRIPT_FILENAME, script); sshFiles.forEach((name, text) -> { diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesService.java b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesService.java index 18b0b78fd22..6de4f7552e6 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesService.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesService.java @@ -16,6 +16,8 @@ */ package org.apache.camel.karavan.kubernetes; +import org.apache.camel.karavan.model.KubernetesConfigMap; +import org.apache.camel.karavan.model.KubernetesSecret; import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.api.model.apps.Deployment; import io.fabric8.kubernetes.client.KubernetesClient; @@ -30,8 +32,6 @@ import jakarta.inject.Inject; import org.apache.camel.karavan.KaravanConstants; import org.apache.camel.karavan.model.ContainerType; -import org.apache.camel.karavan.model.KubernetesConfigMap; -import org.apache.camel.karavan.model.KubernetesSecret; import org.apache.camel.karavan.model.Project; import org.apache.camel.karavan.service.CodeService; import org.apache.camel.karavan.service.ConfigService; @@ -186,6 +186,9 @@ private Pod getBuilderPod(String name, Map labels, String config } Pod pod = Serialization.unmarshal(configFragment, Pod.class); + + pod.getSpec().getContainers().get(0).getEnv().add(new EnvVarBuilder().withName(RUN_IN_BUILD_MODE).withValue("true").build()); + Container container = new ContainerBuilder() .withName(name) .withImage(devmodeImage) @@ -335,7 +338,7 @@ public List getServices(String namespace) { return result; } - public void runDevModeContainer(Project project, Boolean verbose, Map files, String projectDevmodeImage, String deploymentFragment, Map labels, Map envVars) { + public void runDevModeContainer(Project project, Boolean verbose, Boolean compile, Map files, String projectDevmodeImage, String deploymentFragment, Map labels, Map envVars) { String name = project.getProjectId(); Map podLabels = new HashMap<>(labels); podLabels.putAll(getLabels(name, project, ContainerType.devmode)); @@ -346,7 +349,7 @@ public void runDevModeContainer(Project project, Boolean verbose, Map containe .build(); } - private Pod getDevModePod(String name, Boolean verbose, Map labels, String projectDevmodeImage, String deploymentFragment, Map envVars) { + private Pod getDevModePod(String name, Boolean verbose, Boolean compile, Map labels, String projectDevmodeImage, String deploymentFragment, Map envVars) { Deployment deployment = Serialization.unmarshal(deploymentFragment, Deployment.class); PodSpec podSpec = null; @@ -420,7 +423,12 @@ private Pod getDevModePod(String name, Boolean verbose, Map labe List environmentVariables = new ArrayList<>(); envVars.forEach((k, v) -> environmentVariables.add(new EnvVarBuilder().withName(k).withValue(v).build())); - environmentVariables.add(new EnvVarBuilder().withName(ENV_VAR_VERBOSE_OPTION_NAME).withValue(ENV_VAR_VERBOSE_OPTION_VALUE).build()); + if (verbose) { + environmentVariables.add(new EnvVarBuilder().withName(ENV_VAR_VERBOSE_OPTION_NAME).withValue(ENV_VAR_VERBOSE_OPTION_VALUE).build()); + } + if (compile) { + environmentVariables.add(new EnvVarBuilder().withName(RUN_IN_COMPILE_MODE).withValue("true").build()); + } Container container = new ContainerBuilder() .withName(name) diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java index c7944a0c297..46ea88a8e61 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java @@ -93,7 +93,7 @@ public Project commitAndPushProject(String projectId, String message, String aut } } - public String runProjectWithJBangOptions(Project project, Boolean verbose, Map labels, Map envVars) throws Exception { + public String runProjectInDeveloperMode(Project project, Boolean verbose, Boolean compile, Map labels, Map envVars) throws Exception { String containerName = project.getProjectId(); PodContainerStatus status = karavanCache.getDevModePodContainerStatus(project.getProjectId(), environment); if (status == null) { @@ -107,10 +107,10 @@ public String runProjectWithJBangOptions(Project project, Boolean verbose, Map filesMap = karavanCache.getProjectFilesMap(sourceProjectId).entrySet().stream() - .filter(e -> !Objects.equals(e.getValue().getName(), PROJECT_COMPOSE_FILENAME) && - !Objects.equals(e.getValue().getName(), PROJECT_DEPLOYMENT_JKUBE_FILENAME) - ) + .filter(e -> !Objects.equals(e.getValue().getName(), PROJECT_COMPOSE_FILENAME)) + .filter(e -> !Objects.equals(e.getValue().getName(), PROJECT_DEPLOYMENT_JKUBE_FILENAME)) .collect(Collectors.toMap( e -> GroupedKey.create(project.getProjectId(), DEV, e.getValue().getName()), e -> { @@ -299,13 +298,17 @@ public Project copy(String sourceProjectId, Project project) throws Exception { } public Integer getProjectPort(ProjectFile composeFile) { - if (composeFile != null) { - DockerComposeService dcs = DockerComposeConverter.fromCode(composeFile.getCode(), composeFile.getProjectId()); - Optional port = dcs.getPortsMap().entrySet().stream() - .filter(e -> Objects.equals(e.getValue(), INTERNAL_PORT)).map(Map.Entry::getKey).findFirst(); - return port.orElse(null); + try { + if (composeFile != null) { + DockerComposeService dcs = DockerComposeConverter.fromCode(composeFile.getCode(), composeFile.getProjectId()); + Optional port = dcs.getPortsMap().entrySet().stream() + .filter(e -> Objects.equals(e.getValue(), INTERNAL_PORT)).map(Map.Entry::getKey).findFirst(); + return port.orElse(null); + } + return null; + } catch (Exception e) { + return null; } - return null; } private int getMaxPortMappedInProjects() {