Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mgubaidullin committed Dec 12, 2024
1 parent 0fc8590 commit 71689b9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, String> files, String projectDevmodeImage, Map<String, String> labels, Map<String, String> 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<String, String> labels, Map<String, String> envVars) throws InterruptedException {
LOGGER.infof("DevMode starting for %s with verbose=%s", projectId, verbose);

Expand All @@ -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"));
Expand All @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -186,6 +186,9 @@ private Pod getBuilderPod(String name, Map<String, String> 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)
Expand Down Expand Up @@ -335,7 +338,7 @@ public List<String> getServices(String namespace) {
return result;
}

public void runDevModeContainer(Project project, Boolean verbose, Map<String, String> files, String projectDevmodeImage, String deploymentFragment, Map<String, String> labels, Map<String, String> envVars) {
public void runDevModeContainer(Project project, Boolean verbose, Boolean compile, Map<String, String> files, String projectDevmodeImage, String deploymentFragment, Map<String, String> labels, Map<String, String> envVars) {
String name = project.getProjectId();
Map<String, String> podLabels = new HashMap<>(labels);
podLabels.putAll(getLabels(name, project, ContainerType.devmode));
Expand All @@ -346,7 +349,7 @@ public void runDevModeContainer(Project project, Boolean verbose, Map<String, St
}
Pod old = client.pods().inNamespace(getNamespace()).withName(name).get();
if (old == null) {
Pod pod = getDevModePod(name, verbose, podLabels, projectDevmodeImage, deploymentFragment, envVars);
Pod pod = getDevModePod(name, verbose, compile, podLabels, projectDevmodeImage, deploymentFragment, envVars);
Pod result = client.resource(pod).serverSideApply();
copyFilesToContainer(result, files, "/karavan/code");
LOGGER.info("Created pod " + result.getMetadata().getName());
Expand Down Expand Up @@ -389,7 +392,7 @@ public ResourceRequirements getResourceRequirements(Map<String, String> containe
.build();
}

private Pod getDevModePod(String name, Boolean verbose, Map<String, String> labels, String projectDevmodeImage, String deploymentFragment, Map<String, String> envVars) {
private Pod getDevModePod(String name, Boolean verbose, Boolean compile, Map<String, String> labels, String projectDevmodeImage, String deploymentFragment, Map<String, String> envVars) {

Deployment deployment = Serialization.unmarshal(deploymentFragment, Deployment.class);
PodSpec podSpec = null;
Expand Down Expand Up @@ -420,7 +423,12 @@ private Pod getDevModePod(String name, Boolean verbose, Map<String, String> labe

List<EnvVar> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Project commitAndPushProject(String projectId, String message, String aut
}
}

public String runProjectWithJBangOptions(Project project, Boolean verbose, Map<String, String> labels, Map<String, String> envVars) throws Exception {
public String runProjectInDeveloperMode(Project project, Boolean verbose, Boolean compile, Map<String, String> labels, Map<String, String> envVars) throws Exception {
String containerName = project.getProjectId();
PodContainerStatus status = karavanCache.getDevModePodContainerStatus(project.getProjectId(), environment);
if (status == null) {
Expand All @@ -107,10 +107,10 @@ public String runProjectWithJBangOptions(Project project, Boolean verbose, Map<S
String projectDevmodeImage = codeService.getProjectDevModeImage(project.getProjectId());
if (ConfigService.inKubernetes()) {
String deploymentFragment = codeService.getDeploymentFragment(project.getProjectId());
kubernetesService.runDevModeContainer(project, verbose, files, projectDevmodeImage, deploymentFragment, labels, envVars);
kubernetesService.runDevModeContainer(project, verbose, compile, files, projectDevmodeImage, deploymentFragment, labels, envVars);
} else {
DockerComposeService compose = getProjectDockerComposeService(project.getProjectId());
dockerForKaravan.runProjectInDevMode(project.getProjectId(), verbose, compose, files, projectDevmodeImage, labels, envVars);
dockerForKaravan.runProjectInDevMode(project.getProjectId(), verbose, compile, compose, files, projectDevmodeImage, labels, envVars);
}
return containerName;
} else {
Expand Down Expand Up @@ -270,9 +270,8 @@ public Project copy(String sourceProjectId, Project project) throws Exception {

// Copy files from the source and make necessary modifications
Map<String, ProjectFile> 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 -> {
Expand All @@ -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<Integer> 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<Integer> 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() {
Expand Down

0 comments on commit 71689b9

Please sign in to comment.