Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mgubaidullin committed Jan 13, 2025
1 parent 0794ee0 commit e45cd97
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class KaravanConstants {

public static final String PROPERTY_FORMATTER_PROJECT_ID = PROPERTY_NAME_PROJECT_ID + "=%s";
public static final String PROPERTY_FORMATTER_PROJECT_NAME = PROPERTY_NAME_PROJECT_NAME + "=%s";
public static final String PROPERTY_FORMATTER_GAV = PROPERTY_NAME_GAV + "=org.camel.karavan.demo:%s:1";

public enum CamelRuntime {
CAMEL_MAIN("camel-main"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DevModeResource {
@Path("/{verbose}/{compile}")
public Response runProjectInDeveloperMode(Project project, @PathParam("verbose") boolean verbose, @PathParam("compile") boolean compile) {
try {
String containerName = projectService.runProjectInDeveloperMode(project, verbose, compile, Map.of(), Map.of());
String containerName = projectService.runProjectInDeveloperMode(project.getProjectId(), verbose, compile, Map.of(), Map.of());
if (containerName != null) {
return Response.ok(containerName).build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import com.github.dockerjava.api.model.RestartPolicy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.camel.karavan.model.*;
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.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
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 @@ -32,7 +30,8 @@
import jakarta.inject.Inject;
import org.apache.camel.karavan.KaravanConstants;
import org.apache.camel.karavan.model.ContainerType;
import org.apache.camel.karavan.model.Project;
import org.apache.camel.karavan.model.KubernetesConfigMap;
import org.apache.camel.karavan.model.KubernetesSecret;
import org.apache.camel.karavan.service.CodeService;
import org.apache.camel.karavan.service.ConfigService;
import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand Down Expand Up @@ -114,10 +113,10 @@ public void createConfigmap(String name, Map<String, String> data) {
}
}

public void runBuildProject(Project project, String podFragment) {
public void runBuildProject(String projectId, String podFragment) {
try (KubernetesClient client = kubernetesClient()) {
String containerName = project.getProjectId() + BUILDER_SUFFIX;
Map<String, String> labels = getLabels(containerName, project, ContainerType.build);
String containerName = projectId + BUILDER_SUFFIX;
Map<String, String> labels = getLabels(containerName, projectId, ContainerType.build);

// Delete old build pod
Pod old = client.pods().inNamespace(getNamespace()).withName(containerName).get();
Expand All @@ -134,11 +133,11 @@ public void runBuildProject(Project project, String podFragment) {
}
}

private Map<String, String> getLabels(String name, Project project, ContainerType type) {
private Map<String, String> getLabels(String name, String projectId, ContainerType type) {
Map<String, String> labels = new HashMap<>();
labels.putAll(getPartOfLabels());
labels.put("app.kubernetes.io/name", name);
labels.put(LABEL_PROJECT_ID, project.getProjectId());
labels.put(LABEL_PROJECT_ID, projectId);
if (type != null) {
labels.put(LABEL_TYPE, type.name());
}
Expand Down Expand Up @@ -338,24 +337,23 @@ public List<String> getServices(String namespace) {
return result;
}

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();
public void runDevModeContainer(String projectId, Boolean verbose, Boolean compile, Map<String, String> files, String projectDevmodeImage, String deploymentFragment, Map<String, String> labels, Map<String, String> envVars) {
Map<String, String> podLabels = new HashMap<>(labels);
podLabels.putAll(getLabels(name, project, ContainerType.devmode));
podLabels.putAll(getLabels(projectId, projectId, ContainerType.devmode));

try (KubernetesClient client = kubernetesClient()) {
if (devmodePVC.orElse(false)) {
createPVC(name, labels);
createPVC(projectId, labels);
}
Pod old = client.pods().inNamespace(getNamespace()).withName(name).get();
Pod old = client.pods().inNamespace(getNamespace()).withName(projectId).get();
if (old == null) {
Pod pod = getDevModePod(name, verbose, compile, podLabels, projectDevmodeImage, deploymentFragment, envVars);
Pod pod = getDevModePod(projectId, verbose, compile, podLabels, projectDevmodeImage, deploymentFragment, envVars);
Pod result = client.resource(pod).serverSideApply();
copyFilesToContainer(result, files, "/karavan/code");
LOGGER.info("Created pod " + result.getMetadata().getName());
}
}
createService(name, podLabels);
createService(projectId, podLabels);
}

private void copyFilesToContainer(Pod pod, Map<String, String> files, String dirName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum Name {
route,
trace,
jvm,
source
source,
debug
}

Name name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public String getProjectId() {
return projectId;
}

public String getGavPackageSuffix() {
return projectId.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
}

public void setProjectId(String projectId) {
this.projectId = projectId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public ProjectFile generateApplicationProperties(Project project) {
String template = getTemplateText(APPLICATION_PROPERTIES_FILENAME);
String code = substituteVariables(template, Map.of(
"projectId", project.getProjectId(),
"projectName", project.getName()
"projectName", project.getName(),
"packageSuffix", project.getGavPackageSuffix()
));
return new ProjectFile(APPLICATION_PROPERTIES_FILENAME, code, project.getProjectId(), Instant.now().toEpochMilli());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,24 @@ public Project commitAndPushProject(String projectId, String message, String aut
}
}

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);
public String runProjectInDeveloperMode(String projectId, Boolean verbose, Boolean compile, Map<String, String> labels, Map<String, String> envVars) throws Exception {
String containerName = projectId;
PodContainerStatus status = karavanCache.getDevModePodContainerStatus(projectId, environment);
if (status == null) {
status = PodContainerStatus.createDevMode(project.getProjectId(), environment);
status = PodContainerStatus.createDevMode(projectId, environment);
}
if (!Objects.equals(status.getState(), PodContainerStatus.State.running.name())) {
status.setInTransit(true);
eventBus.publish(POD_CONTAINER_UPDATED, JsonObject.mapFrom(status));

Map<String, String> files = codeService.getProjectFilesForDevMode(project.getProjectId(), true);
String projectDevmodeImage = codeService.getProjectDevModeImage(project.getProjectId());
Map<String, String> files = codeService.getProjectFilesForDevMode(projectId, true);
String projectDevmodeImage = codeService.getProjectDevModeImage(projectId);
if (ConfigService.inKubernetes()) {
String deploymentFragment = codeService.getDeploymentFragment(project.getProjectId());
kubernetesService.runDevModeContainer(project, verbose, compile, files, projectDevmodeImage, deploymentFragment, labels, envVars);
String deploymentFragment = codeService.getDeploymentFragment(projectId);
kubernetesService.runDevModeContainer(projectId, verbose, compile, files, projectDevmodeImage, deploymentFragment, labels, envVars);
} else {
DockerComposeService compose = getProjectDockerComposeService(project.getProjectId());
dockerForKaravan.runProjectInDevMode(project.getProjectId(), verbose, compile, compose, files, projectDevmodeImage, labels, envVars);
DockerComposeService compose = getProjectDockerComposeService(projectId);
dockerForKaravan.runProjectInDevMode(projectId, verbose, compile, compose, files, projectDevmodeImage, labels, envVars);
}
return containerName;
} else {
Expand All @@ -126,7 +126,7 @@ public void buildProject(Project project, String tag) throws Exception {
if (ConfigService.inKubernetes()) {
String podFragment = codeService.getBuilderPodFragment();
podFragment = codeService.substituteVariables(podFragment, Map.of( "projectId", project.getProjectId(), "tag", tag));
kubernetesService.runBuildProject(project, podFragment);
kubernetesService.runBuildProject(project.getProjectId(), podFragment);
} else {
Map<String, String> sshFiles = getSshFiles();
String composeFragment = codeService.getBuilderComposeFragment(project.getProjectId(), tag);
Expand Down Expand Up @@ -203,13 +203,13 @@ private void modifyPropertyFileOnProjectCopy(ProjectFile propertyFile, Project s

String sourceProjectIdProperty = String.format(PROPERTY_FORMATTER_PROJECT_ID, sourceProject.getProjectId());
String sourceProjectNameProperty = String.format(PROPERTY_FORMATTER_PROJECT_NAME, sourceProject.getName());
String sourceGavProperty = String.format(codeService.getGavFormatter(), sourceProject.getProjectId());
String sourceGavProperty = fileContent.lines().filter(line -> line.startsWith(PROPERTY_NAME_GAV)).findFirst().orElse("");

String[] searchValues = {sourceProjectIdProperty, sourceProjectNameProperty, sourceGavProperty};

String updatedProjectIdProperty = String.format(PROPERTY_FORMATTER_PROJECT_ID, project.getProjectId());
String updatedProjectNameProperty = String.format(PROPERTY_FORMATTER_PROJECT_NAME, project.getName());
String updatedGavProperty = String.format(codeService.getGavFormatter(), project.getProjectId());
String updatedGavProperty = String.format(codeService.getGavFormatter(), project.getGavPackageSuffix());

String[] replacementValues = {updatedProjectIdProperty, updatedProjectNameProperty, updatedGavProperty};

Expand Down
2 changes: 1 addition & 1 deletion karavan-app/src/main/webui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e45cd97

Please sign in to comment.