You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ma...@apache.org on 2026/08/25 15:19:32 UTC
(camel-karavan) 07/42: Karavan-app java backend update for 4.22.0
This is an automated email from the ASF dual-hosted git repository.
mgubaidullin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
commit 7623d67f4914fc2d6363487c965341720d904684
Author: Marat Gubaidullin <ma...@talismancloud.io>
AuthorDate: Mon Aug 24 13:05:17 2026 -0400
Karavan-app java backend update for 4.22.0
---
.../org/apache/camel/karavan/KaravanConstants.java | 4 +-
.../org/apache/camel/karavan/KaravanEvents.java | 3 -
.../camel/karavan/api/AbstractApiResource.java | 32 +-
.../apache/camel/karavan/api/AccessResource.java | 119 +++++-
.../org/apache/camel/karavan/api/AuthResource.java | 23 +-
.../camel/karavan/api/ConfigurationResource.java | 25 +-
.../camel/karavan/api/ContainerResource.java | 64 +---
.../karavan/api/CookieSessionAuthMechanism.java | 14 +-
.../apache/camel/karavan/api/DevModeResource.java | 1 -
.../camel/karavan/api/InfrastructureResource.java | 6 +-
.../apache/camel/karavan/api/InternalResource.java | 179 +++++++++
.../apache/camel/karavan/api/LabelResource.java | 3 -
.../camel/karavan/api/MetadataResources.java | 18 -
.../apache/camel/karavan/api/ProjectResource.java | 31 +-
.../karavan/api/TokenAuthenticationMechanism.java | 106 ++++++
.../apache/camel/karavan/cache/AccessToken.java | 35 ++
.../camel/karavan/cache/CamelStatusValue.java | 4 +-
.../apache/camel/karavan/cache/KaravanCache.java | 72 +++-
.../camel/karavan/cache/PodContainerStatus.java | 14 +-
.../apache/camel/karavan/cache/ProjectFile.java | 5 +
.../apache/camel/karavan/cache/ProjectFolder.java | 8 +-
.../camel/karavan/complexity/Complexity.java | 5 -
.../karavan/complexity/ComplexityComponent.java | 19 +-
.../camel/karavan/complexity/ComplexityFile.java | 90 -----
.../karavan/complexity/ComplexityProject.java | 66 +---
.../camel/karavan/complexity/ComplexityRoute.java | 80 ++---
.../camel/karavan/docker/DockerForKaravan.java | 99 +----
.../apache/camel/karavan/docker/DockerService.java | 66 +---
.../camel/karavan/docker/DockerStackConverter.java | 177 ---------
.../docker/StackToServiceSpecConverter.java | 400 ---------------------
.../karavan/kubernetes/KubernetesService.java | 199 +++++-----
.../kubernetes/KubernetesStatusService.java | 10 +-
.../camel/karavan/kubernetes/PodEventHandler.java | 19 +-
.../karavan/listener/CamelReloadListener.java | 7 +-
.../karavan/listener/CamelStatusListener.java | 17 +-
.../camel/karavan/listener/ConfigListener.java | 67 ----
.../camel/karavan/listener/DockerListener.java | 13 +-
.../karavan/listener/DockerStatusListener.java | 20 +-
.../karavan/listener/NotificationListener.java | 12 -
.../listener/PodContainerCommandListener.java | 2 -
.../apache/camel/karavan/loader/CacheLoader.java | 2 +
.../org/apache/camel/karavan/loader/GitLoader.java | 2 +
.../apache/camel/karavan/model/Configuration.java | 11 +-
.../apache/camel/karavan/model/DockerStack.java | 80 -----
.../camel/karavan/model/DockerStackService.java | 273 --------------
.../org/apache/camel/karavan/model/PodEvent.java | 68 +---
.../karavan/persistence/PersistenceService.java | 33 +-
.../karavan/scheduler/CamelStatusScheduler.java | 9 +-
.../karavan/scheduler/DockerStatusScheduler.java | 30 +-
.../apache/camel/karavan/service/AuthService.java | 20 +-
.../apache/camel/karavan/service/CodeService.java | 85 +++--
.../camel/karavan/service/ComplexityService.java | 277 +++++---------
.../camel/karavan/service/ConfigService.java | 69 ----
.../camel/karavan/service/ProjectService.java | 69 ++--
54 files changed, 1110 insertions(+), 2052 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 f3b1485f..fd29c53d 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
@@ -26,21 +26,19 @@ public class KaravanConstants {
public static final String ENV_VAR_RUN_IN_BUILD_MODE = "RUN_IN_BUILD_MODE";
public static final String ENV_VAR_VERBOSE_OPTION_VALUE = "--verbose";
public static final String ENV_VAR_BUILDER_SESSION_ID = "BUILDER_SESSION_ID";
+ public static final String ENV_VAR_PROJECT_ID = "PROJECT_ID";
public static final String LABEL_PART_OF = "app.kubernetes.io/part-of";
public static final String LABEL_TYPE = "org.apache.camel.karavan/type";
public static final String LABEL_PROJECT_ID = "org.apache.camel.karavan/projectId";
public static final String LABEL_CAMEL_RUNTIME = "org.apache.camel.karavan/runtime";
public static final String LABEL_TAG = "org.apache.camel.karavan/tag";
- public static final String LABEL_DOCKER_STACK_NAMESPACE = "com.docker.stack.namespace";
- public static final String LABEL_SWARM_SERVICE_ID = "com.docker.swarm.service.id";
public static final String BUILDER_SUFFIX = "-builder";
public static final String CAMEL_PREFIX = "camel";
public static final String BUILD_SCRIPT_VOLUME_NAME = "build-script";
- public static final String BUILD_SCRIPT_CONFIG_MAP = "build.sh";
public static final String BUILD_DOCKER_CONFIG_SECRET = "dockerconfigjson";
public static final String PRIVATE_KEY_SECRET_KEY = "private-key";
public static final String KNOWN_HOSTS_SECRET_KEY = "known-hosts";
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/KaravanEvents.java b/karavan-app/src/main/java/org/apache/camel/karavan/KaravanEvents.java
index 6fedc9fb..aac4a7b1 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/KaravanEvents.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/KaravanEvents.java
@@ -26,9 +26,6 @@ public class KaravanEvents {
public static final String COMMIT_HAPPENED = "COMMIT_HAPPENED";
public static final String NOTIFICATION_IMAGES_LOADED = "NOTIFICATION_IMAGES_LOADED";
- public static final String CMD_SHARE_CONFIGURATION = "CMD_SHARE_CONFIGURATION";
- public static final String NOTIFICATION_CONFIG_SHARED = "NOTIFICATION_CONFIG_SHARED";
-
public static final String NOTIFICATION_ERROR = "NOTIFICATION_ERROR";
public static final String CMD_COLLECT_CAMEL_STATUS = "CMD_COLLECT_CAMEL_STATUS";
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
index 9f63e6c4..9c29d311 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
@@ -20,10 +20,21 @@ import io.quarkus.security.identity.SecurityIdentity;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import jakarta.inject.Inject;
+import org.apache.camel.karavan.cache.AccessUser;
import org.apache.camel.karavan.cache.KaravanCache;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.eclipse.microprofile.jwt.JsonWebToken;
+import org.jboss.logging.Logger;
+
+import java.util.Objects;
public class AbstractApiResource {
+ private static final Logger LOGGER = Logger.getLogger(AbstractApiResource.class.getName());
+
+ @ConfigProperty(name = "platform.auth", defaultValue = "session")
+ String authStrategy;
+
@Inject
SecurityIdentity identity;
@@ -39,9 +50,28 @@ public class AbstractApiResource {
}
String username = identity.getPrincipal().getName();
+ var roles = new JsonArray(new java.util.ArrayList<>(identity.getRoles()));
+ var roleList = roles.stream().map(Object::toString).toList();
var user = karavanCache.getUser(username);
- var roles = new JsonArray(new java.util.ArrayList<>(identity.getRoles()));
+ if ("oidc".equalsIgnoreCase(authStrategy)) {
+ JsonWebToken jwt = (JsonWebToken) identity.getPrincipal();
+ String firstName = jwt.getClaim("given_name");
+ String lastName = jwt.getClaim("family_name");
+ String email = jwt.getClaim("email");
+ if (user == null) {
+ LOGGER.info("OIDC User not found in Talisman Database. Creating new one.");
+ user = new AccessUser(username, firstName, lastName, email, AccessUser.UserStatus.ACTIVE, roleList);
+ karavanCache.saveUser(user, true);
+ } else if (!Objects.equals(firstName, user.getFirstName()) || !Objects.equals(lastName, user.getLastName()) || !Objects.equals(email, user.getEmail())) {
+ LOGGER.info("OIDC User found in Talisman Database. User is outdated. Updating user.");
+ user.setFirstName(firstName);
+ user.setLastName(lastName);
+ user.setEmail(email);
+ user.setRoles(roleList);
+ karavanCache.saveUser(user, true);
+ }
+ }
return JsonObject.of()
.put("email", user != null ? user.getEmail() : null)
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/AccessResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/AccessResource.java
index f8605463..573b505d 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/AccessResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/AccessResource.java
@@ -7,13 +7,17 @@ import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.camel.karavan.cache.AccessRole;
+import org.apache.camel.karavan.cache.AccessToken;
import org.apache.camel.karavan.cache.AccessUser;
import org.apache.camel.karavan.cache.KaravanCache;
import org.apache.camel.karavan.service.AuthService;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.SecureRandom;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.*;
import static org.apache.camel.karavan.service.AuthService.*;
@@ -26,6 +30,16 @@ public class AccessResource extends AbstractApiResource {
@Inject
AuthService authService;
+ public record SessionInfo(String username, long createdAt, long expiredAt) {}
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/sessions")
+ @RolesAllowed({ROLE_ADMIN})
+ public List<SessionInfo> getAllSessions() {
+ return karavanCache.getAccessSessions().stream().map(s -> new SessionInfo(s.username, s.createdAtMillis, s.expiredAt.toEpochMilli())).toList();
+ }
+
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/users")
@@ -42,6 +56,16 @@ public class AccessResource extends AbstractApiResource {
return karavanCache.getRoles();
}
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/tokens")
+ @RolesAllowed({ROLE_ADMIN})
+ public List<AccessToken> getAllTokens() {
+ return karavanCache.getTokens().stream()
+ .map(t -> new AccessToken(t.hashedToken().substring(0, 16), t.ownerName(), t.clientName(), t.allowedProjectIds(), t.createdAt(), t.expiresAt()))
+ .toList();
+ }
+
@POST
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ROLE_ADMIN})
@@ -60,6 +84,58 @@ public class AccessResource extends AbstractApiResource {
return Response.ok().entity(role).build();
}
+ @POST
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ @RolesAllowed({ROLE_ADMIN, ROLE_DEVELOPER})
+ @Path("/tokens")
+ public Response generateToken(GenerateTokenRequest request) {
+ try {
+ // 1. Handle defaults if the frontend omits them
+ int expiresInDays = (request.expiresInDays() != null && request.expiresInDays() > 0)
+ ? request.expiresInDays()
+ : 30;
+
+ Set<String> allowedProjectIds = (request.allowedProjectIds() != null && !request.allowedProjectIds().isEmpty())
+ ? request.allowedProjectIds()
+ : Set.of("*"); // Default to all projects if none specified
+
+ // 2. Generate a secure random raw token
+ SecureRandom random = new SecureRandom();
+ byte[] bytes = new byte[32];
+ random.nextBytes(bytes);
+ String rawToken = "tal_" + Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
+
+ // 3. Hash the raw token via SHA-256
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+ byte[] encodedhash = digest.digest(rawToken.getBytes(StandardCharsets.UTF_8));
+ String hashedToken = HexFormat.of().formatHex(encodedhash);
+
+ // 4. Extract the current user to set as the owner
+ String ownerName = getIdentity().getString("username");
+
+ // 5. Create the Metadata Record
+ AccessToken tokenMetadata = new AccessToken(
+ hashedToken,
+ ownerName,
+ request.clientName(),
+ allowedProjectIds,
+ Instant.now(),
+ Instant.now().plus(expiresInDays, ChronoUnit.DAYS)
+ );
+
+ // 6. Save ONLY the hashed metadata to the cache
+ karavanCache.saveToken(tokenMetadata, true);
+
+ // 7. Return the strict Response DTO
+ GenerateTokenResponse response = new GenerateTokenResponse(rawToken, tokenMetadata);
+ return Response.ok(response).build();
+
+ } catch (Exception e) {
+ return Response.serverError().entity(e.getMessage()).build();
+ }
+ }
+
@PUT
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ROLE_USER})
@@ -121,6 +197,19 @@ public class AccessResource extends AbstractApiResource {
}
}
+ @DELETE
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed({ROLE_ADMIN})
+ @Path("/sessions/{username}")
+ public Response deleteSession(@PathParam("username") String username) {
+ try {
+ karavanCache.deleteAccessSessionByUsername(username);
+ return Response.accepted().build();
+ } catch (Exception e) {
+ return Response.notModified().build();
+ }
+ }
+
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ROLE_ADMIN})
@@ -140,6 +229,19 @@ public class AccessResource extends AbstractApiResource {
}
}
+ @DELETE
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed({ROLE_ADMIN})
+ @Path("/tokens/{hashedToken}")
+ public Response deleteToken(@PathParam("hashedToken") String hashedToken) {
+ try {
+ karavanCache.deleteToken(hashedToken);
+ return Response.accepted().build();
+ } catch (Exception e) {
+ return Response.notModified().build();
+ }
+ }
+
@POST
@Path("/password")
@RolesAllowed({ROLE_ADMIN})
@@ -157,4 +259,15 @@ public class AccessResource extends AbstractApiResource {
return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build();
}
}
+
+ public record GenerateTokenRequest(
+ String clientName,
+ Set<String> allowedProjectIds,
+ Integer expiresInDays
+ ) {}
+
+ public record GenerateTokenResponse(
+ String rawToken,
+ AccessToken metadata
+ ) {}
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/AuthResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/AuthResource.java
index e3f5fe0e..6c4ce241 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/AuthResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/AuthResource.java
@@ -21,7 +21,7 @@ import static org.apache.camel.karavan.service.AuthService.SESSION_MAX_AGE;
public class AuthResource extends AbstractApiResource {
private static final Logger LOGGER = Logger.getLogger(AuthResource.class.getName());
- private static final String SESSION_ID = "taskId";
+ private static final String SESSION_ID = "sessionId";
private static final String CSRF = "csrf";
@Inject
@@ -59,9 +59,24 @@ public class AuthResource extends AbstractApiResource {
public Response login(JsonObject body) throws Exception {
try {
final AccessUser user = authService.login(body.getString("username"), body.getString("password"));
- var session = authService.createAndSaveSession(user.username, true);
- NewCookie sidCookie = new NewCookie.Builder(SESSION_ID).value(session.sessionId).path("/").maxAge(SESSION_MAX_AGE).secure(true).httpOnly(true).build();
- NewCookie csrfCookie = new NewCookie.Builder(CSRF).value(session.csrfToken).path("/").maxAge(SESSION_MAX_AGE).secure(false).httpOnly(true).build();
+ var session = authService.createAndSaveSession(user.username, true, false);
+ // Session ID: Secure from interception, hidden from JavaScript
+ NewCookie sidCookie = new NewCookie.Builder(SESSION_ID)
+ .value(session.sessionId)
+ .path("/")
+ .maxAge(SESSION_MAX_AGE)
+ .secure(true)
+ .httpOnly(true)
+ .build();
+
+ // CSRF Token: Secure from interception, readable by JavaScript
+ NewCookie csrfCookie = new NewCookie.Builder(CSRF)
+ .value(session.csrfToken)
+ .path("/")
+ .maxAge(SESSION_MAX_AGE)
+ .secure(true) // Protect against MitM attacks
+ .httpOnly(false) // Allow frontend JS to read and send as a header
+ .build();
return Response.ok(JsonObject.of("username", user.getUsername(), "roles", user.getRoles()))
.cookie(sidCookie).cookie(csrfCookie).build();
} catch (Exception e) {
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/ConfigurationResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/ConfigurationResource.java
index 06cf86c1..1101d665 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/ConfigurationResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/ConfigurationResource.java
@@ -17,19 +17,16 @@
package org.apache.camel.karavan.api;
import io.quarkus.security.Authenticated;
-import io.vertx.core.json.JsonObject;
import io.vertx.mutiny.core.eventbus.EventBus;
import jakarta.inject.Inject;
-import jakarta.ws.rs.*;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.camel.karavan.docker.DockerService;
import org.apache.camel.karavan.service.ConfigService;
-import java.util.HashMap;
-
-import static org.apache.camel.karavan.KaravanEvents.CMD_SHARE_CONFIGURATION;
-
@Path("/ui/configuration")
public class ConfigurationResource {
@@ -43,12 +40,14 @@ public class ConfigurationResource {
EventBus eventBus;
@GET
+ @Authenticated
@Produces(MediaType.APPLICATION_JSON)
public Response getConfiguration() throws Exception {
return Response.ok(configService.getConfiguration(null)).build();
}
@GET
+ @Authenticated
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
public Response getInfo() throws Exception {
@@ -58,18 +57,4 @@ public class ConfigurationResource {
return Response.ok(dockerService.getInfo()).build();
}
}
-
- @POST
- @Path("/share/")
- @Authenticated
- @Produces(MediaType.APPLICATION_JSON)
- @Consumes(MediaType.APPLICATION_JSON)
- public Response share(HashMap<String, String> params) {
- try {
- eventBus.publish(CMD_SHARE_CONFIGURATION, JsonObject.mapFrom(params));
- return Response.ok().build();
- } catch (Exception e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
- }
- }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/ContainerResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/ContainerResource.java
index 0846e81b..a5353a29 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/ContainerResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/ContainerResource.java
@@ -30,10 +30,8 @@ import org.apache.camel.karavan.cache.ContainerType;
import org.apache.camel.karavan.cache.KaravanCache;
import org.apache.camel.karavan.cache.PodContainerStatus;
import org.apache.camel.karavan.docker.DockerService;
-import org.apache.camel.karavan.docker.StackToServiceSpecConverter;
import org.apache.camel.karavan.kubernetes.KubernetesService;
import org.apache.camel.karavan.model.DockerComposeService;
-import org.apache.camel.karavan.model.DockerStackService;
import org.apache.camel.karavan.service.ConfigService;
import org.apache.camel.karavan.service.ProjectService;
import org.eclipse.microprofile.config.inject.ConfigProperty;
@@ -72,7 +70,7 @@ public class ContainerResource {
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
public List<PodContainerStatus> getAllContainerStatuses() throws Exception {
- return karavanCache.getPodContainerStatuses().stream()
+ return karavanCache.getPodContainerStatusesByProject().stream()
.sorted(Comparator.comparing(PodContainerStatus::getProjectId, Comparator.nullsLast(String::compareTo)))
.collect(Collectors.toList());
}
@@ -94,24 +92,16 @@ public class ContainerResource {
setContainerStatusTransit(projectId, name, type);
// exec docker commands
if (command.containsKey("command")) {
- if (dockerService.isInSwarmMode()) {
- if (command.getString("command").equalsIgnoreCase("deploy")) {
- deployService(projectId, type, command);
- } else if (command.getString("command").equalsIgnoreCase("delete")) {
- dockerService.deleteService(projectId);
- }
- } else {
- if (command.getString("command").equalsIgnoreCase("deploy")) {
- deployContainer(projectId, type, command);
- } else if (command.getString("command").equalsIgnoreCase("run")) {
- dockerService.runContainer(name);
- } else if (command.getString("command").equalsIgnoreCase("stop")) {
- dockerService.stopContainer(name);
- } else if (command.getString("command").equalsIgnoreCase("pause")) {
- dockerService.pauseContainer(name);
- } else if (command.getString("command").equalsIgnoreCase("delete")) {
- dockerService.deleteContainer(name);
- }
+ if (command.getString("command").equalsIgnoreCase("deploy")) {
+ deployContainer(projectId, type, command);
+ } else if (command.getString("command").equalsIgnoreCase("run")) {
+ dockerService.runContainer(name);
+ } else if (command.getString("command").equalsIgnoreCase("stop")) {
+ dockerService.stopContainer(name);
+ } else if (command.getString("command").equalsIgnoreCase("pause")) {
+ dockerService.pauseContainer(name);
+ } else if (command.getString("command").equalsIgnoreCase("delete")) {
+ dockerService.deleteContainer(name);
}
return Response.ok().build();
}
@@ -143,25 +133,6 @@ public class ContainerResource {
}
}
- public void deployService(String projectId, String type, JsonObject command) throws InterruptedException {
- if (Objects.equals(type, ContainerType.packaged.name())) {
- DockerStackService stack = projectService.getProjectDockerStackService(projectId);
- if (stack != null) {
- Map<String, String> labels = new HashMap<>();
- labels.put(LABEL_TYPE, ContainerType.packaged.name());
- labels.put(LABEL_CAMEL_RUNTIME, CamelRuntime.CAMEL_MAIN.getValue());
- labels.put(LABEL_PROJECT_ID, projectId);
- stack.setLabels(labels);
- var serviceSpec = StackToServiceSpecConverter.convertService(projectId, stack);
- dockerService.createService(projectId, serviceSpec);
- }
- } else if (Objects.equals(type, ContainerType.devmode.name())) {
-// TODO: merge with DevMode service
-// dockerForKaravan.createDevmodeContainer(name, "");
-// dockerService.runContainer(name);
- }
- }
-
private DockerService.PULL_IMAGE needPull(JsonObject command) {
try {
return DockerService.PULL_IMAGE.valueOf(command.getString("pullImage"));
@@ -183,17 +154,17 @@ public class ContainerResource {
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
public List<PodContainerStatus> getContainerStatusesByEnv(@PathParam("env") String env) throws Exception {
- return karavanCache.getPodContainerStatuses(env).stream()
+ return karavanCache.getPodContainerStatusesByProject(env).stream()
.sorted(Comparator.comparing(PodContainerStatus::getProjectId))
.collect(Collectors.toList());
}
@GET
- @Path("/{projectId}/{env}")
+ @Path("/{projectId}")
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
- public List<PodContainerStatus> getContainerStatusesByProjectAndEnv(@PathParam("projectId") String projectId, @PathParam("env") String env) throws Exception {
- return karavanCache.getPodContainerStatuses(projectId, env).stream()
+ public List<PodContainerStatus> getContainerStatusesByProjectAndEnv(@PathParam("projectId") String projectId) throws Exception {
+ return karavanCache.getPodContainerStatusesByProject(projectId).stream()
.sorted(Comparator.comparing(PodContainerStatus::getContainerName))
.collect(Collectors.toList());
}
@@ -219,12 +190,11 @@ public class ContainerResource {
}
}
- // TODO: implement log watch
@GET
- @Path("/log/watch/{env}/{name}")
+ @Path("/log/watch/{name}")
@Authenticated
@Produces(MediaType.SERVER_SENT_EVENTS)
- public Multi<String> getContainerLogWatch(@PathParam("env") String env, @PathParam("name") String name) {
+ public Multi<String> getContainerLogWatch(@PathParam("name") String name) {
LOGGER.info("Start sourcing");
return eventBus.<String>consumer(name + "-" + kubernetesService.getNamespace()).toMulti().map(Message::body);
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/CookieSessionAuthMechanism.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/CookieSessionAuthMechanism.java
index f0c1ce82..efc8f654 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/CookieSessionAuthMechanism.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/CookieSessionAuthMechanism.java
@@ -26,7 +26,7 @@ import java.util.Set;
public class CookieSessionAuthMechanism implements HttpAuthenticationMechanism {
private static final Logger LOGGER = Logger.getLogger(CookieSessionAuthMechanism.class.getName());
-
+ private static final String SESSION_ID = "sessionId";
@ConfigProperty(name = "platform.auth", defaultValue = "session")
String authStrategy;
@@ -38,12 +38,11 @@ public class CookieSessionAuthMechanism implements HttpAuthenticationMechanism {
if (!"session".equals(authStrategy)) {
return Uni.createFrom().nullItem();
}
-
var builder = QuarkusSecurityIdentity.builder();
try {
builder.setPrincipal(() -> "anonymous");
builder.setAnonymous(true);
- var cookie = ctx.request().getCookie("taskId");
+ var cookie = ctx.request().getCookie(SESSION_ID);
if (cookie == null) {
return Uni.createFrom().item(builder.build());
}
@@ -54,14 +53,15 @@ public class CookieSessionAuthMechanism implements HttpAuthenticationMechanism {
}
var user = karavanCache.getUser(session.getUsername());
- if (user == null) {
+ if (user == null && karavanCache.getProject(session.getUsername().replace("-builder", "")) == null) {
return Uni.createFrom().item(builder.build());
+ } else if (user != null && user.getRoles() != null) {
+ for (String role : user.getRoles()) {
+ builder.addRole(role);
+ }
}
builder.setPrincipal(session::getUsername);
- for (String role : user.getRoles()) {
- builder.addRole(role);
- }
builder.addAttribute("csrf", session.getCsrfToken());
builder.setAnonymous(false);
return Uni.createFrom().item(builder.build());
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 c444b8eb..498c70a2 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
@@ -66,7 +66,6 @@ public class DevModeResource {
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
- e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build();
}
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/InfrastructureResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/InfrastructureResource.java
index abcb4f67..409e2221 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/InfrastructureResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/InfrastructureResource.java
@@ -115,7 +115,9 @@ public class InfrastructureResource {
@Produces(MediaType.APPLICATION_JSON)
@Path("/pod-events/{containerName}")
public List<PodEvent> getPodEvents(@PathParam("containerName") String containerName) {
- return kubernetesService.getPodEvents(containerName);
+ return kubernetesService.getPodEvents(containerName).stream()
+ .sorted(Comparator.comparing(PodEvent::lastTimestamp).reversed())
+ .collect(Collectors.toList());
}
@GET
@@ -159,7 +161,7 @@ public class InfrastructureResource {
if (ConfigService.inKubernetes()) {
return Response.ok(kubernetesService.getServices(kubernetesService.getNamespace())).build();
} else {
- List<String> list = karavanCache.getPodContainerStatuses(environment).stream()
+ List<String> list = karavanCache.getAllPodContainerStatuses().stream()
.filter(ci -> ci.getPorts() != null && !ci.getPorts().isEmpty())
.map(ci -> ci.getPorts().stream().map(i -> ci.getContainerName() + "|" + ci.getContainerName() + ":" + i.getPrivatePort()).collect(Collectors.toList()))
.flatMap(List::stream).collect(Collectors.toList());
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/InternalResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/InternalResource.java
new file mode 100644
index 00000000..cf814f4f
--- /dev/null
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/InternalResource.java
@@ -0,0 +1,179 @@
+package org.apache.camel.karavan.api;
+
+import jakarta.annotation.security.PermitAll;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.*;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.StreamingOutput;
+import org.apache.camel.karavan.cache.KaravanCache;
+import org.apache.camel.karavan.cache.ProjectFolder;
+import org.apache.camel.karavan.service.AuthService;
+import org.apache.camel.karavan.service.CodeService;
+import org.jboss.logging.Logger;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+@Path("/platform/internal/")
+public class InternalResource {
+
+ private static final Logger LOGGER = Logger.getLogger(InternalResource.class.getName());
+
+ private static final Map<String,String> CAMEL_DEBUG_PROPERTIES = Map.of(
+ "camel.trace.enabled", "true",
+ "camel.trace.standby", "false",
+ "camel.debug.enabled", "true",
+ "camel.debug.standby", "false",
+ "camel.debug.singleStepIncludeStartEnd", "true",
+ "camel.debug.breakpoints", "_all_routes_"
+ );
+
+ @Inject
+ KaravanCache karavanCache;
+
+ @Inject
+ AuthService authService;
+
+ @Inject
+ CodeService codeService;
+
+ @GET
+ @PermitAll
+ @Path("/sources/{projectId}")
+ @Produces("application/zip")
+ public Response getProjectFiles(@PathParam("projectId") String projectId, @CookieParam("sessionId") String sessionId) {
+ try {
+ LOGGER.info("Getting files for project " + projectId);
+ authService.validateSession(sessionId);
+ return getProjectFilesZipResponse(projectId, null);
+ } catch (Exception e) {
+ LOGGER.error("Error retrieving files for project: " + projectId, e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(e.getMessage())
+ .build();
+ } finally {
+ authService.invalidateSession(sessionId);
+ }
+ }
+
+ @GET
+ @PermitAll
+ @Path("/sources/{projectId}/{filename}")
+ @Produces("application/zip")
+ public Response getProjectFile(@PathParam("projectId") String projectId, @PathParam("filename") String filename, @CookieParam("sessionId") String sessionId) {
+ try {
+ LOGGER.info("Getting file " + filename + " for project " + projectId);
+ authService.validateSession(sessionId);
+ return getProjectFilesZipResponse(projectId, filename);
+
+ } catch (Exception e) {
+ LOGGER.error("Error retrieving file " + filename + " for project: " + projectId, e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(e.getMessage())
+ .build();
+ }
+ }
+
+ private Response getProjectFilesZipResponse(String projectId, String filename) {
+ LOGGER.info("Getting project files for project " + projectId);
+
+ ProjectFolder projectFolder = karavanCache.getProject(projectId);
+ if (projectFolder == null) {
+ return Response.status(Response.Status.NOT_FOUND)
+ .entity("Project with given Id does not exist")
+ .build();
+ }
+
+ Map<String, String> projectFiles = new HashMap<>();
+
+ if (filename != null) {
+ var file = karavanCache.getProjectFile(projectId, filename);
+ if (file == null) {
+ return Response.status(Response.Status.NOT_FOUND)
+ .entity("File " + filename + " does not exist in project " + projectId)
+ .build();
+ }
+ projectFiles.put(file.getName(), file.getCode());
+ } else {
+ projectFiles.putAll(codeService.getProjectFilesForDevMode(projectId));
+ }
+
+ // Map<String, String> files = enableDebug(projectFiles, List.of()); // TODO: implement breakpoint
+ return streamZipResponse(projectId, projectFiles);
+ }
+
+ private Response streamZipResponse(String downloadName, Map<String, String> files) {
+ StreamingOutput stream = os -> {
+ try (ZipOutputStream zos = new ZipOutputStream(os)) {
+ for (Map.Entry<String, String> entry : files.entrySet()) {
+ ZipEntry zipEntry = new ZipEntry(entry.getKey());
+ zos.putNextEntry(zipEntry);
+
+ byte[] content = entry.getValue().getBytes(StandardCharsets.UTF_8);
+ zos.write(content);
+ zos.closeEntry();
+ }
+ }
+ };
+
+ // Format download name to ensure it safely ends with .zip
+ String safeDownloadName = downloadName.replace("/", "_");
+ return Response.ok(stream)
+ .header("Content-Disposition", "attachment; filename=\"" + safeDownloadName + ".zip\"")
+ .build();
+ }
+
+ private Map<String, String> enableDebug(Map<String, String> files, List<String> breakPoints) {
+ Map<String, String> result = new HashMap<>(files.size());
+ files.forEach((name, code) -> {
+ if (CodeService.APPLICATION_PROPERTIES_FILENAME.equals(name)) {
+ result.put(name, changeApplicationProperties(code, breakPoints));
+ } else {
+ result.put(name, code);
+ }
+ });
+ return result;
+ }
+
+ private String changeApplicationProperties(String code, List<String> breakPoints) {
+ Map<String, String> result = new HashMap<>();
+
+ Map<String, String> properties = propertiesToMap(code);
+
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ String key = entry.getKey();
+ String value = CAMEL_DEBUG_PROPERTIES.getOrDefault(key, entry.getValue());
+ result.put(key, value);
+ }
+
+ // Add all keys from debug that are not in properties
+ for (Map.Entry<String, String> entry : CAMEL_DEBUG_PROPERTIES.entrySet()) {
+ result.putIfAbsent(entry.getKey(), entry.getValue());
+ }
+
+ return result.entrySet().stream()
+ .map(e -> e.getKey().concat("=").concat(e.getValue()))
+ .collect(Collectors.joining(System.lineSeparator()));
+ }
+
+ private Map<String, String> propertiesToMap(String code) {
+ Map<String, String> properties = new HashMap<>();
+ code.lines().forEach(line -> {
+ // Check for empty lines or comments to avoid IndexOutOfBoundsException
+ if (line != null && !line.trim().isEmpty() && !line.trim().startsWith("#")) {
+ var parts = line.split("=", 2);
+ if (parts.length == 2) {
+ var key = parts[0].trim();
+ var value = parts[1].trim();
+ properties.put(key, value);
+ }
+ }
+ });
+ return properties;
+ }
+}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/LabelResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/LabelResource.java
index 34fd236c..2fbfdd98 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/LabelResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/LabelResource.java
@@ -9,7 +9,6 @@ import jakarta.ws.rs.core.MediaType;
import org.apache.camel.karavan.KaravanConstants;
import org.apache.camel.karavan.cache.KaravanCache;
import org.apache.camel.karavan.service.CodeService;
-import org.jboss.logging.Logger;
import java.util.*;
@@ -18,8 +17,6 @@ import static org.apache.camel.karavan.service.CodeService.APPLICATION_PROPERTIE
@Path("/ui/labels")
public class LabelResource {
- private static final Logger LOGGER = Logger.getLogger(LabelResource.class.getName());
-
@Inject
KaravanCache karavanCache;
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/MetadataResources.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/MetadataResources.java
index 15d1741b..e563fdfc 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/MetadataResources.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/MetadataResources.java
@@ -39,34 +39,16 @@ public class MetadataResources {
@Inject
KaravanCache karavanCache;
- @GET
- @Produces(MediaType.TEXT_PLAIN)
- @Path("/kamelets")
- @Authenticated
- public String getKamelets() {
- StringBuilder kamelets = new StringBuilder(codeService.getResourceFile("/metadata/kamelets.yaml"));
-// List<ProjectFile> custom = karavanCache.getProjectFiles(Project.Type.kamelets.name());
-// if (!custom.isEmpty()) {
-// kamelets.append("\n---\n");
-// kamelets.append(custom.stream()
-// .map(ProjectFile::getCode)
-// .collect(Collectors.joining("\n---\n")));
-// }
- return kamelets.toString();
- }
-
@GET
@Authenticated
@Produces(MediaType.TEXT_PLAIN)
@Path("/kamelets/{projectId}")
public String getKameletsForProject(@PathParam("projectId") String projectId) {
-// StringBuilder kamelets = new StringBuilder(getKamelets());
StringBuilder kamelets = new StringBuilder();
List<ProjectFile> projectKamelets = karavanCache.getProjectFiles(projectId).stream()
.filter(f -> f.getName().endsWith(".kamelet.yaml")).toList();
if (!projectKamelets.isEmpty()) {
-// kamelets.append("\n---\n");
kamelets.append(projectKamelets.stream()
.map(ProjectFile::getCode)
.collect(Collectors.joining("\n---\n")));
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
index 329036c9..285556cc 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
@@ -88,6 +88,7 @@ public class ProjectResource extends AbstractApiResource {
}
@POST
+ @Authenticated
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response create(ProjectFolder projectFolder) {
@@ -106,20 +107,24 @@ public class ProjectResource extends AbstractApiResource {
String projectId = URLDecoder.decode(project, StandardCharsets.UTF_8);
if (deleteContainers) {
LOGGER.info("Deleting containers");
- Response res1 = devModeResource.deleteDevMode(projectId, true);
- Response res2 = containerResource.deleteContainer(projectId, ContainerType.devmode.name(), projectId);
- Response res3 = containerResource.deleteContainer(projectId, ContainerType.packaged.name(), projectId);
+ devModeResource.deleteDevMode(projectId, true);
+ containerResource.deleteContainer(projectId, ContainerType.devmode.name(), projectId);
+ containerResource.deleteContainer(projectId, ContainerType.packaged.name(), projectId);
LOGGER.info("Deleting deployments");
Response res4 = infrastructureResource.deleteDeployment(null, projectId);
}
var identity = getIdentity();
// delete from cache
karavanCache.getProjectFiles(projectId).forEach(file -> karavanCache.deleteProjectFile(projectId, file.getName()));
- karavanCache.getProjectFilesCommited(projectId).forEach(file -> karavanCache.deleteProjectFileCommited(projectId, file.getName()));
karavanCache.deleteProject(projectId);
- karavanCache.deleteProjectCommited(projectId);
- // delete from git
- gitService.deleteProject(projectId, identity.getString("username"), identity.getString("email"));
+
+ var commitedProject = karavanCache.getProjectCommited(projectId);
+ if (commitedProject != null) {
+ karavanCache.getProjectFilesCommited(projectId).forEach(file -> karavanCache.deleteProjectFileCommited(projectId, file.getName()));
+ karavanCache.deleteProjectCommited(projectId);
+ // delete from git
+ gitService.deleteProject(projectId, identity.getString("username"), identity.getString("email"));
+ }
LOGGER.info("Project deleted");
}
@@ -156,9 +161,9 @@ public class ProjectResource extends AbstractApiResource {
@GET
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
- @Path("/status/camel/{projectId}/{env}")
- public Response getCamelStatusForProjectAndEnv(@PathParam("projectId") String projectId, @PathParam("env") String env) {
- List<CamelStatus> statuses = karavanCache.getCamelStatusesByProjectAndEnv(projectId, env)
+ @Path("/status/camel/{projectId}")
+ public Response getCamelStatusForProject(@PathParam("projectId") String projectId) {
+ List<CamelStatus> statuses = karavanCache.getCamelStatusesByProject(projectId)
.stream().filter(Objects::nonNull).peek(camelStatus -> {
var stats = List.copyOf(camelStatus.getStatuses()).stream().filter(s -> !Objects.equals(s.getName(), CamelStatusValue.Name.trace)).toList();
camelStatus.setStatuses(stats);
@@ -173,9 +178,9 @@ public class ProjectResource extends AbstractApiResource {
@GET
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
- @Path("/traces/{projectId}/{env}")
- public Response getCamelTracesForProjectAndEnv(@PathParam("projectId") String projectId, @PathParam("env") String env) {
- List<CamelStatus> statuses = karavanCache.getCamelStatusesByProjectAndEnv(projectId, env)
+ @Path("/traces/{projectId}")
+ public Response getCamelTracesForProjectAndEnv(@PathParam("projectId") String projectId) {
+ List<CamelStatus> statuses = karavanCache.getCamelStatusesByProject(projectId)
.stream().peek(camelStatus -> {
var stats = List.copyOf(camelStatus.getStatuses()).stream().filter(s -> Objects.equals(s.getName(), CamelStatusValue.Name.trace)).toList();
camelStatus.setStatuses(stats);
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/api/TokenAuthenticationMechanism.java b/karavan-app/src/main/java/org/apache/camel/karavan/api/TokenAuthenticationMechanism.java
new file mode 100644
index 00000000..2e5c39b5
--- /dev/null
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/api/TokenAuthenticationMechanism.java
@@ -0,0 +1,106 @@
+package org.apache.camel.karavan.api;
+
+import io.quarkus.security.identity.IdentityProviderManager;
+import io.quarkus.security.identity.SecurityIdentity;
+import io.quarkus.security.identity.request.AuthenticationRequest;
+import io.quarkus.security.runtime.QuarkusSecurityIdentity;
+import io.quarkus.vertx.http.runtime.security.ChallengeData;
+import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism;
+import io.smallrye.mutiny.Uni;
+import io.vertx.ext.web.RoutingContext;
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.apache.camel.karavan.cache.AccessToken;
+import org.apache.camel.karavan.cache.KaravanCache;
+import org.jboss.logging.Logger;
+
+import java.util.Set;
+
+import static org.apache.camel.karavan.service.AuthService.ROLE_SERVICE_ACCOUNT;
+
+/**
+ * Dedicated Authentication Mechanism for external clients.
+ * Runs secondary to CookieSessionAuthMechanism (Priority 1).
+ */
+@ApplicationScoped
+@Priority(2)
+public class TokenAuthenticationMechanism implements HttpAuthenticationMechanism {
+
+ private static final Logger LOGGER = Logger.getLogger(TokenAuthenticationMechanism.class.getName());
+ private static final String AUTHORIZATION_HEADER = "Authorization";
+ private static final String BEARER_PREFIX = "Bearer ";
+ private static final String API_KEY_HEADER = "X-API-Key";
+
+ @Inject
+ KaravanCache karavanCache;
+
+ @Override
+ public Uni<SecurityIdentity> authenticate(RoutingContext ctx, IdentityProviderManager idpm) {
+ String token = extractToken(ctx);
+
+ // If no token is provided, pass control to the next mechanism (e.g., OIDC)
+ if (token == null || token.isBlank()) {
+ return Uni.createFrom().nullItem();
+ }
+
+ try {
+ // 1. Hash the raw incoming token instantly to prevent database/cache exposure
+ String hashedToken = org.apache.commons.codec.digest.DigestUtils.sha256Hex(token);
+
+ // 2. Query the cache for the metadata object instead of a raw String
+ AccessToken metadata = karavanCache.getToken(hashedToken);
+
+ // 3. Fallback to anonymous if token doesn't exist or is expired
+ if (metadata == null || metadata.isExpired()) {
+ LOGGER.warn("SECURITY: Invalid, revoked, or expired API Token attempted.");
+ return Uni.createFrom().item(QuarkusSecurityIdentity.builder().setAnonymous(true).build());
+ }
+
+ // 4. Build the identity and pass the scopes down the request chain
+ QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder()
+ .setPrincipal(metadata::ownerName)
+ .addRole(ROLE_SERVICE_ACCOUNT)
+ .addAttribute("allowedProjectIds", metadata.allowedProjectIds())
+ .addAttribute("clientName", metadata.clientName())
+ .setAnonymous(false);
+
+ LOGGER.infof("Trace: Authenticated MCP agent [%s] owned by [%s]", metadata.clientName(), metadata.ownerName());
+ return Uni.createFrom().item(builder.build());
+
+ } catch (Exception e) {
+ LOGGER.error("Error while validating API token: " + e.getMessage());
+ return Uni.createFrom().item(QuarkusSecurityIdentity.builder().setAnonymous(true).build());
+ }
+ }
+
+ @Override
+ public Uni<ChallengeData> getChallenge(RoutingContext ctx) {
+ // Return null to allow fallback challenge mechanisms (OIDC/Session) to execute
+ return Uni.createFrom().nullItem();
+ }
+
+ @Override
+ public Set<Class<? extends AuthenticationRequest>> getCredentialTypes() {
+ return Set.of();
+ }
+
+ /**
+ * Extracts the token from standard M2M headers.
+ */
+ private String extractToken(RoutingContext ctx) {
+ // 1. Prefer Authorization: Bearer
+ String authHeader = ctx.request().getHeader(AUTHORIZATION_HEADER);
+ if (authHeader != null && authHeader.startsWith(BEARER_PREFIX)) {
+ return authHeader.substring(BEARER_PREFIX.length()).trim();
+ }
+
+ // 2. Fallback to X-API-Key
+ String apiKeyHeader = ctx.request().getHeader(API_KEY_HEADER);
+ if (apiKeyHeader != null && !apiKeyHeader.isBlank()) {
+ return apiKeyHeader.trim();
+ }
+
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/AccessToken.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/AccessToken.java
new file mode 100644
index 00000000..4514b40f
--- /dev/null
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/AccessToken.java
@@ -0,0 +1,35 @@
+package org.apache.camel.karavan.cache;
+
+import java.time.Instant;
+import java.util.Set;
+
+public record AccessToken(
+ String hashedToken,
+ String ownerName, // The username of the developer who generated the token
+ String clientName,
+ Set<String> allowedProjectIds, // Scoped access limits (ABAC). Supports exact matches or ["*"]
+ Instant createdAt,
+ Instant expiresAt
+) {
+
+ /**
+ * Checks if the token has surpassed its expiration timestamp.
+ */
+ public boolean isExpired() {
+ return expiresAt != null && Instant.now().isAfter(expiresAt);
+ }
+
+ /**
+ * Checks whether the external agent has authorization to access a specific project.
+ * Supports a wildcard "*" for unrestricted developer-level access.
+ */
+ public boolean hasProjectAccess(String projectId) {
+ if (isExpired()) {
+ return false;
+ }
+ if (allowedProjectIds == null || allowedProjectIds.isEmpty()) {
+ return false;
+ }
+ return allowedProjectIds.contains("*") || allowedProjectIds.contains(projectId);
+ }
+}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/CamelStatusValue.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/CamelStatusValue.java
index 070a286f..b4869bda 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/cache/CamelStatusValue.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/CamelStatusValue.java
@@ -31,7 +31,9 @@ public class CamelStatusValue {
debug,
thread,
processor,
- consumer
+ consumer,
+ producer,
+ platform_http
}
Name name;
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/KaravanCache.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/KaravanCache.java
index aa3cbffd..2158c54a 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/cache/KaravanCache.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/KaravanCache.java
@@ -22,7 +22,6 @@ import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.apache.camel.karavan.model.ActivityUser;
import org.apache.camel.karavan.service.AuthService;
-import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.*;
@@ -44,8 +43,6 @@ public class KaravanCache {
@Inject
EventBus eventBus;
- static final Logger LOGGER = Logger.getLogger(KaravanCache.class.getName());
-
final Map<String, ProjectFolder> folders = new ConcurrentHashMap<>();
final Map<String, ProjectFolderCommited> foldersCommited = new ConcurrentHashMap<>();
final Map<String, ProjectFile> files = new ConcurrentHashMap<>();
@@ -60,6 +57,8 @@ public class KaravanCache {
final Map<String, AccessPassword> passwords = new ConcurrentHashMap<>();
final Map<String, AccessRole> roles = new ConcurrentHashMap<>();
final Map<String, AccessSession> sessions = new ConcurrentHashMap<>();
+ // The Key is the SHA-256 Hashed Token. The Value is the Metadata.
+ final Map<String, AccessToken> tokens = new ConcurrentHashMap<>();
final Map<String, Map<String, Instant>> projectActivities = new ConcurrentHashMap<>();
final Map<String, ActivityUser> usersWorking = new ConcurrentHashMap<>();
@@ -256,9 +255,9 @@ public class KaravanCache {
// --- Pod / Container Status ---
- public List<PodContainerStatus> getPodContainerStatuses(String projectId, String env) {
+ public List<PodContainerStatus> getPodContainerStatusesByProject(String projectId) {
return query(podContainerStatuses,
- s -> Objects.equals(s.getProjectId(), projectId) && Objects.equals(s.getEnv(), env),
+ s -> Objects.equals(s.getProjectId(), projectId),
PodContainerStatus::copy);
}
@@ -272,7 +271,7 @@ public class KaravanCache {
return query(podContainerStatuses, s -> s.getType() == ContainerType.devmode, PodContainerStatus::copy);
}
- public List<PodContainerStatus> getPodContainerStatuses() {
+ public List<PodContainerStatus> getPodContainerStatusesByProject() {
return new ArrayList<>(podContainerStatuses.values().stream().toList());
}
@@ -281,7 +280,7 @@ public class KaravanCache {
}
public PodContainerStatus getPodContainerStatus(String containerName, String env) {
- return getPodContainerStatuses(env).stream().filter(el -> Objects.equals(el.getContainerName(), containerName)).findFirst().orElse(null);
+ return getPodContainerStatusesByProject(env).stream().filter(el -> Objects.equals(el.getContainerName(), containerName)).findFirst().orElse(null);
}
public PodContainerStatus getPodContainerStatus(String key) {
@@ -297,10 +296,8 @@ public class KaravanCache {
PodContainerStatus::copy);
}
- public List<PodContainerStatus> getPodContainerStatuses(String env) {
- return query(podContainerStatuses,
- status -> Objects.equals(status.getEnv(), env),
- PodContainerStatus::copy);
+ public List<PodContainerStatus> getAllPodContainerStatuses() {
+ return query(podContainerStatuses, unused -> true, PodContainerStatus::copy);
}
public List<PodContainerStatus> getAllContainerStatuses() {
@@ -321,10 +318,8 @@ public class KaravanCache {
// --- Camel Status ---
- public List<CamelStatus> getCamelStatusesByProjectAndEnv(String projectId, String env) {
- return query(camelStatuses,
- s -> Objects.equals(s.getProjectId(), projectId) && Objects.equals(s.getEnv(), env),
- CamelStatus::copy);
+ public List<CamelStatus> getCamelStatusesByProject(String projectId) {
+ return query(camelStatuses, s -> Objects.equals(s.getProjectId(), projectId), CamelStatus::copy);
}
public List<CamelStatus> getCamelStatusesByName(CamelStatusValue.Name name) {
@@ -389,6 +384,9 @@ public class KaravanCache {
public List<AccessRole> getRoles() {
return new ArrayList<>(roles.values().stream().toList());
}
+ public List<AccessToken> getTokens() {
+ return new ArrayList<>(tokens.values().stream().toList());
+ }
public void saveUser(AccessUser user, boolean persist) {
var key = GroupedKey.create(user.getClass().getSimpleName(), DEV, user.username);
@@ -456,10 +454,40 @@ public class KaravanCache {
eventBus.send(PERSIST_SESSION, new CacheEvent(sessionId, DELETE, null));
}
- public void clearAllStatuses() {
- deploymentStatuses.clear();
- podContainerStatuses.clear();
- camelStatuses.clear();
+ public void deleteAccessSessionByUsername(String username) {
+ var entry = sessions.entrySet().stream().filter(e -> e.getValue().username.equals(username)).findFirst().orElse(null);
+ if (entry != null) {
+ var sessionId = entry.getKey();
+ deleteUserHeartBeat(username);
+ deleteUserWorking(username);
+ sessions.remove(sessionId);
+ eventBus.send(PERSIST_SESSION, new CacheEvent(sessionId, DELETE, null));
+ }
+ }
+
+ // API Tokens
+ public void saveToken(AccessToken metadata, boolean persist) {
+ var key = GroupedKey.create(AccessToken.class.getSimpleName(), DEV, metadata.hashedToken());
+ tokens.put(key, metadata);
+ if (persist) {
+ eventBus.send(PERSIST_ACCESS, new CacheEvent(key, SAVE, metadata));
+ }
+ }
+
+ public AccessToken getToken(String hashedToken) {
+ var key = GroupedKey.create(AccessToken.class.getSimpleName(), DEV, hashedToken);
+ return tokens.get(key);
+ }
+
+ public void deleteToken(String hashedTokenPartial) {
+ if (hashedTokenPartial.length() == 16) {
+ var partialKey = GroupedKey.create(AccessToken.class.getSimpleName(), DEV, hashedTokenPartial);
+ var key = tokens.keySet().stream().filter(k -> k.startsWith(partialKey)).findFirst().orElse(null);
+ if (key != null) {
+ tokens.remove(key);
+ eventBus.send(PERSIST_SESSION, new CacheEvent(key, DELETE, null));
+ }
+ }
}
// Activity
@@ -511,4 +539,10 @@ public class KaravanCache {
projectActivities.put(projectId, acts);
});
}
+
+ public void clearAllStatuses() {
+ deploymentStatuses.clear();
+ podContainerStatuses.clear();
+ camelStatuses.clear();
+ }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/PodContainerStatus.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/PodContainerStatus.java
index d42d0cd9..05e6cb86 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/cache/PodContainerStatus.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/PodContainerStatus.java
@@ -53,6 +53,7 @@ public class PodContainerStatus {
String finished;
List<Command> commands;
String state;
+ String stateMessage;
String phase;
Boolean codeLoaded;
Boolean inTransit = false;
@@ -62,7 +63,7 @@ public class PodContainerStatus {
String commit;
Map<String, String> labels;
- public PodContainerStatus(String projectId, String containerName, String containerId, String image, List<ContainerPort> ports, String env, ContainerType type, String memoryInfo, String cpuInfo, String created, String finished, List<Command> commands, String state, String phase, Boolean codeLoaded, Boolean inTransit, String initDate, String podIP, String camelRuntime, String commit, Map<String, String> labels) {
+ public PodContainerStatus(String projectId, String containerName, String containerId, String image, List<ContainerPort> ports, String env, ContainerType type, String memoryInfo, String cpuInfo, String created, String finished, List<Command> commands, String state, String stateMessage, String phase, Boolean codeLoaded, Boolean inTransit, String initDate, String podIP, String camelRuntime, String commit, Map<String, String> labels) {
this.projectId = projectId;
this.containerName = containerName;
this.containerId = containerId;
@@ -76,6 +77,7 @@ public class PodContainerStatus {
this.finished = finished;
this.commands = commands;
this.state = state;
+ this.stateMessage = stateMessage;
this.phase = phase;
this.codeLoaded = codeLoaded;
this.inTransit = inTransit;
@@ -194,7 +196,7 @@ public class PodContainerStatus {
public PodContainerStatus copy() {
return new PodContainerStatus(
- projectId, containerName, containerId, image, ports, env, type, memoryInfo, cpuInfo, created, finished, commands, state, phase, codeLoaded, inTransit, initDate, podIP, camelRuntime, commit, labels
+ projectId, containerName, containerId, image, ports, env, type, memoryInfo, cpuInfo, created, finished, commands, state, stateMessage, phase, codeLoaded, inTransit, initDate, podIP, camelRuntime, commit, labels
);
}
@@ -302,6 +304,14 @@ public class PodContainerStatus {
this.state = state;
}
+ public String getStateMessage() {
+ return stateMessage;
+ }
+
+ public void setStateMessage(String stateMessage) {
+ this.stateMessage = stateMessage;
+ }
+
public Boolean getCodeLoaded() {
return codeLoaded;
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFile.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFile.java
index 2f98fa20..0f70290c 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFile.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFile.java
@@ -17,6 +17,8 @@
package org.apache.camel.karavan.cache;
+import java.time.Instant;
+
public class ProjectFile {
String name;
@@ -30,6 +32,9 @@ public class ProjectFile {
this.projectId = projectId;
this.lastUpdate = lastUpdate;
}
+ public ProjectFile(String name, String code, String projectId) {
+ this(name, code, projectId, Instant.now().getEpochSecond() * 1000);
+ }
public ProjectFile() {
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFolder.java b/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFolder.java
index 50a63e95..9d7839df 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFolder.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/cache/ProjectFolder.java
@@ -29,9 +29,10 @@ public class ProjectFolder {
configuration,
documentation,
contracts,
+ mcp,
+ sdx,
services,
- integration,
- backlog,
+ integration
}
String projectId;
@@ -107,7 +108,8 @@ public class ProjectFolder {
Type.templates.name(),
Type.contracts.name(),
Type.documentation.name(),
- Type.backlog.name()
+ Type.sdx.name(),
+ Type.mcp.name()
);
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/Complexity.java b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/Complexity.java
deleted file mode 100644
index e708b9fc..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/Complexity.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package org.apache.camel.karavan.complexity;
-
-public enum Complexity {
- easy, normal, complex
-}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityComponent.java b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityComponent.java
index 4ee80e43..d0ffcff0 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityComponent.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityComponent.java
@@ -1,5 +1,7 @@
package org.apache.camel.karavan.complexity;
+import io.vertx.core.json.JsonObject;
+
import java.util.HashMap;
import java.util.Map;
@@ -7,14 +9,16 @@ public class ComplexityComponent {
private String id;
private String name;
+ private boolean remote;
private Map<String, String > parameters = new HashMap<>();
public ComplexityComponent() {
}
- public ComplexityComponent(String id, String name, Map<String, String> parameters) {
+ public ComplexityComponent(String id, String name, boolean remote, Map<String, String> parameters) {
this.id = id;
this.name = name;
+ this.remote = remote;
this.parameters = parameters;
}
@@ -41,4 +45,17 @@ public class ComplexityComponent {
public void setId(String id) {
this.id = id;
}
+
+ public boolean isRemote() {
+ return remote;
+ }
+
+ public void setRemote(boolean remote) {
+ this.remote = remote;
+ }
+
+ public ComplexityComponent copy() {
+ var json = JsonObject.mapFrom(this).encode();
+ return new JsonObject(json).mapTo(ComplexityComponent.class);
+ }
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityFile.java b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityFile.java
index 9d004982..c393ec1b 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityFile.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityFile.java
@@ -15,15 +15,6 @@ public class ComplexityFile {
private Integer beans = 0;
private Integer rests = 0;
private boolean isGenerated = false;
- private Complexity complexity = Complexity.easy;
- private Complexity complexityLines = Complexity.easy;
- private Complexity complexityRoutes = Complexity.easy;
- private Complexity complexityRests = Complexity.easy;
- private Complexity complexityBeans = Complexity.easy;
- private Complexity complexityProcessors = Complexity.easy;
- private Complexity complexityComponentsInt = Complexity.easy;
- private Complexity complexityComponentsExt = Complexity.easy;
- private Complexity complexityKamelets = Complexity.easy;
private Map<String, Integer> processors = new HashMap<>();
private Map<String, Integer> componentsInt = new HashMap<>();
private Map<String, Integer> componentsExt = new HashMap<>();
@@ -88,78 +79,6 @@ public class ComplexityFile {
this.rests = rests;
}
- public Complexity getComplexity() {
- return complexity;
- }
-
- public void setComplexity(Complexity complexity) {
- this.complexity = complexity;
- }
-
- public Complexity getComplexityLines() {
- return complexityLines;
- }
-
- public void setComplexityLines(Complexity complexityLines) {
- this.complexityLines = complexityLines;
- }
-
- public Complexity getComplexityRoutes() {
- return complexityRoutes;
- }
-
- public void setComplexityRoutes(Complexity complexityRoutes) {
- this.complexityRoutes = complexityRoutes;
- }
-
- public Complexity getComplexityRests() {
- return complexityRests;
- }
-
- public void setComplexityRests(Complexity complexityRests) {
- this.complexityRests = complexityRests;
- }
-
- public Complexity getComplexityBeans() {
- return complexityBeans;
- }
-
- public void setComplexityBeans(Complexity complexityBeans) {
- this.complexityBeans = complexityBeans;
- }
-
- public Complexity getComplexityProcessors() {
- return complexityProcessors;
- }
-
- public void setComplexityProcessors(Complexity complexityProcessors) {
- this.complexityProcessors = complexityProcessors;
- }
-
- public Complexity getComplexityComponentsInt() {
- return complexityComponentsInt;
- }
-
- public void setComplexityComponentsInt(Complexity complexityComponentsInt) {
- this.complexityComponentsInt = complexityComponentsInt;
- }
-
- public Complexity getComplexityComponentsExt() {
- return complexityComponentsExt;
- }
-
- public void setComplexityComponentsExt(Complexity complexityComponentsExt) {
- this.complexityComponentsExt = complexityComponentsExt;
- }
-
- public Complexity getComplexityKamelets() {
- return complexityKamelets;
- }
-
- public void setComplexityKamelets(Complexity complexityKamelets) {
- this.complexityKamelets = complexityKamelets;
- }
-
public Map<String, Integer> getProcessors() {
return processors;
}
@@ -224,15 +143,6 @@ public class ComplexityFile {
", routes=" + routes +
", beans=" + beans +
", rests=" + rests +
- ", complexity=" + complexity +
- ", complexityLines=" + complexityLines +
- ", complexityRoutes=" + complexityRoutes +
- ", complexityRests=" + complexityRests +
- ", complexityBeans=" + complexityBeans +
- ", complexityProcessors=" + complexityProcessors +
- ", complexityComponentsInt=" + complexityComponentsInt +
- ", complexityComponentsExt=" + complexityComponentsExt +
- ", complexityKamelets=" + complexityKamelets +
", processors=" + processors +
", componentsInt=" + componentsInt +
", componentsExt=" + componentsExt +
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityProject.java b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityProject.java
index 7cab8948..e9b13109 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityProject.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityProject.java
@@ -8,17 +8,14 @@ public class ComplexityProject {
private String projectId;
private String type;
private Long lastUpdateDate = 0L;
- private Complexity complexityRoute = Complexity.easy;
- private Complexity complexityRest = Complexity.easy;
- private Complexity complexityJava = Complexity.easy;
- private Complexity complexityFiles = Complexity.easy;
private List<ComplexityFile> files = new ArrayList<>();
private List<ComplexityRoute> routes = new ArrayList<>();
private Integer rests = 0;
private boolean exposesOpenApi = false;
+ private boolean mcp = false;
+ private boolean agents = false;
private List<String> dependencies = new ArrayList<>();
-
public ComplexityProject() {
}
@@ -46,38 +43,6 @@ public class ComplexityProject {
this.lastUpdateDate = lastUpdateDate;
}
- public Complexity getComplexityRoute() {
- return complexityRoute;
- }
-
- public void setComplexityRoute(Complexity complexityRoute) {
- this.complexityRoute = complexityRoute;
- }
-
- public Complexity getComplexityRest() {
- return complexityRest;
- }
-
- public void setComplexityRest(Complexity complexityRest) {
- this.complexityRest = complexityRest;
- }
-
- public Complexity getComplexityJava() {
- return complexityJava;
- }
-
- public void setComplexityJava(Complexity complexityJava) {
- this.complexityJava = complexityJava;
- }
-
- public Complexity getComplexityFiles() {
- return complexityFiles;
- }
-
- public void setComplexityFiles(Complexity complexityFiles) {
- this.complexityFiles = complexityFiles;
- }
-
public List<ComplexityFile> getFiles() {
return files;
}
@@ -122,20 +87,19 @@ public class ComplexityProject {
this.exposesOpenApi = exposesOpenApi;
}
- @Override
- public String toString() {
- return "ComplexityProject{" +
- "projectId='" + projectId + '\'' +
- ", lastUpdateDate=" + lastUpdateDate +
- ", complexityRoute=" + complexityRoute +
- ", complexityRest=" + complexityRest +
- ", complexityJava=" + complexityJava +
- ", complexityFiles=" + complexityFiles +
- ", files=" + files +
- ", routes=" + routes +
- ", rests=" + rests +
- ", dependencies=" + dependencies +
- '}';
+ public boolean isMcp() {
+ return mcp;
}
+ public void setMcp(boolean mcp) {
+ this.mcp = mcp;
+ }
+
+ public boolean isAgents() {
+ return agents;
+ }
+
+ public void setAgents(boolean agents) {
+ this.agents = agents;
+ }
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityRoute.java b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityRoute.java
index bd4080f0..cc5820f8 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityRoute.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/complexity/ComplexityRoute.java
@@ -1,5 +1,7 @@
package org.apache.camel.karavan.complexity;
+import io.vertx.core.json.JsonObject;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -7,26 +9,41 @@ import java.util.Map;
public class ComplexityRoute {
+ public enum Type {ROUTE, ROUTE_TEMPlATE, TEMPLATED_ROUTE}
+
private String routeId;
private String routeDescription;
private String nodePrefixId;
private String routeTemplateRef;
- private Boolean isTemplated;
+ private Type type;
private String fileName;
- private Complexity complexityProcessors = Complexity.easy;
- private Complexity complexityComponentsInt = Complexity.easy;
- private Complexity complexityComponentsExt = Complexity.easy;
- private Complexity complexityKamelets = Complexity.easy;
private List<ComplexityComponent> consumers = new ArrayList<>();
private List<ComplexityComponent> producers = new ArrayList<>();
private Map<String, Integer> processors = new HashMap<>();
private Map<String, Integer> componentsInt = new HashMap<>();
private Map<String, Integer> componentsExt = new HashMap<>();
private Map<String, Integer> kamelets = new HashMap<>();
+ private Map<String, Object> parameters = new HashMap<>();
public ComplexityRoute() {
}
+ public ComplexityRoute(String routeId, String routeDescription, String nodePrefixId, String routeTemplateRef, Type type, String fileName, List<ComplexityComponent> consumers, List<ComplexityComponent> producers, Map<String, Integer> processors, Map<String, Integer> componentsInt, Map<String, Integer> componentsExt, Map<String, Integer> kamelets, Map<String, Object> parameters) {
+ this.routeId = routeId;
+ this.routeDescription = routeDescription;
+ this.nodePrefixId = nodePrefixId;
+ this.routeTemplateRef = routeTemplateRef;
+ this.type = type;
+ this.fileName = fileName;
+ this.consumers = consumers;
+ this.producers = producers;
+ this.processors = processors;
+ this.componentsInt = componentsInt;
+ this.componentsExt = componentsExt;
+ this.kamelets = kamelets;
+ this.parameters = parameters;
+ }
+
public String getNodePrefixId() {
return nodePrefixId;
}
@@ -99,38 +116,6 @@ public class ComplexityRoute {
kamelets.put(component, kamelets.getOrDefault(component, 0) + 1);
}
- public Complexity getComplexityProcessors() {
- return complexityProcessors;
- }
-
- public void setComplexityProcessors(Complexity complexityProcessors) {
- this.complexityProcessors = complexityProcessors;
- }
-
- public Complexity getComplexityComponentsInt() {
- return complexityComponentsInt;
- }
-
- public void setComplexityComponentsInt(Complexity complexityComponentsInt) {
- this.complexityComponentsInt = complexityComponentsInt;
- }
-
- public Complexity getComplexityComponentsExt() {
- return complexityComponentsExt;
- }
-
- public void setComplexityComponentsExt(Complexity complexityComponentsExt) {
- this.complexityComponentsExt = complexityComponentsExt;
- }
-
- public Complexity getComplexityKamelets() {
- return complexityKamelets;
- }
-
- public void setComplexityKamelets(Complexity complexityKamelets) {
- this.complexityKamelets = complexityKamelets;
- }
-
public List<ComplexityComponent> getConsumers() {
return consumers;
}
@@ -162,12 +147,12 @@ public class ComplexityRoute {
this.routeTemplateRef = routeTemplateRef;
}
- public Boolean getTemplated() {
- return isTemplated;
+ public Type getType() {
+ return type;
}
- public void setTemplated(Boolean templated) {
- isTemplated = templated;
+ public void setType(Type type) {
+ this.type = type;
}
public String getRouteDescription() {
@@ -177,4 +162,17 @@ public class ComplexityRoute {
public void setRouteDescription(String routeDescription) {
this.routeDescription = routeDescription;
}
+
+ public Map<String, Object> getParameters() {
+ return parameters;
+ }
+
+ public void setParameters(Map<String, Object> parameters) {
+ this.parameters = parameters;
+ }
+
+ public ComplexityRoute copy() {
+ var json = JsonObject.mapFrom(this).encode();
+ return new JsonObject(json).mapTo(ComplexityRoute.class);
+ }
}
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 f71046c2..d18f623a 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
@@ -16,12 +16,16 @@
*/
package org.apache.camel.karavan.docker;
-import com.github.dockerjava.api.model.*;
+import com.github.dockerjava.api.model.Container;
+import com.github.dockerjava.api.model.HealthCheck;
+import com.github.dockerjava.api.model.MountType;
+import com.github.dockerjava.api.model.RestartPolicy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.camel.karavan.cache.ContainerType;
import org.apache.camel.karavan.cache.ProjectFolder;
-import org.apache.camel.karavan.model.*;
+import org.apache.camel.karavan.model.DockerComposeService;
+import org.apache.camel.karavan.model.DockerVolumeDefinition;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
@@ -44,27 +48,20 @@ public class DockerForKaravan {
@Inject
DockerService dockerService;
- public void runProjectInDevMode(String projectId, Boolean verbose, Boolean compile, DockerStackService stack, String projectDevmodeImage, Map<String, String> labels, Map<String, String> envVars) throws Exception {
- createDevmodeService(projectId, verbose, compile, stack, projectDevmodeImage, labels, envVars);
- }
-
- 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 {
+ public void runProjectInDevMode(String projectId, Boolean verbose, Boolean compile, DockerComposeService composeService, String projectDevmodeImage, Map<String, String> labels, Map<String, String> envVars) throws Exception {
Container c = createDevmodeContainer(projectId, verbose, compile, composeService, projectDevmodeImage, labels, envVars);
- dockerService.runContainer(projectId);
- dockerService.copyFiles(c.getId(), "/karavan/code", files, true);
- dockerService.copyFiles(c.getId(), "/tmp", Map.of(".karavan.done", "done"), true);
+ dockerService.runContainer(c);
}
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);
- HealthCheck healthCheck = new HealthCheck().withTest(List.of("CMD", "curl", "-f", "http://localhost:8080/q/dev/health"))
+ HealthCheck healthCheck = new HealthCheck().withTest(List.of("CMD", "curl", "-f", "http://localhost:9876/q/observe/health"))
.withInterval(10000000000L).withTimeout(10000000000L).withStartPeriod(10000000000L).withRetries(30);
List<String> env = new ArrayList<>(compose.getEnvironmentList());
- envVars.forEach((k,v) -> env.add(k + "=" + v));
+ envVars.forEach((k, v) -> env.add(k + "=" + v));
if (verbose) {
env.add(ENV_VAR_VERBOSE_OPTION_NAME + "=" + ENV_VAR_VERBOSE_OPTION_VALUE);
}
@@ -73,7 +70,7 @@ public class DockerForKaravan {
}
if (createM2.orElse(false)) {
- compose.getVolumes().add(new DockerVolumeDefinition(MountType.VOLUME.name(), projectId+ "-m2-repository", "/karavan/.m2/repository"));
+ compose.getVolumes().add(new DockerVolumeDefinition(MountType.VOLUME.name(), projectId + "-m2-repository", "/karavan/.m2/repository"));
}
var imageName = projectDevmodeImage != null ? projectDevmodeImage : devmodeImage;
@@ -94,47 +91,6 @@ public class DockerForKaravan {
compose.getCpus(), compose.getCpu_percent(), compose.getMem_limit(), compose.getMem_reservation(), compose.getCommand());
}
- protected Service createDevmodeService(String projectId, Boolean verbose, Boolean compile, DockerStackService stack,
- String projectDevmodeImage, Map<String, String> labels, Map<String, String> envVars) throws InterruptedException {
- LOGGER.infof("DevMode starting for %s with verbose=%s", projectId, verbose);
-
- var healthCheck = new DockerHealthCheckDefinition();
- healthCheck.setInterval(String.valueOf(10000000000L));
- healthCheck.setTimeout(String.valueOf(10000000000L));
- healthCheck.setStart_period(String.valueOf(10000000000L));
- healthCheck.setRetries(30);
- healthCheck.setTest(List.of("CMD", "curl", "-f", "http://localhost:8080/q/dev/health"));
- stack.setHealthcheck(healthCheck);
-
- envVars.forEach(stack::addEnvironment);
- if (verbose) {
- stack.addEnvironment(ENV_VAR_VERBOSE_OPTION_NAME, ENV_VAR_VERBOSE_OPTION_VALUE);
- }
- if (compile) {
- stack.addEnvironment(ENV_VAR_RUN_IN_COMPILE_MODE, "true");
- }
-
- if (createM2.orElse(false)) {
- stack.getVolumes().add(new DockerVolumeDefinition(MountType.VOLUME.name(), projectId+ "-m2-repository", "/karavan/.m2/repository"));
- }
-
- var imageName = projectDevmodeImage != null ? projectDevmodeImage : devmodeImage;
- stack.setImage(imageName);
-
- var containerLabels = new HashMap<>(labels);
- if (stack.getLabels() != null) {
- containerLabels.putAll(stack.getLabels());
- }
- containerLabels.put(LABEL_TYPE, ContainerType.devmode.name());
- containerLabels.put(LABEL_PROJECT_ID, projectId);
- containerLabels.put(LABEL_CAMEL_RUNTIME, CamelRuntime.CAMEL_MAIN.getValue());
- stack.setLabels(containerLabels);
-
- var serviceSpec = StackToServiceSpecConverter.convertService(projectId, stack);
- return dockerService.createService(projectId, serviceSpec);
- }
-
-
public void runBuildProject(ProjectFolder projectFolder, String script, DockerComposeService compose, Map<String, String> sshFiles, String tag) throws Exception {
String containerName = projectFolder.getProjectId() + BUILDER_SUFFIX;
dockerService.deleteContainer(containerName);
@@ -142,35 +98,13 @@ public class DockerForKaravan {
compose.getVolumes().add(new DockerVolumeDefinition(MountType.VOLUME.name(), projectFolder.getProjectId() + "-build-m2-repository", "/karavan/.m2/repository"));
}
Container c = createBuildContainer(containerName, projectFolder, compose.getEnvironmentList(), compose.getVolumes(), tag);
- dockerService.copyExecFile(c.getId(), "/karavan/builder", BUILD_SCRIPT_FILENAME, script);
+ dockerService.copyFileToContainer(c.getId(), "/karavan/builder", BUILD_SCRIPT_FILENAME, script, 0755);
sshFiles.forEach((name, text) -> {
- dockerService.copyExecFile(c.getId(), "/karavan/.ssh", name, text);
+ dockerService.copyFileToContainer(c.getId(), "/karavan/.ssh", name, text, 0600);
});
dockerService.runContainer(c);
}
- public void runBuildProject(ProjectFolder projectFolder, DockerStackService stack, String tag) throws Exception {
- String serviceName = projectFolder.getProjectId() + BUILDER_SUFFIX;
- dockerService.deleteService(serviceName);
- if (createM2.orElse(false)) {
- stack.getVolumes().add(new DockerVolumeDefinition(MountType.VOLUME.name(), projectFolder.getProjectId() + "-build-m2-repository", "/karavan/.m2/repository"));
- }
- stack.setHostname(serviceName);
- stack.addConfig(new DockerConfigDefinition(BUILD_SCRIPT_FILENAME, "/karavan/builder" + BUILD_SCRIPT_FILENAME));
-// Map<String, String> sshFiles = codeService.getSshFiles();
-// TODO: ssh_keys for Docker in Swarm Mode should be already in Secrets
- stack.addConfig(new DockerConfigDefinition("ssh_id_rsa", "/karavan/.ssh/id_rsa"));
- stack.addConfig(new DockerConfigDefinition("ssh_known_hosts", "/karavan/.ssh/known_hosts"));
-
- stack.addLabel(LABEL_TYPE, ContainerType.build.name());
- stack.addLabel(LABEL_PROJECT_ID, projectFolder.getProjectId());
- stack.addLabel(LABEL_TAG, tag);
- stack.setImage(devmodeImage);
- stack.setCommand("/karavan/builder/build.sh");
-
- createBuildService(serviceName, stack);
- }
-
protected Container createBuildContainer(String containerName, ProjectFolder projectFolder, List<String> env, List<DockerVolumeDefinition> volumes, String tag) throws InterruptedException {
LOGGER.infof("Starting Build Container ", containerName);
@@ -185,11 +119,4 @@ public class DockerForKaravan {
null, null, null, null,
"/karavan/builder/build.sh");
}
-
- protected void createBuildService(String serviceName, DockerStackService stack) {
- LOGGER.infof("Starting Build Service ", serviceName);
-
- var spec = StackToServiceSpecConverter.convertService(serviceName, stack);
- dockerService.createService(serviceName, spec);
- }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerService.java b/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerService.java
index 0811337e..aab6f863 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerService.java
@@ -41,7 +41,8 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
-import static org.apache.camel.karavan.KaravanConstants.*;
+import static org.apache.camel.karavan.KaravanConstants.LABEL_PROJECT_ID;
+import static org.apache.camel.karavan.KaravanConstants.LABEL_TYPE;
@Default
@Readiness
@@ -90,15 +91,6 @@ public class DockerService {
private volatile DockerClient dockerClient;
private volatile DockerClient dockerClientConnectedToRegistry;
- private static Boolean IN_SWARM_MODE = null;
-
- public boolean isInSwarmMode() {
- if (IN_SWARM_MODE == null) {
- IN_SWARM_MODE = checkDockerSwarm();
- }
- return IN_SWARM_MODE;
- }
-
void onStart(@Observes StartupEvent ev) {
if (!ConfigService.inKubernetes()) {
// getDockerClient().eventsCmd().withEventFilter("pull", "create", "attach", "start", "stop", "kill").exec(dockerEventHandler);
@@ -141,33 +133,14 @@ public class DockerService {
}
}
- public boolean checkDockerSwarm() {
- try {
- Info info = getDockerClient().infoCmd().exec();
- var swarmInfo = info.getSwarm();
- var nodes = (swarmInfo != null && swarmInfo.getNodes() != null) ? swarmInfo.getNodes() : 0;
- return swarmInfo != null && nodes > 0;
- } catch (Exception e) {
- LOGGER.error("Error connecting Docker: " + e.getMessage());
- return false;
- }
- }
-
public JsonObject getInfo() {
try {
var info = getDockerClient().infoCmd().exec();
- var swarm = info.getSwarm();
- if (swarm != null) {
- return JsonObject.of(
- "Nodes", swarm.getNodes(),
- "NodeId", swarm.getNodeID(),
- "Managers", swarm.getManagers(),
- "Error", swarm.getError(),
- "MemTotal", info.getMemTotal()
- );
- }
+ return JsonObject.of(
+ "MemTotal", info.getMemTotal()
+ );
} catch (Exception e) {
- LOGGER.error(e.getMessage());
+// LOGGER.error(e.getMessage());
}
return JsonObject.of("Nodes", 0, "Error", "Swarm or Info is unavailable");
}
@@ -219,25 +192,11 @@ public class DockerService {
}
}
- public List<Container> findContainersByProjectId(String projectId) {
- return getDockerClient().listContainersCmd().withLabelFilter(Map.of(LABEL_PROJECT_ID, projectId)).exec();
- }
-
- public List<Container> findContainersByServiceId(String serviceId) {
- return getDockerClient().listContainersCmd().withLabelFilter(Map.of(LABEL_SWARM_SERVICE_ID, serviceId)).exec();
- }
-
public List<Container> findContainers(String containerName) {
- var isSwarm = isInSwarmMode();
var list = getDockerClient().listContainersCmd().withShowAll(true).withNameFilter(List.of(containerName)).exec();
return list.stream().filter(c -> {
var contName = c.getNames()[0].replace("/", "");
- if (Objects.equals(contName, containerName)) return true;
- if (isSwarm) {
- var stack = c.getLabels().get(LABEL_DOCKER_STACK_NAMESPACE);
- return contName.startsWith(stack + "_" + containerName);
- }
- return false;
+ return Objects.equals(contName, containerName);
}).toList();
}
@@ -296,7 +255,7 @@ public class DockerService {
.withDirChildrenOnly(dirChildrenOnly).withHostResource(temp).exec();
}
- public void copyExecFile(String containerId, String containerPath, String filename, String script) {
+ public void copyFileToContainer(String containerId, String containerPath, String filename, String script, int mode) {
String temp = vertx.fileSystem().createTempDirectoryBlocking(containerId);
String path = temp + File.separator + filename;
vertx.fileSystem().writeFileBlocking(path, Buffer.buffer(script));
@@ -308,7 +267,7 @@ public class DockerService {
TarArchiveEntry tarEntry = new TarArchiveEntry(new File(path));
tarEntry.setName(filename);
- tarEntry.setMode(0755);
+ tarEntry.setMode(mode);
tarArchive.putArchiveEntry(tarEntry);
IOUtils.write(Files.readAllBytes(Paths.get(path)), tarArchive);
tarArchive.closeArchiveEntry();
@@ -318,8 +277,7 @@ public class DockerService {
.withTarInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))
.withRemotePath(containerPath).exec();
} catch (Exception e) {
- LOGGER.error(e.getMessage());
- e.printStackTrace();
+ LOGGER.error("Failed to copy file to container: " + e.getMessage(), e);
}
}
@@ -481,8 +439,4 @@ public class DockerService {
getDockerClient().removeImageCmd(image.get().getId()).exec();
}
}
-
- public void createConfig(String name, String config) {
- getDockerClient().createConfigCmd().withName(name).withData(config.getBytes()).exec();
- }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerStackConverter.java b/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerStackConverter.java
deleted file mode 100644
index 30f31e07..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/docker/DockerStackConverter.java
+++ /dev/null
@@ -1,177 +0,0 @@
-package org.apache.camel.karavan.docker;
-
-import io.vertx.core.json.JsonArray;
-import io.vertx.core.json.JsonObject;
-import org.apache.camel.karavan.model.DockerStack;
-import org.apache.camel.karavan.model.DockerStackService;
-import org.apache.camel.karavan.model.DockerVolumeDefinition;
-import org.yaml.snakeyaml.DumperOptions;
-import org.yaml.snakeyaml.Yaml;
-import org.yaml.snakeyaml.introspector.Property;
-import org.yaml.snakeyaml.nodes.*;
-import org.yaml.snakeyaml.representer.Representer;
-
-import java.util.Map;
-
-import static com.github.dockerjava.api.model.MountType.BIND;
-import static com.github.dockerjava.api.model.MountType.VOLUME;
-
-/**
- * Utility class to convert docker-stack.yaml <-> DockerStack model.
- * Handles short syntax for environment, labels, and volumes.
- */
-public class DockerStackConverter {
-
- private static final String ENVIRONMENT = "environment";
- private static final String LABELS = "labels";
- private static final String VOLUMES = "volumes";
-
- public static DockerStack fromCode(String code) {
- Yaml yaml = new Yaml();
- Map<String, Object> obj = yaml.load(code);
- JsonObject json = JsonObject.mapFrom(obj);
- JsonObject services = json.getJsonObject("services");
- JsonObject stackServices = new JsonObject();
-
- if (services != null) {
- services.getMap().forEach((name, value) -> {
- JsonObject serviceJson = services.getJsonObject(name);
- DockerStackService service = convertToDockerStackService(name, serviceJson);
- stackServices.put(name, service);
- });
- }
- json.put("services", stackServices);
- return json.mapTo(DockerStack.class);
- }
-
- public static DockerStackService fromCode(String code, String serviceName) {
- DockerStack stack = fromCode(code);
- return stack.getServices().get(serviceName);
- }
-
- public static String toCode(DockerStack stack) {
- Yaml yaml = new Yaml(new StackRepresenter());
- return yaml.dumpAs(stack, Tag.MAP, DumperOptions.FlowStyle.BLOCK);
- }
-
- /**
- * Converts a single service to YAML.
- * @param service The service object
- * @param serviceName The name (key) for the service (e.g., "my-app")
- * @return A YAML string
- */
- public static String toCode(DockerStackService service, String serviceName) {
- DockerStack ds = new DockerStack();
- ds.getServices().put(serviceName, service);
- return toCode(ds);
- }
-
- private static DockerStackService convertToDockerStackService(String name, JsonObject service) {
- // This logic is identical for docker-compose and docker-stack
- if (service.containsKey(ENVIRONMENT) && service.getValue(ENVIRONMENT) instanceof JsonArray) {
- JsonObject env = new JsonObject();
- service.getJsonArray(ENVIRONMENT).forEach(o -> {
- String[] kv = o.toString().split("=", 2); // Split only on the first =
- if (kv.length == 2) {
- env.put(kv[0], kv[1]);
- }
- });
- service.put(ENVIRONMENT, env);
- }
-
- // This logic is identical for docker-compose and docker-stack
- if (service.containsKey(LABELS) && service.getValue(LABELS) instanceof JsonArray) {
- JsonObject labels = new JsonObject();
- service.getJsonArray(LABELS).forEach(o -> {
- String[] kv = o.toString().split("=", 2); // Split only on the first =
- if (kv.length == 2) {
- labels.put(kv[0], kv[1]);
- }
- });
- service.put(LABELS, labels);
- }
-
- // This logic is identical for docker-compose and docker-stack
- // It converts short syntax volumes to the long syntax (type, source, target)
- if (service.containsKey(VOLUMES) && service.getValue(VOLUMES) instanceof JsonArray) {
- JsonArray volumes = new JsonArray();
- JsonArray yamlVolumes = service.getJsonArray(VOLUMES);
- yamlVolumes.forEach(o -> {
- if (o instanceof JsonObject) {
- volumes.add(o);
- } else if (o instanceof String) {
- var parts = ((String) o).split(":");
- if (parts.length == 2) {
- var part0 = parts[0];
- // Guess type: if it looks like a path, it's a bind mount.
- var type = (part0.startsWith("/") || part0.startsWith("~") || part0.startsWith("./")) ? BIND : VOLUME;
- volumes.add(JsonObject.mapFrom(new DockerVolumeDefinition(type.name().toLowerCase(), parts[0], parts[1])));
- } else if (parts.length == 1) {
- // Anonymous volume
- volumes.add(JsonObject.mapFrom(new DockerVolumeDefinition(VOLUME.name().toLowerCase(), null, parts[0])));
- }
- }
- });
- service.put(VOLUMES, volumes);
- }
-
- // Map to the DockerStackService class
- DockerStackService ds = service.mapTo(DockerStackService.class);
-
- // **REMOVED**: container_name is not supported in docker-stack
- // if (ds.getContainer_name() == null) {
- // ds.setContainer_name(name);
- // }
-
- return ds;
- }
-
- /**
- * Custom SnakeYAML Representer to skip nulls and empty collections/maps.
- */
- private static class StackRepresenter extends Representer {
-
- public StackRepresenter() {
- super(create());
- }
-
- private static DumperOptions create() {
- DumperOptions options = new DumperOptions();
- options.setExplicitStart(true);
- options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
- options.setLineBreak(DumperOptions.LineBreak.UNIX);
- return options;
- }
-
- @Override
- protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
- if (propertyValue == null) {
- return null; // skip 'null' values
- } else {
- NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
- Node valueNode = tuple.getValueNode();
- if (Tag.NULL.equals(valueNode.getTag())) {
- return null; // skip 'null' values
- }
- if (propertyValue instanceof String && (((String) propertyValue).isEmpty() || ((String) propertyValue).isBlank()) ) {
- return null; // skip '' values
- }
- if (valueNode instanceof CollectionNode) {
- if (Tag.SEQ.equals(valueNode.getTag())) {
- SequenceNode seq = (SequenceNode) valueNode;
- if (seq.getValue().isEmpty()) {
- return null; // skip empty lists
- }
- }
- if (Tag.MAP.equals(valueNode.getTag())) {
- MappingNode seq = (MappingNode) valueNode;
- if (seq.getValue().isEmpty()) {
- return null; // skip empty maps
- }
- }
- }
- return tuple;
- }
- }
- }
-}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/docker/StackToServiceSpecConverter.java b/karavan-app/src/main/java/org/apache/camel/karavan/docker/StackToServiceSpecConverter.java
deleted file mode 100644
index 672081b0..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/docker/StackToServiceSpecConverter.java
+++ /dev/null
@@ -1,400 +0,0 @@
-package org.apache.camel.karavan.docker;
-
-import com.github.dockerjava.api.model.*;
-import org.apache.camel.karavan.model.*;
-
-import java.time.Duration;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-
-/**
- * Converts a docker-stack.yaml file, parsed by DockerStackConverter,
- * into a List of docker-java ServiceSpec objects.
- */
-public class StackToServiceSpecConverter {
-
- /**
- * Parses a docker-stack.yaml file content and converts all defined services
- * into a list of docker-java ServiceSpec objects.
- *
- * @param stackFileContent The raw string content of the docker-stack.yaml
- * @return A list of ServiceSpec objects ready for the Docker API
- */
- public static List<ServiceSpec> convertStack(String stackFileContent) {
- DockerStack stack = DockerStackConverter.fromCode(stackFileContent);
- return convertStack(stack);
- }
-
- public static List<ServiceSpec> convertStack(DockerStack stack) {
- if (stack.getServices() == null) {
- return new ArrayList<>();
- }
-
- // 2. Map each DockerStackService entry to a ServiceSpec
- return stack.getServices().entrySet().stream()
- .map(entry -> convertService(entry.getKey(), entry.getValue()))
- .collect(Collectors.toList());
- }
-
- /**
- * Converts a single DockerStackService (your POJO) into a ServiceSpec (docker-java model)
- *
- * @param serviceName The name of the service (from the YAML key)
- * @param service The parsed service object
- * @return A docker-java ServiceSpec
- */
- public static ServiceSpec convertService(String serviceName, DockerStackService service) {
- ServiceSpec spec = new ServiceSpec();
-
- // Top-level properties
- spec.withName(serviceName);
- spec.withLabels(service.getLabels());
-
- // --- Task Template (The "container" definition) ---
- spec.withTaskTemplate(buildTaskSpec(service));
-
- // --- Networks ---
- if (service.getNetworks() != null && !service.getNetworks().isEmpty()) {
- spec.withNetworks(buildNetworks(service.getNetworks()));
- }
-
- // --- Ports / EndpointSpec ---
- if (service.getPorts() != null && !service.getPorts().isEmpty()) {
- spec.withEndpointSpec(buildEndpointSpec(service.getPorts()));
- }
-
- // --- Deploy Mode (Replicas) ---
- if (service.getDeploy() != null) {
- spec.withMode(buildServiceMode(service.getDeploy()));
- }
-
- // Note: UpdateConfig and RollbackConfig are not fully modeled
- // in your DockerStackService.Deploy object, so they are omitted here.
-
- return spec;
- }
-
- /**
- * Builds the TaskSpec, which contains the ContainerSpec, Resources, and RestartPolicy
- */
- private static TaskSpec buildTaskSpec(DockerStackService service) {
- TaskSpec taskSpec = new TaskSpec();
-
- // --- ContainerSpec ---
- taskSpec.withContainerSpec(buildContainerSpec(service));
-
- // --- Resources and RestartPolicy (from 'deploy' block) ---
- if (service.getDeploy() != null) {
- if (service.getDeploy().getResources() != null) {
- taskSpec.withResources(buildResources(service.getDeploy().getResources()));
- }
- if (service.getDeploy().getRestart_policy() != null) {
- taskSpec.withRestartPolicy(buildRestartPolicy(service.getDeploy().getRestart_policy()));
- }
- }
-
- return taskSpec;
- }
-
- /**
- * Builds the ContainerSpec, which defines the actual container.
- */
- private static ContainerSpec buildContainerSpec(DockerStackService service) {
- ContainerSpec cs = new ContainerSpec();
- cs.withImage(service.getImage());
- cs.withLabels(service.getLabels());
- cs.withHostname(service.getHostname());
- cs.withConfigs(buildConfigs(service.getConfigs()));
- cs.withCommand(List.of(service.getCommand()));
-
- // --- Environment Variables ---
- if (service.getEnvironment() != null && !service.getEnvironment().isEmpty()) {
- List<String> env = service.getEnvironment().entrySet().stream()
- .map(e -> e.getKey() + "=" + e.getValue())
- .collect(Collectors.toList());
- cs.withEnv(env);
- }
-
- // --- Command ---
- if (service.getCommand() != null && !service.getCommand().isBlank()) {
- // Docker API 'command' is a list. Stack 'command' can be a string.
- // Best practice is to run string commands in a shell.
- cs.withCommand(Arrays.asList("sh", "-c", service.getCommand()));
- }
-
- // --- Mounts (Volumes) ---
- if (service.getVolumes() != null && !service.getVolumes().isEmpty()) {
- cs.withMounts(buildMounts(service.getVolumes()));
- }
-
- // --- Healthcheck ---
- if (service.getHealthcheck() != null) {
- cs.withHealthCheck(buildHealthCheck(service.getHealthcheck()));
- }
-
- // --- Container Labels (from 'deploy' block) ---
- if (service.getDeploy() != null && service.getDeploy().getLabels() != null) {
- cs.withLabels(service.getDeploy().getLabels());
- }
-
- // --- Expose (Note: less common in Swarm than 'ports') ---
-// if (service.getExpose() != null && !service.getExpose().isEmpty()) {
-// Map<String, ExposedPort> exposedPorts = service.getExpose().stream()
-// .collect(Collectors.toMap(
-// port -> port.contains("/") ? port : port + "/tcp", // Assume TCP if not specified
-// port -> new ExposedPort()
-// ));
-// }
-
- return cs;
- }
-
- private static List<ContainerSpecConfig> buildConfigs(List<DockerConfigDefinition> configs) {
- List<ContainerSpecConfig> configList = new ArrayList<>();
- for (DockerConfigDefinition config : configs) {
-
- ContainerSpecFile file = new ContainerSpecFile();
- if (config.getMode() != null) {
- file.withMode(Long.valueOf(config.getMode()));
- }
- file.withName(config.getTarget());
-
- ContainerSpecConfig containerSpecConfig = new ContainerSpecConfig();
- containerSpecConfig.withFile(file);
- containerSpecConfig.withConfigName(config.getSource());
- configList.add(containerSpecConfig);
- }
- return configList;
- }
-
- /**
- * Builds the port mappings
- */
- private static EndpointSpec buildEndpointSpec(List<String> ports) {
- List<PortConfig> portConfigs = ports.stream()
- .map(portString -> {
- // Simple parser, assumes "PUBLISHED:TARGET"
- // Add more logic here for "IP:PUB:TARGET" or "PUB:TARGET/udp"
- String[] parts = portString.split(":");
- Integer published = Integer.parseInt(parts[0]);
- Integer target = Integer.parseInt(parts[1]);
-
- return new PortConfig()
- .withPublishedPort(published)
- .withTargetPort(target)
- .withProtocol(PortConfigProtocol.TCP); // Assume TCP
- })
- .collect(Collectors.toList());
-
- return new EndpointSpec().withPorts(portConfigs);
- }
-
- /**
- * Builds the network attachments
- */
- private static List<NetworkAttachmentConfig> buildNetworks(List<String> networks) {
- return networks.stream()
- .map(netName -> new NetworkAttachmentConfig().withTarget(netName))
- .collect(Collectors.toList());
- }
-
- /**
- * Builds the service mode (e.g., replicated)
- */
- private static ServiceModeConfig buildServiceMode(DockerStackService.Deploy deploy) {
- if (deploy.getReplicas() != null) {
- ServiceReplicatedModeOptions replicated = new ServiceReplicatedModeOptions().withReplicas(deploy.getReplicas());
- return new ServiceModeConfig().withReplicated(replicated);
- }
- // Add logic for 'global' mode if you add it to your Deploy model
- return null;
- }
-
- /**
- * Builds the task resource constraints
- */
- private static ResourceRequirements buildResources(DockerStackService.Resources res) {
- ResourceRequirements tsr = new ResourceRequirements();
-
- if (res.getLimits() != null) {
- var resourceSpec = new ResourceSpecs();
- if (res.getLimits().getCpus() != null) {
- resourceSpec.withNanoCPUs(parseCpusToNanoCpus(res.getLimits().getCpus()));
- }
- if (res.getLimits().getMemory() != null) {
- resourceSpec.withMemoryBytes(parseMemoryToBytes(res.getLimits().getMemory()));
- }
- tsr.withLimits(resourceSpec);
- }
- if (res.getReservations() != null) {
- var resourceSpec = new ResourceSpecs();
- if (res.getReservations().getCpus() != null) {
- resourceSpec.withNanoCPUs(parseCpusToNanoCpus(res.getReservations().getCpus()));
- }
- if (res.getReservations().getMemory() != null) {
- resourceSpec.withMemoryBytes(parseMemoryToBytes(res.getReservations().getMemory()));
- }
- tsr.withLimits(resourceSpec);
- }
- return tsr;
- }
-
- /**
- * Builds the task restart policy
- */
- private static ServiceRestartPolicy buildRestartPolicy(DockerStackService.RestartPolicy policy) {
- ServiceRestartPolicy tsp = new ServiceRestartPolicy();
-
- if (policy.getCondition() != null) {
- tsp.withCondition(toServiceRestartCondition(policy.getCondition()));
- }
- if (policy.getDelay() != null) {
- tsp.withDelay(parseDurationToNanos(policy.getDelay()));
- }
- if (policy.getMax_attempts() != null) {
- tsp.withMaxAttempts(policy.getMax_attempts().longValue());
- }
-
- return tsp;
- }
-
- private static ServiceRestartCondition toServiceRestartCondition(String condition) {
- return switch (condition.toLowerCase()) {
- case "on-failure" -> ServiceRestartCondition.ON_FAILURE;
- case "any" -> ServiceRestartCondition.ANY;
- case "none" -> ServiceRestartCondition.NONE;
- default -> throw new IllegalArgumentException("Unknown restart condition: " + condition);
- };
- }
-
- /**
- * Builds the mounts
- */
- private static List<Mount> buildMounts(List<DockerVolumeDefinition> volumes) {
- return volumes.stream()
- .map(v -> new Mount()
- .withType(MountType.valueOf(v.getType().toUpperCase()))
- .withSource(v.getSource())
- .withTarget(v.getTarget())
- // Add more properties like ReadOnly if you model them
- )
- .collect(Collectors.toList());
- }
-
- /**
- * Builds the health check
- */
- private static HealthCheck buildHealthCheck(DockerHealthCheckDefinition hc) {
- HealthCheck config = new HealthCheck();
- config.withTest(hc.getTest());
- config.withRetries(hc.getRetries());
-
- if (hc.getInterval() != null) {
- config.withInterval(parseDurationToNanos(hc.getInterval()));
- }
- if (hc.getTimeout() != null) {
- config.withTimeout(parseDurationToNanos(hc.getTimeout()));
- }
- if (hc.getStart_period() != null) {
- config.withStartPeriod(parseDurationToNanos(hc.getStart_period()));
- }
- return config;
- }
-
- // --- UTILITY HELPERS ---
-
- /**
- * Converts a duration string (e.g., "30s", "1m", "10000000") into nanoseconds.
- * Accepts:
- * - ISO-8601 style (e.g., "PT30S", "PT1M")
- * - Simplified Docker style (e.g., "30s", "1m")
- * - Plain numeric values (treated as nanoseconds)
- */
- private static Long parseDurationToNanos(String duration) {
- if (duration == null || duration.isBlank()) return null;
- duration = duration.trim();
-
- // Case 1: plain numeric value (no letters) → assume nanoseconds
- if (duration.matches("^\\d+$")) {
- try {
- return Long.parseLong(duration);
- } catch (NumberFormatException e) {
- System.err.println("Warning: Numeric duration overflow for '" + duration + "'");
- return null;
- }
- }
-
- // Case 2: textual duration like "30s", "1m", "2h"
- try {
- // Normalize to ISO-8601 by prefixing "PT"
- return Duration.parse("PT" + duration.toUpperCase()).toNanos();
- } catch (Exception e1) {
- // Case 3: user might have provided already valid ISO-8601 like "PT30S"
- try {
- return Duration.parse(duration.toUpperCase()).toNanos();
- } catch (Exception e2) {
- System.err.println("Warning: Could not parse duration '" + duration + "'. " + e2.getMessage());
- return null;
- }
- }
- }
-
-
- /**
- * Converts a memory string (e.g., "512M", "1G") into bytes.
- */
- private static Long parseMemoryToBytes(String memory) {
- if (memory == null || memory.isBlank()) {
- return null;
- }
-
- // This regex captures the numeric value (group 1) and the optional unit (group 2)
- Pattern pattern = Pattern.compile("^(\\d+)([a-zA-Z]*)$");
- Matcher matcher = pattern.matcher(memory.trim());
-
- if (!matcher.matches()) {
- System.err.println("Warning: Could not parse memory string '" + memory + "'. Invalid format.");
- return null;
- }
-
- try {
- long value = Long.parseLong(matcher.group(1));
- // Normalize the unit to uppercase to handle "mb", "Mb", "MB", etc.
- String unit = matcher.group(2).toUpperCase();
-
- return switch (unit) {
- case "G", "GB" -> value * 1024 * 1024 * 1024;
- case "M", "MB" -> value * 1024 * 1024;
- case "K", "KB" -> value * 1024;
- // No unit, or explicitly "B" for bytes
- case "", "B" -> value;
- default -> {
- System.err.println("Warning: Unknown memory unit '" + matcher.group(2) + "' in '" + memory + "'. Treating as bytes.");
- yield value;
- }
- };
- } catch (NumberFormatException e) {
- // This should be rare given the regex, but it's safe to handle
- System.err.println("Warning: Could not parse numeric part of memory '" + memory + "'. " + e.getMessage());
- return null;
- }
- }
-
- /**
- * Converts a CPU string (e.g., "0.5", "1") into nano-CPUs.
- */
- private static Long parseCpusToNanoCpus(String cpus) {
- if (cpus == null) return null;
- try {
- double cpuValue = Double.parseDouble(cpus);
- return (long) (cpuValue * 1_000_000_000L); // 1 CPU = 1e9 nano-CPUs
- } catch (Exception e) {
- System.err.println("Warning: Could not parse cpus '" + cpus + "'. " + e.getMessage());
- return null;
- }
- }
-}
\ No newline at end of file
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 8e74fbbf..7f3516ff 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
@@ -20,6 +20,7 @@ import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
+import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.fabric8.kubernetes.client.dsl.LogWatch;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.quarkus.runtime.LaunchMode;
@@ -38,13 +39,13 @@ import org.apache.camel.karavan.service.ConfigService;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
+import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
-import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.apache.camel.karavan.KaravanConstants.*;
-import static org.apache.camel.karavan.service.CodeService.BUILD_SCRIPT_FILENAME;
+import static org.apache.camel.karavan.service.CodeService.CAMEL_OBSERVABILITY_PORT;
@Default
@ApplicationScoped
@@ -52,6 +53,13 @@ public class KubernetesService {
private static final Logger LOGGER = Logger.getLogger(KubernetesService.class.getName());
+ public static final Map<String, Quantity> DEFAULT_CONTAINER_RESOURCES = Map.of(
+ "requests.memory", new Quantity("256Mi"),
+ "requests.cpu", new Quantity("500m"),
+ "limits.memory", new Quantity("2048Mi"),
+ "limits.cpu", new Quantity("2000m")
+ );
+
@ConfigProperty(name = "karavan.environment", defaultValue = KaravanConstants.DEV)
private String environment;
@@ -89,32 +97,6 @@ public class KubernetesService {
@ConfigProperty(name = "karavan.openshift")
Optional<Boolean> isOpenShift;
- public void createConfigmap(String name, Map<String, String> data) {
- LOGGER.info("Creating configmap " + name);
- if (ConfigService.inKubernetes()) {
- try (KubernetesClient client = kubernetesClient()) {
- ConfigMap configMap = client.configMaps().inNamespace(getNamespace()).withName(name).get();
- if (configMap == null) {
- configMap = new ConfigMapBuilder()
- .withMetadata(new ObjectMetaBuilder()
- .withName(name)
- .withLabels(getPartOfLabels())
- .withNamespace(getNamespace())
- .build())
- .build();
- configMap.setData(data);
- client.resource(configMap).create();
- } else {
- configMap.setData(data);
- client.resource(configMap).update();
- }
-
- } catch (Exception e) {
- LOGGER.error("Error create Configmap: " + e.getMessage());
- }
- }
- }
-
public void runBuildProject(String projectId, String podFragment, Map<String, String> envVars) {
try (KubernetesClient client = kubernetesClient()) {
String containerName = projectId + BUILDER_SUFFIX;
@@ -123,7 +105,7 @@ public class KubernetesService {
// Delete old build pod
Pod old = client.pods().inNamespace(getNamespace()).withName(containerName).get();
if (old != null) {
- client.resource(old).delete();
+ client.resource(old).delete().wait(60000);
}
boolean hasDockerConfigSecret = hasDockerConfigSecret();
Pod pod = getBuilderPod(containerName, labels, podFragment, hasDockerConfigSecret, envVars);
@@ -162,7 +144,6 @@ public class KubernetesService {
return labels;
}
- // TODO: Move all possible stuff to pod fragment
private Pod getBuilderPod(String name, Map<String, String> labels, String configFragment, boolean hasDockerConfigSecret, Map<String, String> envVars) {
ObjectMeta meta = new ObjectMetaBuilder()
.withName(name)
@@ -170,14 +151,10 @@ public class KubernetesService {
.withNamespace(getNamespace())
.build();
- ContainerPort port = new ContainerPortBuilder()
- .withContainerPort(8080)
- .withName("http")
- .withProtocol("TCP")
- .build();
+ ContainerPort port = new ContainerPortBuilder().withContainerPort(8080).withName("http").withProtocol("TCP").build();
+ ContainerPort observabilityPort = new ContainerPortBuilder().withContainerPort(CAMEL_OBSERVABILITY_PORT).withName("observability").withProtocol("TCP").build();
List<VolumeMount> volumeMounts = new ArrayList<>();
- volumeMounts.add(new VolumeMountBuilder().withName(BUILD_SCRIPT_VOLUME_NAME).withMountPath("/karavan/builder").withReadOnly(true).build());
if (hasDockerConfigSecret) {
volumeMounts.add(new VolumeMountBuilder().withName(BUILD_DOCKER_CONFIG_SECRET).withMountPath("/karavan/.docker").withReadOnly(true).build());
}
@@ -194,18 +171,14 @@ public class KubernetesService {
Container container = new ContainerBuilder()
.withName(name)
.withImage(devmodeImage)
- .withPorts(port)
+ .withPorts(port, observabilityPort)
.withImagePullPolicy(devmodeImagePullPolicy.orElse("IfNotPresent"))
.withEnv(pod.getSpec().getContainers().getFirst().getEnv())
- .withCommand("/bin/sh", "-c", "/karavan/builder/build.sh")
+// .withCommand("/bin/sh", "-c", "/karavan/builder/build.sh")
.withVolumeMounts(volumeMounts)
.build();
List<Volume> volumes = new ArrayList<>();
- volumes.add(new VolumeBuilder().withName(BUILD_SCRIPT_VOLUME_NAME)
- .withConfigMap(new ConfigMapVolumeSourceBuilder().withName(BUILD_SCRIPT_CONFIG_MAP).withItems(
- new KeyToPathBuilder().withKey(BUILD_SCRIPT_FILENAME).withPath(BUILD_SCRIPT_FILENAME).build()
- ).withDefaultMode(511).build()).build());
if (hasDockerConfigSecret) {
volumes.add(new VolumeBuilder().withName(BUILD_DOCKER_CONFIG_SECRET)
.withSecret(new SecretVolumeSourceBuilder().withSecretName(BUILD_DOCKER_CONFIG_SECRET).withItems(
@@ -248,6 +221,19 @@ public class KubernetesService {
public Tuple2<LogWatch, KubernetesClient> getContainerLogWatch(String podName) {
KubernetesClient client = kubernetesClient();
+ try {
+ // Wait up to 30 seconds for the pod to leave the 'Pending' phase (ContainerCreating)
+ client.pods().inNamespace(getNamespace()).withName(podName)
+ .waitUntilCondition(pod -> pod != null &&
+ ("Running".equals(pod.getStatus().getPhase()) ||
+ "Succeeded".equals(pod.getStatus().getPhase()) ||
+ "Failed".equals(pod.getStatus().getPhase())),
+ 30, TimeUnit.SECONDS);
+ } catch (Exception e) {
+ LOGGER.warn("Timeout or error waiting for pod " + podName + " to become ready: " + e.getMessage());
+ // Proceeding anyway so we don't completely block log attempts if the condition logic fails
+ }
+
LogWatch logWatch = client.pods().inNamespace(getNamespace()).withName(podName).tailingLines(100).watchLog();
return Tuple2.of(logWatch, client);
}
@@ -256,7 +242,7 @@ public class KubernetesService {
try (KubernetesClient client = kubernetesClient()) {
client.apps().deployments().inNamespace(getNamespace()).withName(name).rolling().restart();
} catch (Exception ex) {
- LOGGER.error(ex.getMessage());
+ LOGGER.error("Failed to apply Kubernetes resources", ex);
}
}
@@ -273,7 +259,7 @@ public class KubernetesService {
client.resource(item).inNamespace(getNamespace()).serverSideApply();
});
} catch (Exception ex) {
- LOGGER.error(ex.getMessage());
+ LOGGER.error("Failed to apply Kubernetes resources", ex);
}
}
@@ -340,7 +326,7 @@ public class KubernetesService {
return result;
}
- 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) {
+ public void runDevModeContainer(String projectId, Boolean verbose, Boolean compile, String projectDevmodeImage, String deploymentFragment, Map<String, String> labels, Map<String, String> envVars) {
Map<String, String> podLabels = new HashMap<>(labels);
podLabels.putAll(getLabels(projectId, projectId, ContainerType.devmode));
@@ -351,32 +337,12 @@ public class KubernetesService {
Pod old = client.pods().inNamespace(getNamespace()).withName(projectId).get();
if (old == null) {
Pod pod = getDevModePod(projectId, verbose, compile, podLabels, projectDevmodeImage, deploymentFragment, envVars);
- Pod result = client.resource(pod).serverSideApply(); // important
- result = client.pods().inNamespace(getNamespace()).withName(projectId).waitUntilReady(30, TimeUnit.SECONDS);
- LOGGER.info("Pod " + result.getMetadata().getName() + " status " + result.getStatus());
- var copyFiles = copyFilesToContainer(result, files, "/karavan/code");
- LOGGER.info("Pod files copy result is " + copyFiles);
- var copyDone = copyFilesToContainer(result, Map.of(".karavan.done", "done"), "/tmp");
- LOGGER.info("Pod files copy done is " + copyDone);
- LOGGER.info("Pod pod " + result.getMetadata().getName());
+ client.resource(pod).serverSideApply();
}
}
createService(projectId, podLabels);
}
- private boolean copyFilesToContainer(Pod pod, Map<String, String> files, String dirName) {
- try (KubernetesClient client = kubernetesClient()) {
- String temp = codeService.saveProjectFilesInTemp(files);
- return client.pods().inNamespace(getNamespace())
- .withName(pod.getMetadata().getName())
- .dir(dirName)
- .upload(Paths.get(temp));
- } catch (Exception e) {
- LOGGER.info("Error copying filed to devmode pod: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
- return false;
- }
- }
-
public void deletePodAndService(String name, boolean deletePVC) {
try (KubernetesClient client = kubernetesClient()) {
LOGGER.info("Delete pod/service: " + name + " in the namespace: " + getNamespace());
@@ -389,13 +355,30 @@ public class KubernetesService {
LOGGER.error(ex.getMessage());
}
}
+ private Map<String, Quantity> getResourceLimits(PodSpec podSpec) {
+ try {
+ return podSpec.getContainers().get(0).getResources().getLimits();
+ } catch (Exception e) {
+ return new HashMap<>();
+ }
+ }
+
+ private Map<String, Quantity> getResourceRequests(PodSpec podSpec) {
+ try {
+ return podSpec.getContainers().get(0).getResources().getRequests();
+ } catch (Exception e) {
+ return new HashMap<>();
+ }
+ }
- public ResourceRequirements getResourceRequirements(Map<String, String> containerResources) {
+ public ResourceRequirements getResourceRequirements(PodSpec podSpec) {
+ var limits = getResourceLimits(podSpec);
+ var requests = getResourceRequests(podSpec);
return new ResourceRequirementsBuilder()
- .addToRequests("cpu", new Quantity(containerResources.get("requests.cpu")))
- .addToRequests("memory", new Quantity(containerResources.get("requests.memory")))
- .addToLimits("cpu", new Quantity(containerResources.get("limits.cpu")))
- .addToLimits("memory", new Quantity(containerResources.get("limits.memory")))
+ .addToRequests("cpu", requests.getOrDefault("cpu", DEFAULT_CONTAINER_RESOURCES.get("requests.cpu")))
+ .addToRequests("memory", requests.getOrDefault("memory", DEFAULT_CONTAINER_RESOURCES.get("requests.memory")))
+ .addToLimits("cpu", limits.getOrDefault("cpu", DEFAULT_CONTAINER_RESOURCES.get("limits.cpu")))
+ .addToLimits("memory", limits.getOrDefault("memory", DEFAULT_CONTAINER_RESOURCES.get("limits.memory")))
.build();
}
@@ -413,8 +396,7 @@ public class KubernetesService {
volumeMounts = podSpec.getContainers().getFirst().getVolumeMounts();
} catch (Exception ignored) {}
- Map<String, String> containerResources = CodeService.DEFAULT_CONTAINER_RESOURCES;
- ResourceRequirements resources = getResourceRequirements(containerResources);
+ ResourceRequirements resources = getResourceRequirements(podSpec);
ObjectMeta meta = new ObjectMetaBuilder()
.withName(name)
@@ -422,11 +404,8 @@ public class KubernetesService {
.withNamespace(getNamespace())
.build();
- ContainerPort port = new ContainerPortBuilder()
- .withContainerPort(8080)
- .withName("http")
- .withProtocol("TCP")
- .build();
+ ContainerPort port = new ContainerPortBuilder().withContainerPort(8080).withName("http").withProtocol("TCP").build();
+ ContainerPort observabilityPort = new ContainerPortBuilder().withContainerPort(CAMEL_OBSERVABILITY_PORT).withName("observability").withProtocol("TCP").build();
List<EnvVar> environmentVariables = new ArrayList<>();
try {
@@ -448,7 +427,7 @@ public class KubernetesService {
Container container = new ContainerBuilder()
.withName(name)
.withImage(projectDevmodeImage != null ? projectDevmodeImage : devmodeImage)
- .withPorts(port)
+ .withPorts(port, observabilityPort)
.withResources(resources)
.withImagePullPolicy(devmodeImagePullPolicy.orElse("IfNotPresent"))
.withEnv(environmentVariables)
@@ -718,12 +697,33 @@ public class KubernetesService {
.withField("regarding.name", containerName)
.list(new ListOptionsBuilder().withLimit(100L).build())
.getItems().forEach(e -> {
+ String lastTimestamp;
+ Integer count;
+
+ if (e.getSeries() != null && e.getSeries().getLastObservedTime() != null) {
+ // 1. Best case: Event has repeated, use the new series time
+ lastTimestamp = e.getSeries().getLastObservedTime().getTime();
+ count = e.getSeries().getCount();
+ } else if (e.getEventTime() != null) {
+ // 2. Event happened once: Use the single-occurrence time
+ lastTimestamp = e.getEventTime().getTime();
+ count = 1;
+ } else if (e.getDeprecatedLastTimestamp() != null) {
+ // 3. Fallback: Legacy compatibility field
+ lastTimestamp = e.getDeprecatedLastTimestamp();
+ count = e.getDeprecatedCount();
+ } else {
+ lastTimestamp = "Unknown";
+ count = 0;
+ }
PodEvent pe = new PodEvent(
e.getMetadata().getName(),
e.getRegarding().getName(),
e.getReason(),
e.getNote(),
- e.getMetadata().getCreationTimestamp());
+ e.getType(),
+ count,
+ lastTimestamp);
list.add(pe);
});
} catch (Exception e) {
@@ -731,4 +731,43 @@ public class KubernetesService {
}
return list;
}
+
+ public void execCommandInDeployment(String deploymentName, String command) {
+ try (KubernetesClient client = kubernetesClient()) {
+ Deployment deployment = client.apps().deployments().inNamespace(getNamespace()).withName(deploymentName).get();
+
+ if (deployment != null && deployment.getSpec().getSelector() != null) {
+ Map<String, String> matchLabels = deployment.getSpec().getSelector().getMatchLabels();
+ List<Pod> pods = client.pods().inNamespace(getNamespace()).withLabels(matchLabels).list().getItems();
+
+ if (!pods.isEmpty()) {
+ Pod pod = pods.getFirst();
+ String podName = pod.getMetadata().getName();
+ String containerName = pod.getSpec().getContainers().getFirst().getName();
+
+ LOGGER.info("Executing command in pod " + podName + " (container: " + containerName + ")");
+
+ try (ExecWatch watch = client.pods().inNamespace(getNamespace()).withName(podName)
+ .inContainer(containerName)
+ .writingOutput(OutputStream.nullOutputStream())
+ .writingError(OutputStream.nullOutputStream())
+ .exec("sh", "-c", command)) {
+
+ // Block the thread until the command finishes (or times out after 30s)
+ // This keeps the WebSocket open long enough for the command to run.
+ watch.exitCode().get(30, TimeUnit.SECONDS);
+
+ } catch (Exception e) {
+ LOGGER.error("Exec failed or timed out: " + e.getMessage());
+ }
+ } else {
+ LOGGER.warn("No pods found for deployment: " + deploymentName);
+ }
+ } else {
+ LOGGER.warn("Deployment not found or has no selector: " + deploymentName);
+ }
+ } catch (Exception e) {
+ LOGGER.error("Error executing command in container for deployment " + deploymentName + ": " + e.getMessage(), e);
+ }
+ }
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesStatusService.java b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesStatusService.java
index 9d150031..4137bf58 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesStatusService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesStatusService.java
@@ -151,12 +151,12 @@ public class KubernetesStatusService implements HealthCheck {
}
}
- public ResourceRequirements getResourceRequirements(Map<String, String> containerResources) {
+ public ResourceRequirements getResourceRequirements(Map<String, Quantity> containerResources) {
return new ResourceRequirementsBuilder()
- .addToRequests("cpu", new Quantity(containerResources.get("requests.cpu")))
- .addToRequests("memory", new Quantity(containerResources.get("requests.memory")))
- .addToLimits("cpu", new Quantity(containerResources.get("limits.cpu")))
- .addToLimits("memory", new Quantity(containerResources.get("limits.memory")))
+ .addToRequests("cpu", containerResources.get("requests.cpu"))
+ .addToRequests("memory", containerResources.get("requests.memory"))
+ .addToLimits("cpu", containerResources.get("limits.cpu"))
+ .addToLimits("memory", containerResources.get("limits.memory"))
.build();
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/PodEventHandler.java b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/PodEventHandler.java
index 3ec79859..c78aa88a 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/PodEventHandler.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/PodEventHandler.java
@@ -30,12 +30,12 @@ import org.apache.camel.karavan.cache.PodContainerStatus;
import org.jboss.logging.Logger;
import java.util.List;
-import java.util.Map;
import java.util.Objects;
import static org.apache.camel.karavan.KaravanConstants.*;
import static org.apache.camel.karavan.KaravanEvents.POD_CONTAINER_DELETED;
import static org.apache.camel.karavan.KaravanEvents.POD_CONTAINER_UPDATED;
+import static org.apache.camel.karavan.kubernetes.KubernetesService.DEFAULT_CONTAINER_RESOURCES;
public class PodEventHandler implements ResourceEventHandler<Pod> {
@@ -44,13 +44,6 @@ public class PodEventHandler implements ResourceEventHandler<Pod> {
private final KubernetesStatusService kubernetesStatusService;
private final EventBus eventBus;
- public static final Map<String, String> DEFAULT_CONTAINER_RESOURCES = Map.of(
- "requests.memory", "256Mi",
- "requests.cpu", "500m",
- "limits.memory", "2048Mi",
- "limits.cpu", "2000m"
- );
-
public PodEventHandler(KubernetesStatusService kubernetesStatusService, EventBus eventBus) {
this.kubernetesStatusService = kubernetesStatusService;
this.eventBus = eventBus;
@@ -161,6 +154,16 @@ public class PodEventHandler implements ResourceEventHandler<Pod> {
} else {
status.setState(PodContainerStatus.State.created.name());
}
+ if (pod.getStatus() != null && pod.getStatus().getContainerStatuses() != null && !pod.getStatus().getContainerStatuses().isEmpty()) {
+ var containerStatus = pod.getStatus().getContainerStatuses().getFirst();
+ if (containerStatus != null) {
+ if (containerStatus.getState().getWaiting() != null) {
+ status.setStateMessage(containerStatus.getState().getWaiting().getMessage());
+ } else if (containerStatus.getState().getTerminated() != null) {
+ status.setStateMessage(containerStatus.getState().getTerminated().getMessage());
+ }
+ }
+ }
return status;
} catch (Exception ex) {
ex.printStackTrace();
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelReloadListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelReloadListener.java
index a1275bdf..7fadfa71 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelReloadListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelReloadListener.java
@@ -39,6 +39,7 @@ import java.util.concurrent.ExecutionException;
import static org.apache.camel.karavan.KaravanEvents.CMD_RELOAD_PROJECT_CODE;
import static org.apache.camel.karavan.KaravanEvents.POD_CONTAINER_UPDATED;
+import static org.apache.camel.karavan.service.CodeService.CAMEL_OBSERVABILITY_PORT;
@ApplicationScoped
public class CamelReloadListener {
@@ -75,7 +76,7 @@ public class CamelReloadListener {
try {
PodContainerStatus podContainerStatus = karavanCache.getDevModePodContainerStatus(projectId, environment);
deleteRequest(podContainerStatus);
- Map<String, String> files = codeService.getProjectFilesForDevMode(projectId, true);
+ Map<String, String> files = codeService.getProjectFilesForDevMode(projectId);
files.forEach((name, code) -> putRequest(podContainerStatus, name, code, 1000));
reloadRequest(podContainerStatus);
podContainerStatus.setCodeLoaded(true);
@@ -120,9 +121,9 @@ public class CamelReloadListener {
String getContainerAddressForReload(PodContainerStatus podContainerStatus) throws Exception {
if (ConfigService.inKubernetes()) {
- return "http://" + podContainerStatus.getPodIP() + ":8080";
+ return "http://" + podContainerStatus.getPodIP() + ":" + CAMEL_OBSERVABILITY_PORT;
} else if (ConfigService.inDocker()) {
- return "http://" + podContainerStatus.getContainerName() + ":8080";
+ return "http://" + podContainerStatus.getContainerName() + ":" + CAMEL_OBSERVABILITY_PORT;
} else if (podContainerStatus.getPorts() != null && !podContainerStatus.getPorts().isEmpty()) {
Integer port = podContainerStatus.getPorts().get(0).getPublicPort();
if (port != null) {
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelStatusListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelStatusListener.java
index b221c52a..7c644e5e 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelStatusListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CamelStatusListener.java
@@ -41,6 +41,7 @@ import java.util.Objects;
import java.util.concurrent.ExecutionException;
import static org.apache.camel.karavan.KaravanEvents.CMD_COLLECT_CAMEL_STATUS;
+import static org.apache.camel.karavan.service.CodeService.CAMEL_OBSERVABILITY_PORT;
@ApplicationScoped
public class CamelStatusListener {
@@ -83,32 +84,32 @@ public class CamelStatusListener {
CamelStatus cs = new CamelStatus(projectId, containerName, statuses, environment);
karavanCache.saveCamelStatus(cs);
} catch (Exception ex) {
-// LOGGER.warn("collectCamelStatuses " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
+ LOGGER.warn("collectCamelStatuses " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
}
}
public String getContainerAddressForStatus(PodContainerStatus podContainerStatus) throws Exception {
if (ConfigService.inKubernetes()) {
- return "http://" + podContainerStatus.getPodIP() + ":8080";
+ return "http://" + podContainerStatus.getPodIP() + ":" + CAMEL_OBSERVABILITY_PORT;
} else if (ConfigService.inDocker()) {
- return "http://" + podContainerStatus.getContainerName() + ":8080";
+ return "http://" + podContainerStatus.getContainerName() + ":" + CAMEL_OBSERVABILITY_PORT;
} else if (podContainerStatus.getPorts() != null && !podContainerStatus.getPorts().isEmpty()) {
Integer port = podContainerStatus.getPorts().get(0).getPublicPort();
if (port != null) {
- return "http://localhost:" + port;
+ return "http://localhost:" + CAMEL_OBSERVABILITY_PORT;
}
}
throw new Exception("No port configured for project " + podContainerStatus.getContainerName());
}
public String getCamelStatus(PodContainerStatus podContainerStatus, CamelStatusValue.Name statusName) throws Exception {
- var name = statusName.name();
+ var name = statusName.name().replace("_", "-");
var path = "/q/dev/" + name + (Objects.equals(name, "trace") ? "?dump=true" : "");
String url = getContainerAddressForStatus(podContainerStatus) + path;
try {
- return getResult(url, 500);
+ return getResult(url, 1500);
} catch (InterruptedException | ExecutionException ex) {
-// LOGGER.warn("getCamelStatus " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
+ LOGGER.warn("getCamelStatus " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
}
return null;
}
@@ -123,7 +124,7 @@ public class CamelStatusListener {
return res.encodePrettily();
}
} catch (Exception ex) {
-// LOGGER.warn("getResult " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
+ LOGGER.warn("getResult " + (ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage()));
}
return null;
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/ConfigListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/ConfigListener.java
deleted file mode 100644
index e6efc3f7..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/ConfigListener.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.apache.camel.karavan.listener;
-
-import io.quarkus.vertx.ConsumeEvent;
-import io.vertx.core.json.JsonObject;
-import io.vertx.mutiny.core.eventbus.EventBus;
-import jakarta.enterprise.context.ApplicationScoped;
-import jakarta.enterprise.inject.Default;
-import jakarta.inject.Inject;
-import org.apache.camel.karavan.service.ConfigService;
-import org.jboss.logging.Logger;
-
-import static org.apache.camel.karavan.KaravanEvents.*;
-
-@Default
-@ApplicationScoped
-public class ConfigListener {
-
- private static final Logger LOGGER = Logger.getLogger(ConfigListener.class.getName());
-
- @Inject
- ConfigService configService;
-
- @Inject
- EventBus eventBus;
-
- @ConsumeEvent(value = NOTIFICATION_PROJECTS_STARTED, blocking = true)
- public void shareOnStartup(String data) throws Exception {
- configService.shareOnStartup();
- }
-
- @ConsumeEvent(value = CMD_SHARE_CONFIGURATION, blocking = true, ordered = true)
- public void shareConfig(JsonObject event) throws Exception {
- String filename = event.getString("filename");
- String userId = event.getString("userId");
- LOGGER.info("Config share event: for " + (filename != null ? filename : "all"));
- try {
- configService.share(filename);
- eventBus.publish(NOTIFICATION_CONFIG_SHARED, JsonObject.of("userId", userId, "className", "filename", "filename", filename));
- } catch (Exception e) {
- var error = e.getCause() != null ? e.getCause() : e;
- LOGGER.error("Failed to share configuration", error);
- if (userId != null) {
- eventBus.publish(NOTIFICATION_ERROR, JsonObject.of(
- "userId", userId,
- "className", filename,
- "error", "Failed to share configuration: " + e.getMessage())
- );
- }
- }
- }
-}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerListener.java
index 5d5255c9..353d5eb7 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerListener.java
@@ -34,7 +34,6 @@ import java.util.Map;
import java.util.Objects;
import static org.apache.camel.karavan.KaravanEvents.*;
-import static org.apache.camel.karavan.service.CodeService.BUILD_SCRIPT_FILENAME;
@ApplicationScoped
public class DockerListener {
@@ -77,23 +76,23 @@ public class DockerListener {
var projectId = event.getString("projectId");
var containerId = event.getString("containerId");
var type = event.getString("type");
- var statuses = karavanCache.getPodContainerStatuses(projectId, environment);
+ var statuses = karavanCache.getPodContainerStatusesByProject(projectId);
var status = statuses != null && !statuses.isEmpty() ? statuses.getFirst() : null;
try {
status = status != null ? status : PodContainerStatus.createDevMode(projectId, environment);
if (!status.getCodeLoaded()) {
if (Objects.equals(type, "devmode")) {
- Map<String, String> files = codeService.getProjectFilesForDevMode(projectId, true);
+ Map<String, String> files = codeService.getProjectFilesForDevMode(projectId);
LOGGER.info("Copy files: " + files.size());
- dockerService.copyFiles(containerId, "/karavan/code", files, true);
- dockerService.copyFiles(containerId, "/tmp", Map.of(".karavan.done", "done"), true);
+// dockerService.copyFiles(containerId, "/karavan/code", files, true);
+// dockerService.copyFiles(containerId, "/tmp", Map.of(".karavan.done", "done"), true);
} else if (Objects.equals(type, "build")) {
Map<String, String> sshFiles = codeService.getSshFiles();
String script = codeService.getBuilderScript();
LOGGER.info("Copy build script: " + script.length() + " and ssh files: " + sshFiles.size());
- dockerService.copyExecFile(containerId, "/karavan/builder", BUILD_SCRIPT_FILENAME, script);
+// dockerService.copyFileToContainer(containerId, "/karavan/builder", BUILD_SCRIPT_FILENAME, script, 0755);
sshFiles.forEach((name, text) -> {
- dockerService.copyExecFile(containerId, "/karavan/.ssh", name, text);
+// dockerService.copyFileToContainer(containerId, "/karavan/.ssh", name, text, 0600);
});
}
status.setCodeLoaded(true);
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerStatusListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerStatusListener.java
index bf99456d..fc8ab1ec 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerStatusListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/DockerStatusListener.java
@@ -67,11 +67,9 @@ public class DockerStatusListener {
@ConsumeEvent(value = CMD_CLEAN_STATUSES, blocking = true)
void cleanContainersStatuses(String data) {
try {
- List<PodContainerStatus> statusesInDocker = dockerService.isInSwarmMode()
- ? getServicesStatuses()
- : getContainersStatuses();
+ List<PodContainerStatus> statusesInDocker = getContainersStatuses();
List<String> namesInDocker = statusesInDocker.stream().map(PodContainerStatus::getContainerName).toList();
- List<PodContainerStatus> statusesInCache = karavanCache.getPodContainerStatuses(environment);
+ List<PodContainerStatus> statusesInCache = karavanCache.getAllPodContainerStatuses();
// clean deleted
statusesInCache.stream()
.filter(cs -> !checkTransit(cs))
@@ -100,20 +98,6 @@ public class DockerStatusListener {
return result;
}
- public List<PodContainerStatus> getServicesStatuses() {
- List<PodContainerStatus> result = new ArrayList<>();
- dockerService.getAllServices().forEach(service -> {
- var containers = dockerService.findContainersByServiceId(service.getId());
- if (containers != null) {
- containers.forEach(container -> {
- PodContainerStatus podContainerStatus = DockerUtils.getServiceStatus(service, container, environment);
- result.add(podContainerStatus);
- });
- }
- });
- return result;
- }
-
public PodContainerStatus getContainerStatistics(PodContainerStatus podContainerStatus) {
Container container = dockerService.getContainer(podContainerStatus.getContainerId());
if (container != null) {
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/NotificationListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/NotificationListener.java
index a3138969..d218a1db 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/NotificationListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/NotificationListener.java
@@ -39,7 +39,6 @@ public class NotificationListener {
public static final String EVENT_ERROR = "error";
public static final String EVENT_COMMIT = "commit";
- public static final String EVENT_CONFIG_SHARED = "configShared";
public static final String EVENT_IMAGES_LOADED = "imagesLoaded";
public static final String EVENT_PROJECT_CHANGED = "projectChanged";
@@ -58,17 +57,6 @@ public class NotificationListener {
}
}
- @ConsumeEvent(value = NOTIFICATION_CONFIG_SHARED, blocking = true, ordered = true)
- public void onShareHappened(JsonObject event) throws Exception {
- String userId = event.getString("userId");
- String className = event.getString("className");
- if (userId != null) {
- send(userId, null, EVENT_CONFIG_SHARED, className, event);
- } else {
- sendSystem(null, EVENT_CONFIG_SHARED, className, event);
- }
- }
-
@ConsumeEvent(value = NOTIFICATION_IMAGES_LOADED, blocking = true, ordered = true)
public void onImageLoaded(JsonObject event) throws Exception {
String userId = event.getString("userId");
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/listener/PodContainerCommandListener.java b/karavan-app/src/main/java/org/apache/camel/karavan/listener/PodContainerCommandListener.java
index 8f6a7599..454d0baa 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/listener/PodContainerCommandListener.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/listener/PodContainerCommandListener.java
@@ -57,8 +57,6 @@ public class PodContainerCommandListener {
setContainerStatusTransit(projectId, ContainerType.devmode.name());
if (ConfigService.inKubernetes()) {
kubernetesService.deletePodAndService(projectId, false);
- } else if (dockerService.isInSwarmMode()) {
- dockerService.deleteService(projectId);
} else {
dockerService.deleteContainer(projectId);
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/loader/CacheLoader.java b/karavan-app/src/main/java/org/apache/camel/karavan/loader/CacheLoader.java
index da896d4b..95127c16 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/loader/CacheLoader.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/loader/CacheLoader.java
@@ -48,6 +48,8 @@ public class CacheLoader {
karavanCache.saveRole(json.mapTo(AccessRole.class), false);
} else if (AccessPassword.class.getSimpleName().equals(entity.type)) {
karavanCache.savePassword(json.mapTo(AccessPassword.class), false);
+ } else if (AccessToken.class.getSimpleName().equals(entity.type)) {
+ karavanCache.saveToken(json.mapTo(AccessToken.class), false);
}
});
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/loader/GitLoader.java b/karavan-app/src/main/java/org/apache/camel/karavan/loader/GitLoader.java
index d3cef68b..1234ec00 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/loader/GitLoader.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/loader/GitLoader.java
@@ -69,6 +69,8 @@ public class GitLoader {
addBuildInProject(ProjectFolder.Type.configuration.name());
addBuildInProject(ProjectFolder.Type.documentation.name());
addBuildInProject(ProjectFolder.Type.contracts.name());
+ addBuildInProject(ProjectFolder.Type.mcp.name());
+ addBuildInProject(ProjectFolder.Type.sdx.name());
}
gitHistoryService.importCommits();
} else {
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/model/Configuration.java b/karavan-app/src/main/java/org/apache/camel/karavan/model/Configuration.java
index 183f1bf0..d907dc5f 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/model/Configuration.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/model/Configuration.java
@@ -23,7 +23,6 @@ public class Configuration {
private String title;
private String version;
private String infrastructure;
- private boolean swarmMode;
private String environment;
private String platformSecretName;
private String platformConfigName;
@@ -35,11 +34,10 @@ public class Configuration {
public Configuration() {
}
- public Configuration(String title, String version, String infrastructure, boolean swarmMode, String environment, String platformSecretName, String platformConfigName, List<String> environments, List<String> configFilenames, Map<String, String> advanced) {
+ public Configuration(String title, String version, String infrastructure, String environment, String platformSecretName, String platformConfigName, List<String> environments, List<String> configFilenames, Map<String, String> advanced) {
this.title = title;
this.version = version;
this.infrastructure = infrastructure;
- this.swarmMode = swarmMode;
this.environment = environment;
this.platformSecretName = platformSecretName;
this.platformConfigName = platformConfigName;
@@ -48,13 +46,6 @@ public class Configuration {
this.advanced = advanced;
}
- public boolean isSwarmMode() {
- return swarmMode;
- }
-
- public void setSwarmMode(boolean swarmMode) {
- this.swarmMode = swarmMode;
- }
public String getTitle() {
return title;
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStack.java b/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStack.java
deleted file mode 100644
index 92781965..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStack.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package org.apache.camel.karavan.model;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Represents a Docker Stack file (e.g., docker-stack.yml) for Swarm mode.
- * * Assumes you have corresponding classes:
- * - DockerStackService (from the previous response)
- * - DockerNetworkDefinition (represents network definitions)
- * - DockerVolumeDefinition (represents top-level volume definitions)
- */
-public class DockerStack {
- private String version;
- private Map<String, DockerStackService> services = new HashMap<>();
- private Map<String, DockerNetworkDefinition> networks = new HashMap<>();
- private Map<String, DockerVolumeDefinition> volumes = new HashMap<>(); // Added for stack files
-
- public DockerStack() {
- }
-
- public DockerStack(Map<String, DockerStackService> services) {
- this.services = services;
- }
-
- // Note: The static 'create' method was removed.
- // It relied on 'container_name', which is not supported in Docker Swarm.
- // You would now add a service like this:
- // myStack.getServices().put("my-service-name", myStackServiceInstance);
-
- public Map<String, DockerStackService> getServices() {
- return services;
- }
-
- public void setServices(Map<String, DockerStackService> services) {
- this.services = services;
- }
-
- public String getVersion() {
- return version;
- }
-
- public void setVersion(String version) {
- this.version = version;
- }
-
- public Map<String, DockerNetworkDefinition> getNetworks() {
- return networks;
- }
-
- public void setNetworks(Map<String, DockerNetworkDefinition> networks) {
- this.networks = networks;
- }
-
- public Map<String, DockerVolumeDefinition> getVolumes() {
- return volumes;
- }
-
- public void setVolumes(Map<String, DockerVolumeDefinition> volumes) {
- this.volumes = volumes;
- }
-
- // You would also need to define these helper classes,
- // similar to your DockerComposeNetwork class.
- //
- // public class DockerNetworkDefinition {
- // private String driver;
- // private boolean attachable;
- // private Map<String, String> driver_opts;
- // private Map<String, String> labels;
- // // ... getters/setters ...
- // }
- //
- // public class DockerVolumeDefinition {
- // private String driver;
- // private Map<String, String> driver_opts;
- // private Map<String, String> labels;
- // // ... getters/setters ...
- // }
-}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStackService.java b/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStackService.java
deleted file mode 100644
index 79ab057d..00000000
--- a/karavan-app/src/main/java/org/apache/camel/karavan/model/DockerStackService.java
+++ /dev/null
@@ -1,273 +0,0 @@
-package org.apache.camel.karavan.model;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Represents a service definition for a Docker Stack (Swarm mode).
- * * Note: Assumes you also have/create DockerVolumeDefinition and DockerHealthCheckDefinition
- * classes, which would be the stack-compatible versions of your
- * DockerComposeVolume and DockerComposeHealthCheck classes.
- */
-public class DockerStackService {
-
- // Fields that are the same as DockerComposeService
- private String image;
- private String hostname;
- private String command;
- private List<String> ports = new ArrayList<>();
- private List<DockerConfigDefinition> configs = new ArrayList<>();
- private List<DockerVolumeDefinition> volumes = new ArrayList<>();
- private List<String> expose = new ArrayList<>();
- private List<String> depends_on = new ArrayList<>();
- private List<String> networks = new ArrayList<>();
- private Map<String,String> environment = new HashMap<>();
- private DockerHealthCheckDefinition healthcheck; // Renamed class
- private Map<String,String> labels = new HashMap<>();
-
- // --- Fields moved from top-level to the 'deploy' block ---
- // container_name: REMOVED (not supported in stack)
- // restart: MOVED to deploy.restart_policy
- // cpus, cpu_percent, mem_limit, mem_reservation: MOVED to deploy.resources
-
- /**
- * NEW: The 'deploy' block for Swarm-specific configuration
- */
- private Deploy deploy;
-
- public DockerStackService() {
- }
-
- // --- Getters and Setters for existing fields ---
-
- public String getImage() {
- return image;
- }
-
- public void setImage(String image) {
- this.image = image;
- }
-
- public String getHostname() {
- return hostname;
- }
-
- public void setHostname(String hostname) {
- this.hostname = hostname;
- }
-
- public String getCommand() {
- return command;
- }
-
- public void setCommand(String command) {
- this.command = command;
- }
-
- public List<String> getPorts() {
- return ports;
- }
-
- public void setPorts(List<String> ports) {
- this.ports = ports;
- }
-
- public Map<Integer, Integer> getPortsMap() {
- Map<Integer, Integer> p = new HashMap<>();
- if (ports != null && !ports.isEmpty()) {
- ports.forEach(s -> {
- String[] values = s.split(":");
- p.put(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
- });
- }
- return p;
- }
-
- public List<String> getExpose() {
- return expose;
- }
-
- public void setExpose(List<String> expose) {
- this.expose = expose;
- }
-
- public List<String> getDepends_on() {
- return depends_on;
- }
-
- public void setDepends_on(List<String> depends_on) {
- this.depends_on = depends_on;
- }
-
- public Map<String, String> getEnvironment() {
- return environment != null ? environment : new HashMap<>();
- }
-
- public List<String> getEnvironmentList() {
- return environment != null
- ? environment.entrySet().stream() .map(e -> e.getKey().concat("=").concat(e.getValue())).collect(Collectors.toList())
- : new ArrayList<>();
- }
-
- public void addEnvironment(String key, String value) {
- Map<String, String> map = getEnvironment();
- map.put(key, value);
- setEnvironment(map);
- }
-
- public void setEnvironment(Map<String, String> environment) {
- this.environment = environment;
- }
-
- public DockerHealthCheckDefinition getHealthcheck() {
- return healthcheck;
- }
-
- public void setHealthcheck(DockerHealthCheckDefinition healthcheck) {
- this.healthcheck = healthcheck;
- }
-
- public List<String> getNetworks() {
- return networks;
- }
-
- public void setNetworks(List<String> networks) {
- this.networks = networks;
- }
-
- public List<DockerConfigDefinition> getConfigs() {
- return configs;
- }
-
- public void setConfigs(List<DockerConfigDefinition> configs) {
- this.configs = configs;
- }
-
- public void addConfig(DockerConfigDefinition config) {
- this.configs.add(config);
- }
-
- public List<DockerVolumeDefinition> getVolumes() {
- return volumes;
- }
-
- public void setVolumes(List<DockerVolumeDefinition> volumes) {
- this.volumes = volumes;
- }
-
- public Map<String,String> getLabels() {
- return labels;
- }
-
- public void setLabels(Map<String,String> labels) {
- this.labels = labels;
- }
-
- public void addLabel(String key, String value) {
- this.labels.put(key, value);
- }
-
- // --- Getter and Setter for NEW 'deploy' block ---
-
- public Deploy getDeploy() {
- return deploy;
- }
-
- public void setDeploy(Deploy deploy) {
- this.deploy = deploy;
- }
-
- @Override
- public String toString() {
- return "DockerStackService {" +
- "image='" + image + '\'' +
- ", ports=" + ports +
- ", networks=" + networks +
- ", expose=" + expose +
- ", depends_on='" + depends_on + '\'' +
- ", environment=" + environment +
- ", healthcheck=" + healthcheck +
- ", volumes=" + volumes +
- ", labels=" + labels +
- ", deploy=" + deploy + // Added deploy
- '}';
- }
-
- // --- NESTED CLASSES FOR THE 'deploy' BLOCK ---
-
- /**
- * Corresponds to the 'deploy' section of a stack file.
- */
- public static class Deploy {
- private Integer replicas;
- private RestartPolicy restart_policy;
- private Resources resources;
- private Map<String, String> labels; // Note: these are *container* labels, not *service* labels
-
- // Getters and Setters
- public Integer getReplicas() { return replicas; }
- public void setReplicas(Integer replicas) { this.replicas = replicas; }
- public RestartPolicy getRestart_policy() { return restart_policy; }
- public void setRestart_policy(RestartPolicy restart_policy) { this.restart_policy = restart_policy; }
- public Resources getResources() { return resources; }
- public void setResources(Resources resources) { this.resources = resources; }
- public Map<String, String> getLabels() { return labels; }
- public void setLabels(Map<String, String> labels) { this.labels = labels; }
- }
-
- /**
- * Corresponds to 'deploy.restart_policy'
- */
- public static class RestartPolicy {
- private String condition; // e.g., "on-failure", "any"
- private String delay; // e.g., "5s"
- private Integer max_attempts;
- private String window; // e.g., "120s"
-
- // Getters and Setters
- public String getCondition() { return condition; }
- public void setCondition(String condition) { this.condition = condition; }
- public String getDelay() { return delay; }
- public void setDelay(String delay) { this.delay = delay; }
- public Integer getMax_attempts() { return max_attempts; }
- public void setMax_attempts(Integer max_attempts) { this.max_attempts = max_attempts; }
- public String getWindow() { return window; }
- public void setWindow(String window) { this.window = window; }
- }
-
- /**
- * Corresponds to 'deploy.resources'
- */
- public static class Resources {
- private ResourceLimit limits;
- private ResourceLimit reservations;
-
- // Getters and Setters
- public ResourceLimit getLimits() { return limits; }
- public void setLimits(ResourceLimit limits) { this.limits = limits; }
- public ResourceLimit getReservations() { return reservations; }
- public void setReservations(ResourceLimit reservations) { this.reservations = reservations; }
- }
-
- /**
- * Corresponds to 'deploy.resources.limits' and 'deploy.resources.reservations'
- */
- public static class ResourceLimit {
- private String cpus; // e.g., "0.50"
- private String memory; // e.g., "512M"
-
- // Getters and Setters
- public String getCpus() { return cpus; }
- public void setCpus(String cpus) { this.cpus = cpus; }
- public String getMemory() { return memory; }
- public void setMemory(String memory) { this.memory = memory; }
- }
-
- // You would also need to define these classes,
- // presumably by renaming/adapting them from your DockerCompose versions.
- // public static class DockerVolumeDefinition { /* ... */ }
- // public static class DockerHealthCheckDefinition { /* ... */ }
-}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/model/PodEvent.java b/karavan-app/src/main/java/org/apache/camel/karavan/model/PodEvent.java
index 145c0b93..a8e0a824 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/model/PodEvent.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/model/PodEvent.java
@@ -1,61 +1,11 @@
package org.apache.camel.karavan.model;
-public class PodEvent {
-
- private String id;
- private String containerName;
- private String reason;
- private String note;
- private String creationTimestamp;
-
- public PodEvent() {
- }
-
- public PodEvent(String id, String containerName, String reason, String note, String creationTimestamp) {
- this.id = id;
- this.containerName = containerName;
- this.reason = reason;
- this.note = note;
- this.creationTimestamp = creationTimestamp;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getContainerName() {
- return containerName;
- }
-
- public void setContainerName(String containerName) {
- this.containerName = containerName;
- }
-
- public String getReason() {
- return reason;
- }
-
- public void setReason(String reason) {
- this.reason = reason;
- }
-
- public String getNote() {
- return note;
- }
-
- public void setNote(String note) {
- this.note = note;
- }
-
- public String getCreationTimestamp() {
- return creationTimestamp;
- }
-
- public void setCreationTimestamp(String creationTimestamp) {
- this.creationTimestamp = creationTimestamp;
- }
-}
\ No newline at end of file
+public record PodEvent(
+ String id,
+ String containerName,
+ String reason,
+ String note,
+ String type,
+ Integer count,
+ String lastTimestamp
+) {}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/persistence/PersistenceService.java b/karavan-app/src/main/java/org/apache/camel/karavan/persistence/PersistenceService.java
index 4525d96d..45ee2f25 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/persistence/PersistenceService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/persistence/PersistenceService.java
@@ -1,6 +1,7 @@
package org.apache.camel.karavan.persistence;
import io.quarkus.vertx.ConsumeEvent;
+import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -9,22 +10,31 @@ import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import org.apache.camel.karavan.cache.AccessSession;
import org.apache.camel.karavan.cache.CacheEvent;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.List;
import java.util.Objects;
+import java.util.concurrent.atomic.AtomicLong;
import static org.apache.camel.karavan.KaravanEvents.*;
@ApplicationScoped
-public class PersistenceService {
+public class PersistenceService implements HealthCheck {
private static final Logger LOGGER = Logger.getLogger(PersistenceService.class.getName());
@Inject
EntityManager entityManager;
+ @Inject
+ EventBus eventBus;
+
+ private final AtomicLong persistFailures = new AtomicLong(0);
+
private static final String SAVE_SQL =
"INSERT INTO %s (key, type, data, last_update) " +
"VALUES (:key, :type, CAST(:data AS jsonb), :now) " +
@@ -34,7 +44,7 @@ public class PersistenceService {
"last_update = EXCLUDED.last_update";
private static final String SAVE_SESSION_SQL =
- "INSERT INTO session_state (key, type, data, expiry, last_update) " + // Added expiry
+ "INSERT INTO session_state (key, type, data, expiry, last_update) " +
"VALUES (:key, :type, CAST(:data AS jsonb), :expiry, :now) " +
"ON CONFLICT (key) DO UPDATE SET " +
"type = EXCLUDED.type, data = EXCLUDED.data, " +
@@ -85,6 +95,9 @@ public class PersistenceService {
query.executeUpdate();
} catch (Exception e) {
LOGGER.errorf(e, "Failed to persist event %s in %s with key %s", event.operation(), tableName, event.key());
+ persistFailures.incrementAndGet();
+ eventBus.publish(NOTIFICATION_ERROR, JsonObject.of("error",
+ "Failed to persist " + tableName + " " + event.key() + " — restart may lose this change"));
}
}
@@ -105,4 +118,20 @@ public class PersistenceService {
.setParameter("now", Instant.now())
.getResultList();
}
+
+ @Override
+ public HealthCheckResponse call() {
+ HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database Persistence Health");
+ long failures = persistFailures.get();
+
+ if (failures > 0) {
+ return responseBuilder.down()
+ .withData("status", "DEGRADED")
+ .withData("persistFailures", failures)
+ .withData("reason", "In-memory cache is out of sync with the database due to failed writes.")
+ .build();
+ }
+
+ return responseBuilder.up().build();
+ }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/CamelStatusScheduler.java b/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/CamelStatusScheduler.java
index 351f1e90..d7d6f4b4 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/CamelStatusScheduler.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/CamelStatusScheduler.java
@@ -26,7 +26,6 @@ import org.apache.camel.karavan.cache.ContainerType;
import org.apache.camel.karavan.cache.KaravanCache;
import org.apache.camel.karavan.model.CamelStatusRequest;
import org.apache.camel.karavan.service.ConfigService;
-import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import java.util.Map;
@@ -44,9 +43,6 @@ public class CamelStatusScheduler {
@Inject
KaravanCache karavanCache;
- @ConfigProperty(name = "karavan.environment", defaultValue = KaravanConstants.DEV)
- String environment;
-
@Inject
EventBus eventBus;
@@ -54,10 +50,11 @@ public class CamelStatusScheduler {
public void collectCamelStatuses() {
LOGGER.debug("Collect Camel Statuses");
if (ConfigService.inKubernetes()) {
- karavanCache.getPodContainerStatuses(environment).stream()
+ karavanCache.getAllPodContainerStatuses().stream()
.filter(cs -> Objects.equals(cs.getLabels().get(LABEL_KUBERNETES_RUNTIME), CAMEL_PREFIX))
.filter(cs -> Objects.equals(cs.getType(), ContainerType.devmode) || Objects.equals(cs.getType(), ContainerType.packaged))
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
+ .filter(cs -> Objects.equals(cs.getPhase(), "Running"))
.forEach(cs -> {
CamelStatusRequest csr = new CamelStatusRequest(cs.getProjectId(), cs.getContainerName());
eventBus.publish(CMD_COLLECT_CAMEL_STATUS,
@@ -65,7 +62,7 @@ public class CamelStatusScheduler {
);
});
} else {
- karavanCache.getPodContainerStatuses(environment).stream()
+ karavanCache.getAllPodContainerStatuses().stream()
.filter(Objects::nonNull)
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
.filter(cs -> Objects.equals(cs.getType(), ContainerType.devmode) || Objects.equals(cs.getType(), ContainerType.packaged))
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/DockerStatusScheduler.java b/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/DockerStatusScheduler.java
index 88dc8b1e..3bb1afb5 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/DockerStatusScheduler.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/scheduler/DockerStatusScheduler.java
@@ -55,18 +55,10 @@ public class DockerStatusScheduler {
@Scheduled(every = "{karavan.container.statistics.interval:off}", concurrentExecution = Scheduled.ConcurrentExecution.SKIP)
void collectContainersStatistics() {
if (!ConfigService.inKubernetes()) {
- if (configService.inDockerSwarmMode()) {
- List<PodContainerStatus> statusesInDocker = getServicesStatuses();
- statusesInDocker.forEach(containerStatus -> {
- eventBus.publish(CMD_COLLECT_CONTAINER_STATISTIC, JsonObject.mapFrom(containerStatus));
- });
- } else {
- List<PodContainerStatus> statusesInDocker = getContainersStatuses();
- statusesInDocker.forEach(containerStatus -> {
- eventBus.publish(CMD_COLLECT_CONTAINER_STATISTIC, JsonObject.mapFrom(containerStatus));
- });
- }
-
+ List<PodContainerStatus> statusesInDocker = getContainersStatuses();
+ statusesInDocker.forEach(containerStatus -> {
+ eventBus.publish(CMD_COLLECT_CONTAINER_STATISTIC, JsonObject.mapFrom(containerStatus));
+ });
}
}
@@ -89,18 +81,4 @@ public class DockerStatusScheduler {
});
return result;
}
-
- public List<PodContainerStatus> getServicesStatuses() {
- List<PodContainerStatus> result = new ArrayList<>();
- dockerService.getAllServices().forEach(service -> {
- var containers = dockerService.findContainersByServiceId(service.getId());
- if (containers != null) {
- containers.forEach(container -> {
- PodContainerStatus podContainerStatus = DockerUtils.getServiceStatus(service, container, environment);
- result.add(podContainerStatus);
- });
- }
- });
- return result;
- }
}
\ No newline at end of file
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/AuthService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/AuthService.java
index 3cf42533..a175697b 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/service/AuthService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/AuthService.java
@@ -30,6 +30,7 @@ public class AuthService {
public static final String ROLE_ADMIN = "platform-admin";
public static final String ROLE_DEVELOPER = "platform-developer";
public static final String ROLE_USER = "platform-user";
+ public static final String ROLE_SERVICE_ACCOUNT = "platform-service-account";
public static final String USER_ADMIN = "admin";
public static final String USER_DEVELOPER = "developer";
@@ -127,7 +128,23 @@ public class AuthService {
return new AccessSession(sessionId, username, csrf, createdAt.toEpochMilli(), Instant.now().plus(SESSION_MAX_AGE, ChronoUnit.SECONDS));
}
- public AccessSession createAndSaveSession(String username, boolean persist) throws Exception {
+ public void validateSession(String sessionId) throws Exception {
+ var accessSession = karavanCache.getAccessSession(sessionId);
+ if (accessSession == null) {
+ throw new Exception("Invalid session id " + sessionId);
+ } else if (accessSession.getExpiredAt().isBefore(Instant.now())){
+ throw new Exception("Session expired");
+ }
+ }
+
+ public void invalidateSession(String sessionId) {
+ karavanCache.deleteAccessSession(sessionId);
+ }
+
+ public AccessSession createAndSaveSession(String username, boolean persist, boolean unique) throws Exception {
+ if (unique) {
+ karavanCache.deleteAccessSessionByUsername(username);
+ }
var session = createSession(username);
karavanCache.saveAccessSession(session, persist);
return session;
@@ -143,4 +160,3 @@ public class AuthService {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
-
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/CodeService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/CodeService.java
index b926b436..749cba9b 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/service/CodeService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/CodeService.java
@@ -47,6 +47,8 @@ import java.nio.file.*;
import java.nio.file.FileSystem;
import java.time.Instant;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.apache.camel.karavan.KaravanConstants.*;
@@ -57,16 +59,16 @@ public class CodeService {
private static final Logger LOGGER = Logger.getLogger(CodeService.class.getName());
public static final String APPLICATION_PROPERTIES_FILENAME = "application.properties";
public static final String PROJECT_COMPOSE_FILENAME = "docker-compose.yaml";
- public static final String PROJECT_STACK_FILENAME = "docker-stack.yaml";
public static final String MARKDOWN_EXTENSION = ".md";
+ public static final String PROJECT_MD_FILENAME = "PROJECT.md";
public static final String PROJECT_JKUBE_EXTENSION = ".jkube.yaml";
public static final String PROJECT_DEPLOYMENT_JKUBE_FILENAME = "deployment" + PROJECT_JKUBE_EXTENSION;
private static final String DOCKER_FOLDER = "/docker/";
private static final String KUBERNETES_FOLDER = "/kubernetes/";
public static final int INTERNAL_PORT = 8080;
+ public static final int CAMEL_OBSERVABILITY_PORT = 9876;
private static final String BUILDER_POD_FRAGMENT_FILENAME = "builder.pod.jkube.yaml";
private static final String BUILDER_COMPOSE_FILENAME = "builder.docker-compose.yaml";
- private static final String BUILDER_STACK_FILENAME = "builder.docker-stack.yaml";
public static final String BUILD_SCRIPT_FILENAME = "build.sh";
public static final String JSON_EXTENSION = ".json";
public static final String YAML_EXTENSION = ".yaml";
@@ -74,6 +76,7 @@ public class CodeService {
public static final String CAMEL_YAML_EXTENSION = ".camel.yaml";
public static final String KAMELET_YAML_EXTENSION = ".kamelet.yaml";
public static final String COMPOSE_FILENAME_PREFIX= "docker-compose.";
+ private static final Pattern KAMELET_PATTERN = Pattern.compile("uri:\\s*kamelet:([^\\s'\"]+)");
@ConfigProperty(name = "karavan.environment", defaultValue = KaravanConstants.DEV)
String environment;
@@ -93,15 +96,6 @@ public class CodeService {
@Inject
Vertx vertx;
- List<String> beansTemplates = List.of("database", "messaging");
-
- public static final Map<String, String> DEFAULT_CONTAINER_RESOURCES = Map.of(
- "requests.memory", "256Mi",
- "requests.cpu", "500m",
- "limits.memory", "2048Mi",
- "limits.cpu", "2000m"
- );
-
public String getProjectDevModeImage(String projectId) {
try {
ProjectFile appProp = getApplicationProperties(projectId);
@@ -116,7 +110,7 @@ public class CodeService {
return karavanCache.getProjectFile(projectId, APPLICATION_PROPERTIES_FILENAME);
}
- public Map<String, String> getProjectFilesForDevMode(String projectId, Boolean withKamelets) {
+ public Map<String, String> getProjectFilesForDevMode(String projectId) {
Map<String, String> files = karavanCache.getProjectFiles(projectId).stream()
.filter(f -> !f.getName().endsWith(MARKDOWN_EXTENSION))
.filter(f -> !Objects.equals(f.getName(), PROJECT_COMPOSE_FILENAME))
@@ -124,10 +118,12 @@ public class CodeService {
.filter(this::isDevFile)
.collect(Collectors.toMap(ProjectFile::getName, ProjectFile::getCode));
- if (withKamelets) {
- karavanCache.getProjectFiles(ProjectFolder.Type.kamelets.name())
- .forEach(file -> files.put(file.getName(), file.getCode()));
- }
+ // add kamelets
+ List<String> usedKameletFilenames = extractKameletFileNames(files);
+ karavanCache.getProjectFiles(ProjectFolder.Type.kamelets.name())
+ .stream().filter(f -> usedKameletFilenames.contains(f.getName()))
+ .forEach(file -> files.put(file.getName(), file.getCode()));
+
return files;
}
@@ -167,18 +163,21 @@ public class CodeService {
return replaceEnvWithRuntimeProperties(code2);
}
- public String getBuilderStackFragment(String projectId, String tag) {
- ProjectFile projectFile = karavanCache.getProjectFile(ProjectFolder.Type.configuration.name(), BUILDER_STACK_FILENAME);
- var code = projectFile != null ? projectFile.getCode() : null;
- var code2 = substituteVariables(code, Map.of( "projectId", projectId, "tag", tag));
- return replaceEnvWithRuntimeProperties(code2);
- }
-
public String substituteVariables(String template, Map<String, String> variables) {
StringSubstitutor sub = new StringSubstitutor(variables);
return sub.replace(template);
}
+ public ProjectFile generateProjectMd(ProjectFolder projectFolder) {
+ String template = getTemplateText(PROJECT_MD_FILENAME);
+ String code = substituteVariables(template, Map.of(
+ "projectId", projectFolder.getProjectId(),
+ "projectName", projectFolder.getName(),
+ "packageSuffix", CodeService.getGavPackageSuffix(projectFolder.getProjectId())
+ ));
+ return new ProjectFile(PROJECT_MD_FILENAME, code, projectFolder.getProjectId(), Instant.now().getEpochSecond() * 1000L);
+ }
+
public ProjectFile generateApplicationProperties(ProjectFolder projectFolder) {
String template = getTemplateText(APPLICATION_PROPERTIES_FILENAME);
String code = substituteVariables(template, Map.of(
@@ -367,16 +366,6 @@ public class CodeService {
return new ProjectFile(PROJECT_COMPOSE_FILENAME, code, projectFolder.getProjectId(), Instant.now().getEpochSecond() * 1000L);
}
- public ProjectFile createInitialProjectStack(ProjectFolder projectFolder, int nextAvailablePort) {
- String template = getTemplateText(PROJECT_STACK_FILENAME);
- String code = substituteVariables(template, Map.of(
- "projectId", projectFolder.getProjectId(),
- "projectPort", String.valueOf(nextAvailablePort),
- "projectImage", projectFolder.getProjectId()
- ));
- return new ProjectFile(PROJECT_STACK_FILENAME, code, projectFolder.getProjectId(), Instant.now().getEpochSecond() * 1000L);
- }
-
public ProjectFile createInitialDeployment(ProjectFolder projectFolder) {
String template = getTemplateText(PROJECT_DEPLOYMENT_JKUBE_FILENAME);
return new ProjectFile(PROJECT_DEPLOYMENT_JKUBE_FILENAME, template, projectFolder.getProjectId(), Instant.now().getEpochSecond() * 1000L);
@@ -409,18 +398,6 @@ public class CodeService {
return null;
}
- public String getDockerStackFileForProject(String projectId) {
- String stackFileName = PROJECT_STACK_FILENAME;
- if (!Objects.equals(environment, DEV)) {
- stackFileName = environment + "." + PROJECT_STACK_FILENAME;
- }
- ProjectFile stack = karavanCache.getProjectFile(projectId, stackFileName);
- if (stack != null) {
- return stack.getCode();
- }
- return null;
- }
-
public void updateDockerComposeImage(String projectId, String imageName) {
ProjectFile compose = karavanCache.getProjectFile(projectId, PROJECT_COMPOSE_FILENAME);
if (compose != null) {
@@ -522,4 +499,22 @@ public class CodeService {
public String getGav() {
return gav.orElse("org.camel.karavan.demo") + ":%s:1";
}
+
+ public static List<String> extractKameletFileNames(Map<String, String> files) {
+ return files.entrySet().stream()
+ .filter(entry -> entry.getKey() != null && entry.getKey().endsWith(CAMEL_YAML_EXTENSION))
+ .map(Map.Entry::getValue)
+ .flatMap(content -> {
+ List<String> names = new ArrayList<>();
+ if (content != null) {
+ Matcher matcher = KAMELET_PATTERN.matcher(content);
+ while (matcher.find()) {
+ names.add(matcher.group(1));
+ }
+ }
+ return names.stream();
+ })
+ .map(k -> k.concat(KAMELET_YAML_EXTENSION))
+ .collect(Collectors.toList());
+ }
}
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/ComplexityService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/ComplexityService.java
index 828067fc..0b844c95 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/service/ComplexityService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/ComplexityService.java
@@ -7,7 +7,10 @@ import jakarta.inject.Inject;
import org.apache.camel.karavan.cache.KaravanCache;
import org.apache.camel.karavan.cache.ProjectFile;
import org.apache.camel.karavan.cache.ProjectFolder;
-import org.apache.camel.karavan.complexity.*;
+import org.apache.camel.karavan.complexity.ComplexityComponent;
+import org.apache.camel.karavan.complexity.ComplexityFile;
+import org.apache.camel.karavan.complexity.ComplexityProject;
+import org.apache.camel.karavan.complexity.ComplexityRoute;
import org.jboss.logging.Logger;
import org.yaml.snakeyaml.Yaml;
@@ -20,68 +23,21 @@ import static org.apache.camel.karavan.service.CodeService.CAMEL_YAML_EXTENSION;
public class ComplexityService {
private static final Logger LOGGER = Logger.getLogger(ComplexityService.class.getName());
- private static final int LIMIT_COMPLEX_ROUTES = 20;
- private static final int LIMIT_NORMAL_ROUTES = 10;
- private static final int LIMIT_COMPLEX_ROUTES_PER_FILE = 2;
- private static final int LIMIT_NORMAL_ROUTES_PER_FILE = 1;
- private static final int LIMIT_COMPLEX_PROCESSORS = 20;
- private static final int LIMIT_NORMAL_PROCESSORS = 10;
- private static final int LIMIT_COMPLEX_COMPONENTS_INT = 20;
- private static final int LIMIT_NORMAL_COMPONENTS_INT = 10;
- private static final int LIMIT_COMPLEX_COMPONENTS_EXT = 10;
- private static final int LIMIT_NORMAL_COMPONENTS_EXT = 5;
- private static final int LIMIT_COMPLEX_KAMELETS = 10;
- private static final int LIMIT_NORMAL_KAMELETS = 5;
- private static final int LIMIT_COMPLEX_RESTS = 20;
- private static final int LIMIT_NORMAL_RESTS = 10;
- private static final int LIMIT_COMPLEX_BEANS = 10;
- private static final int LIMIT_NORMAL_BEANS = 5;
- private static final int LIMIT_COMPLEX_FILES = 20;
- private static final int LIMIT_NORMAL_FILES = 10;
- private static final int LIMIT_COMPLEX_FILE_LENGTH = 5000;
- private static final int LIMIT_NORMAL_FILE_LENGTH = 2000;
@Inject
KaravanCache karavanCache;
@Inject
- CodeService codeService;
+ CamelComponentService componentService;
- private static JsonArray components;
- private JsonArray getComponents() {
- if (components == null) {
- var json = codeService.getResourceFile("/metadata/components.json");
- components = new JsonArray(json);
- }
- return components;
- }
-
- private Map<String,String> getComponentDefaultParameters(String name) {
- Map<String,String> result = new HashMap<>();
- try {
- var comps = getComponents();
- var comp = comps.stream().filter(o -> ((JsonObject)o).getJsonObject("component").getString("name").equals(name)).findFirst().orElse(JsonObject.of());
- if (comp instanceof JsonObject) {
- var properties = ((JsonObject) comp).getJsonObject("properties");
- if (properties != null) {
- for (String key: properties.fieldNames()){
- var prop = properties.getJsonObject(key);
- if (Objects.equals(prop.getString("kind"), "path") || Objects.equals(prop.getBoolean("required"), true)) {
- result.put(key, prop.getString("defaultValue"));
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
+ @Inject
+ CodeService codeService;
public List<ComplexityProject> getProjectComplexities() {
return karavanCache.getFolders().stream()
.filter(p -> Objects.equals(p.getType(), ProjectFolder.Type.integration)
|| Objects.equals(p.getType(), ProjectFolder.Type.templates)
+ || Objects.equals(p.getType(), ProjectFolder.Type.sdx)
|| Objects.equals(p.getType(), ProjectFolder.Type.kamelets)
|| Objects.equals(p.getType(), ProjectFolder.Type.documentation)
|| Objects.equals(p.getType(), ProjectFolder.Type.contracts))
@@ -106,7 +62,7 @@ public class ComplexityService {
ComplexityFile complexityFile = new ComplexityFile();
try {
complexityFile.setFileName(file.getName());
- complexityFile.setChars(Long.valueOf(file.getCode().length()).intValue());
+ complexityFile.setChars(file.getCode().length());
if (file.getName().endsWith(CAMEL_YAML_EXTENSION)) {
complexityFile.setType(ComplexityFile.Type.camel);
@@ -119,7 +75,13 @@ public class ComplexityService {
routes1.forEach(r -> r.getComponentsExt().forEach(complexityFile::addComponentExt));
routes1.forEach(r -> r.getComponentsInt().forEach(complexityFile::addComponentInt));
routes1.forEach(r -> r.getKamelets().forEach(complexityFile::addKamelet));
+ if (file.getName().equals("mcp-route.camel.yaml")) {
+ complexityProject.setMcp(true);
+ }
routes.addAll(routes1);
+
+ } else if (file.getName().equals("AgentConfig.java")) {
+ complexityProject.setAgents(true);
} else if (file.getName().equals(APPLICATION_PROPERTIES_FILENAME)) {
complexityFile.setType(ComplexityFile.Type.properties);
complexityProject.setDependencies(getDependencies(file.getCode()));
@@ -137,122 +99,74 @@ public class ComplexityService {
} else {
complexityFile.setType(ComplexityFile.Type.other);
}
- complexityProject.addFile(calculateComplexity(complexityFile));
} catch (Exception e) {
complexityFile.setError(e.getMessage());
+ } finally {
complexityProject.addFile(complexityFile);
}
}
- complexityProject.setRoutes(routes);
+ var augmentedRoutes = augmentTemplatedRoutes(routes);
+ complexityProject.setRoutes(augmentedRoutes);
} catch (Exception e) {
LOGGER.error(e);
- e.printStackTrace();
- }
- return calculateComplexity(complexityProject);
- }
-
- public ComplexityFile calculateComplexity(ComplexityFile f) {
- int processors = f.getProcessors().values().stream().mapToInt(Integer::intValue).sum();
- int kamelets = f.getKamelets().values().stream().mapToInt(Integer::intValue).sum();
- int componentsExt = f.getComponentsExt().values().stream().mapToInt(Integer::intValue).sum();
- int componentsInt = f.getComponentsInt().values().stream().mapToInt(Integer::intValue).sum();
- if (processors > LIMIT_COMPLEX_PROCESSORS) {
- f.setComplexityProcessors(Complexity.complex);
- } else if (processors > LIMIT_NORMAL_PROCESSORS) {
- f.setComplexityProcessors(Complexity.normal);
- }
- if (f.getRests() > LIMIT_COMPLEX_RESTS) {
- f.setComplexityRests(Complexity.complex);
- } else if (f.getRests() > LIMIT_NORMAL_RESTS) {
- f.setComplexityRests(Complexity.normal);
- }
- if (f.getBeans() > LIMIT_COMPLEX_BEANS) {
- f.setComplexityBeans(Complexity.complex);
- } else if (f.getBeans() > LIMIT_NORMAL_BEANS) {
- f.setComplexityBeans(Complexity.normal);
- }
- if (kamelets > LIMIT_COMPLEX_KAMELETS) {
- f.setComplexityKamelets(Complexity.complex);
- } else if (kamelets > LIMIT_NORMAL_KAMELETS) {
- f.setComplexityKamelets(Complexity.normal);
- }
- if (componentsExt > LIMIT_COMPLEX_COMPONENTS_EXT) {
- f.setComplexityComponentsExt(Complexity.complex);
- } else if (componentsExt > LIMIT_NORMAL_COMPONENTS_EXT) {
- f.setComplexityComponentsExt(Complexity.normal);
- }
- if (componentsInt > LIMIT_COMPLEX_COMPONENTS_INT) {
- f.setComplexityComponentsInt(Complexity.complex);
- } else if (componentsInt > LIMIT_NORMAL_COMPONENTS_INT) {
- f.setComplexityComponentsInt(Complexity.normal);
- }
- if (f.getChars() > LIMIT_COMPLEX_FILE_LENGTH) {
- f.setComplexityLines(Complexity.complex);
- } else if (f.getChars() > LIMIT_NORMAL_FILE_LENGTH) {
- f.setComplexityLines(Complexity.normal);
- }
-
- if (f.getRoutes() > LIMIT_COMPLEX_ROUTES_PER_FILE) {
- f.setComplexityRoutes(Complexity.complex);
- } else if (f.getRoutes() > LIMIT_NORMAL_ROUTES_PER_FILE) {
- f.setComplexityRoutes(Complexity.normal);
- }
-
- if (f.getComplexityLines().equals(Complexity.complex)
- || f.getComplexityRoutes().equals(Complexity.complex)
- || f.getComplexityRests().equals(Complexity.complex)
- || f.getComplexityBeans().equals(Complexity.complex)
- || f.getComplexityComponentsExt().equals(Complexity.complex)
- || f.getComplexityComponentsInt().equals(Complexity.complex)
- || f.getComplexityKamelets().equals(Complexity.complex)
- || f.getComplexityProcessors().equals(Complexity.complex) ) {
- f.setComplexity(Complexity.complex);
- } else if (f.getComplexityLines().equals(Complexity.normal)
- || f.getComplexityRoutes().equals(Complexity.normal)
- || f.getComplexityRests().equals(Complexity.normal)
- || f.getComplexityBeans().equals(Complexity.normal)
- || f.getComplexityComponentsExt().equals(Complexity.normal)
- || f.getComplexityComponentsInt().equals(Complexity.normal)
- || f.getComplexityKamelets().equals(Complexity.normal)
- || f.getComplexityProcessors().equals(Complexity.normal) ) {
- f.setComplexity(Complexity.normal);
+// e.printStackTrace();
}
- return f;
+ return complexityProject;
}
- public ComplexityProject calculateComplexity(ComplexityProject p) {
- if (p.getRoutes().size() > LIMIT_COMPLEX_ROUTES) {
- p.setComplexityRoute(Complexity.complex);
- } else if (p.getFiles().stream().anyMatch(f -> f.getComplexityRoutes() == Complexity.complex)) {
- p.setComplexityRoute(Complexity.complex);
- } else if (p.getRoutes().size() > LIMIT_NORMAL_ROUTES) {
- p.setComplexityRoute(Complexity.normal);
- } else if (p.getFiles().stream().anyMatch(f -> f.getComplexityRoutes() == Complexity.normal)) {
- p.setComplexityRoute(Complexity.normal);
- }
-
- if (p.getFiles().size() > LIMIT_COMPLEX_FILES) {
- p.setComplexityFiles(Complexity.complex);
- } else if (p.getFiles().size() > LIMIT_NORMAL_FILES) {
- p.setComplexityFiles(Complexity.normal);
- }
-
- if (p.getFiles().stream().anyMatch(f -> f.getComplexityRests() == Complexity.complex)) {
- p.setComplexityRest(Complexity.complex);
- } else if (p.getFiles().stream().anyMatch(f -> f.getComplexityRests() == Complexity.normal)) {
- p.setComplexityRest(Complexity.normal);
- }
-
- if (p.getFiles().stream().anyMatch(f -> f.getFileName().endsWith(".java") && f.getComplexityLines() == Complexity.complex)) {
- p.setComplexityJava(Complexity.complex);
- } else if (p.getFiles().stream().anyMatch(f -> f.getFileName().endsWith(".java") && f.getComplexityLines() == Complexity.normal)) {
- p.setComplexityJava(Complexity.normal);
- }
-
- p.setRests(p.getFiles().stream().map(ComplexityFile::getRests).mapToInt(Integer::intValue).sum());
+ private List<ComplexityRoute> getRoutesByType(List<ComplexityRoute> routes, ComplexityRoute.Type type) {
+ return new ArrayList<>(routes.stream().filter(route -> type.equals(route.getType())).toList());
+ }
- return p;
+ private List<ComplexityRoute> augmentTemplatedRoutes(List<ComplexityRoute> routes) {
+ List<ComplexityRoute> templatedRoutes = getRoutesByType(routes, ComplexityRoute.Type.TEMPLATED_ROUTE);
+ List<ComplexityRoute> augmentedRoutes = new ArrayList<>(templatedRoutes.size());
+ List<ComplexityRoute> templates = getRoutesByType(routes, ComplexityRoute.Type.ROUTE_TEMPlATE);
+ templatedRoutes.forEach(route -> {
+ var routeTemplate = templates.stream().filter(t -> t.getRouteTemplateRef().equals(route.getRouteTemplateRef())).findFirst();
+ if (routeTemplate.isPresent()) {
+ var template = routeTemplate.get();
+ List<ComplexityComponent> consumers = new ArrayList<>();
+ template.getConsumers().forEach(c -> {
+ var newC = c.copy();
+ Map<String, String> params = new HashMap<>();
+ c.getParameters().forEach((key, value) -> {
+ if (value.startsWith("{{") && value.endsWith("}}")) {
+ var paramName = value.substring(2, value.length() - 2);
+ params.put(key, route.getParameters().getOrDefault(paramName, value).toString());
+ } else {
+ params.put(key, value);
+ }
+ });
+ newC.setParameters(params);
+ consumers.add(newC);
+ });
+ route.setConsumers(consumers);
+
+ List<ComplexityComponent> producers = new ArrayList<>();
+ template.getProducers().forEach(c -> {
+ var newC = c.copy();
+ Map<String, String> params = new HashMap<>();
+ c.getParameters().forEach((key, value) -> {
+ if (value.startsWith("{{") && value.endsWith("}}")) {
+ var paramName = value.substring(2, value.length() - 2);
+ params.put(key, route.getParameters().getOrDefault(paramName, value).toString());
+ } else {
+ params.put(key, value);
+ }
+ });
+ newC.setParameters(params);
+ producers.add(newC);
+ });
+ route.setProducers(producers);
+ augmentedRoutes.add(route.copy());
+ }
+ });
+ List<ComplexityRoute> result = getRoutesByType(routes, ComplexityRoute.Type.ROUTE);
+ result.addAll(templates);
+ result.addAll(augmentedRoutes);
+ return result;
}
public List<String> getDependencies(String code) {
@@ -262,24 +176,6 @@ public class ComplexityService {
return result;
}
- public Integer getFileRoutesCount(String code) {
- int result = 0;
- Yaml yaml = new Yaml();
- List<Object> obj = yaml.load(code);
- JsonArray json = JsonArray.of(obj);
- for (Object list : json) {
- if (list instanceof JsonArray l) {
- for (Object obj1 : l) {
- var element = (JsonObject) obj1;
- if (element.containsKey("route")) {
- result++;
- }
- }
- }
- }
- return result;
- }
-
public Integer getFileBeandCount(String code) {
int result = 0;
Yaml yaml = new Yaml();
@@ -347,7 +243,11 @@ public class ComplexityService {
} else if (element.containsKey("routeTemplate")) {
var rt = element.getJsonObject("routeTemplate");
var r = rt.getJsonObject("route");
- result.add(getRouteComplexity(r, fileName));
+ var routeTemplateRef = rt.getString("id");
+ var complexity = getRouteComplexity(r, fileName);
+ complexity.setRouteTemplateRef(routeTemplateRef);
+ complexity.setType(ComplexityRoute.Type.ROUTE_TEMPlATE);
+ result.add(complexity);
} else if (element.containsKey("templatedRoute")) {
var tr = element.getJsonObject("templatedRoute");
result.add(getTemplatedRouteComplexity(tr, fileName));
@@ -371,15 +271,16 @@ public class ComplexityService {
complexity.setRouteId(route.getString("id"));
complexity.setNodePrefixId(route.getString("nodePrefixId"));
complexity.setRouteDescription(route.getString("description"));
+ complexity.setType(ComplexityRoute.Type.ROUTE);
var from = route.getJsonObject("from");
var id = from.getString("id");
var fromUri = from.getString("uri");
- var parameters = getComponentDefaultParameters(fromUri);
+ var parameters = componentService.getComponentDefaultParameters(fromUri);
var params = from.containsKey("parameters") ? from.getJsonObject("parameters") : JsonObject.of();
for (String key: params.fieldNames()) {
parameters.put(key, params.getString(key));
}
- complexity.addConsumer(new ComplexityComponent(id, fromUri, parameters));
+ complexity.addConsumer(new ComplexityComponent(id, fromUri, componentService.isComponentRemote(fromUri), parameters));
if (fromUri != null && fromUri.startsWith("kamelet:")) {
complexity.addKamelet(fromUri);
} else if (fromUri != null && (fromUri.startsWith("direct") || fromUri.startsWith("seda") || fromUri.startsWith("vertx"))) {
@@ -404,11 +305,19 @@ public class ComplexityService {
try {
complexity.setRouteId(templatedRoute.getString("routeId"));
complexity.setRouteTemplateRef(templatedRoute.getString("routeTemplateRef"));
- complexity.setTemplated(true);
+ complexity.setType(ComplexityRoute.Type.TEMPLATED_ROUTE);
complexity.setNodePrefixId(templatedRoute.getString("prefixId"));
+
+ Map<String, Object> parameters = new HashMap<>();
+ for (Object paramObj: templatedRoute.getJsonArray("parameters").getList()) {
+ try {
+ Map<String, Object> param = (Map<String, Object>) paramObj;
+ parameters.put(param.get("name").toString(), param.get("value"));
+ } catch (Exception ignored) {}
+ }
+ complexity.setParameters(parameters);
} catch (Exception e) {
LOGGER.error(e);
- e.printStackTrace();
}
return complexity;
}
@@ -431,12 +340,12 @@ public class ComplexityService {
if (stepName.equals("poll") || stepName.equals("pollEnrich")) {
var id = step.getString("id");
var uri = step.getString("uri");
- var parameters = getComponentDefaultParameters(uri);
+ var parameters = componentService.getComponentDefaultParameters(uri);
var params = step.containsKey("parameters") ? step.getJsonObject("parameters") : JsonObject.of();
for (String key: params.fieldNames()) {
parameters.put(key, params.getString(key));
}
- complexity.addConsumer(new ComplexityComponent(id, uri, parameters));
+ complexity.addConsumer(new ComplexityComponent(id, uri, componentService.isComponentRemote(uri), parameters));
if (uri != null && uri.startsWith("kamelet:")) {
complexity.addKamelet(uri);
} else if (uri != null && (uri.startsWith("direct") || uri.startsWith("seda") || uri.startsWith("vertx"))) {
@@ -448,12 +357,12 @@ public class ComplexityService {
} else if (stepName.equals("to")) {
var id = step.getString("id");
var uri = step.getString("uri");
- var parameters = getComponentDefaultParameters(uri);
+ var parameters = componentService.getComponentDefaultParameters(uri);
var params = step.containsKey("parameters") ? step.getJsonObject("parameters") : JsonObject.of();
for (String key: params.fieldNames()) {
parameters.put(key, params.getString(key));
}
- complexity.addProducer(new ComplexityComponent(id, uri, parameters));
+ complexity.addProducer(new ComplexityComponent(id, uri, componentService.isComponentRemote(uri), parameters));
if (uri != null && uri.startsWith("kamelet:")) {
complexity.addKamelet(uri);
} else if (uri != null && (uri.startsWith("direct") || uri.startsWith("seda") || uri.startsWith("vertx"))) {
diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/ConfigService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/ConfigService.java
index 9623b214..c83e2bfc 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/service/ConfigService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/ConfigService.java
@@ -22,8 +22,6 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.apache.camel.karavan.KaravanConstants;
-import org.apache.camel.karavan.cache.KaravanCache;
-import org.apache.camel.karavan.cache.ProjectFile;
import org.apache.camel.karavan.cache.ProjectFolder;
import org.apache.camel.karavan.docker.DockerService;
import org.apache.camel.karavan.kubernetes.KubernetesService;
@@ -40,7 +38,6 @@ import java.util.Objects;
import java.util.Optional;
import static org.apache.camel.karavan.KaravanConstants.DEV;
-import static org.apache.camel.karavan.service.CodeService.BUILD_SCRIPT_FILENAME;
@ApplicationScoped
public class ConfigService {
@@ -59,15 +56,9 @@ public class ConfigService {
@ConfigProperty(name = "karavan.environments")
Optional<List<String>> environments;
- @ConfigProperty(name = "karavan.shared.folder")
- Optional<String> sharedFolder;
-
@ConfigProperty(name = "karavan.secret.name", defaultValue = "karavan")
String secretName;
- @Inject
- KaravanCache karavanCache;
-
@Inject
KubernetesService kubernetesService;
@@ -80,7 +71,6 @@ public class ConfigService {
private Configuration configuration;
private static Boolean inKubernetes;
private static Boolean inDocker;
- private static Boolean inDockerSwarmMode;
void onStart(@Observes @Priority(10) StartupEvent ev) {
getConfiguration(null);
@@ -93,7 +83,6 @@ public class ConfigService {
title,
version,
inKubernetes() ? "kubernetes" : "docker",
- inDockerSwarmMode(),
environment,
secretName,
secretName,
@@ -112,13 +101,6 @@ public class ConfigService {
return inKubernetes;
}
- public boolean inDockerSwarmMode() {
- if (inDockerSwarmMode == null) {
- inDockerSwarmMode = dockerService.isInSwarmMode();
- }
- return inDockerSwarmMode;
- }
-
public static boolean inDocker() {
if (inDocker == null) {
inDocker = !inKubernetes() && Files.exists(Paths.get("/.dockerenv"));
@@ -126,57 +108,6 @@ public class ConfigService {
return inDocker;
}
- public void shareOnStartup() {
- if (ConfigService.inKubernetes() && environment.equals(DEV)) {
- LOGGER.info("Creating Configmap for " + BUILD_SCRIPT_FILENAME);
- try {
- share(BUILD_SCRIPT_FILENAME);
- } catch (Exception e) {
- var error = e.getCause() != null ? e.getCause() : e;
- LOGGER.error("Error while trying to share build.sh as Configmap", error);
- }
- }
- }
-
- public void share(String filename) throws Exception {
- if (filename != null) {
- ProjectFile f = karavanCache.getProjectFile(ProjectFolder.Type.configuration.name(), filename);
- if (f != null) {
- shareFile(f);
- }
- } else {
- for (ProjectFile f : karavanCache.getProjectFiles(ProjectFolder.Type.configuration.name())) {
- shareFile(f);
- }
- }
- }
-
- private void shareFile(ProjectFile f) throws Exception {
- var filename = f.getName();
- var parts = filename.split("\\.");
- var prefix = parts[0];
- if (environment.equals(DEV) && !getEnvs().contains(prefix)) { // no prefix AND dev env
- storeFile(f.getName(), f.getCode());
- } else if (Objects.equals(prefix, environment)) { // with prefix == env
- filename = f.getName().substring(environment.length() + 1);
- storeFile(filename, f.getCode());
- }
- }
-
- private void storeFile(String filename , String code) throws Exception {
- if (inKubernetes()) {
- kubernetesService.createConfigmap(filename, Map.of(filename, code));
- } else if (inDockerSwarmMode()){
- dockerService.createConfig(filename, code);
- } else {
- if (sharedFolder.isPresent()) {
- Files.writeString(Paths.get(sharedFolder.get(), filename), code);
- } else {
- throw new Exception("Shared folder not configured");
- }
- }
- }
-
protected List<String> getEnvs() {
return environments.orElse(List.of(DEV));
}
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 44eebaf9..76611338 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
@@ -25,11 +25,9 @@ import jakarta.inject.Inject;
import org.apache.camel.karavan.cache.*;
import org.apache.camel.karavan.docker.DockerComposeConverter;
import org.apache.camel.karavan.docker.DockerForKaravan;
-import org.apache.camel.karavan.docker.DockerStackConverter;
import org.apache.camel.karavan.kubernetes.KubernetesService;
import org.apache.camel.karavan.model.CommitResult;
import org.apache.camel.karavan.model.DockerComposeService;
-import org.apache.camel.karavan.model.DockerStackService;
import org.apache.camel.karavan.model.PathCommitDetails;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -109,7 +107,10 @@ public class ProjectService {
if (statuses.stream().noneMatch(status -> status != RemoteRefUpdate.Status.OK)) {
String commitId = commit.getId().getName();
Long lastUpdate = commit.getCommitTime() * 1000L;
- importProject(projectId);
+ // Only refresh the commited baseline for the files that were actually commited.
+ // Re-importing the whole project from Git would overwrite the working copy of
+ // the files the user did not select and revert their uncommited changes.
+ updateCommitedState(projectId, files, fileNames, commitId, lastUpdate);
return new CommitResult(p, statuses, messages, commitId, lastUpdate);
} else {
return new CommitResult(p, statuses, messages, null, null);
@@ -120,6 +121,10 @@ public class ProjectService {
}
public String runProjectInDeveloperMode(String projectId, Boolean verbose, Boolean compile, Map<String, String> labels, Map<String, String> envVars, Boolean appOnly) throws Exception {
+ var extendedEnvVars = new HashMap<>(envVars);
+ var session = authService.createAndSaveSession(projectId, false, true);
+ extendedEnvVars.put(ENV_VAR_BUILDER_SESSION_ID, session.sessionId);
+ extendedEnvVars.put(ENV_VAR_PROJECT_ID, projectId);
PodContainerStatus status = karavanCache.getDevModePodContainerStatus(projectId, environment);
if (status == null) {
status = PodContainerStatus.createDevMode(projectId, environment);
@@ -128,19 +133,13 @@ public class ProjectService {
status.setInTransit(true);
eventBus.publish(POD_CONTAINER_UPDATED, JsonObject.mapFrom(status));
- Map<String, String> files = codeService.getProjectFilesForDevMode(projectId, true)
- .entrySet().stream().filter(e -> !appOnly || APPLICATION_PROPERTIES_FILENAME.equals(e.getKey()))
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
String projectDevmodeImage = codeService.getProjectDevModeImage(projectId);
if (ConfigService.inKubernetes()) {
String deploymentFragment = codeService.getDeploymentFragment(projectId);
- kubernetesService.runDevModeContainer(projectId, verbose, compile, files, projectDevmodeImage, deploymentFragment, labels, envVars);
- } else if (configService.inDockerSwarmMode()) {
- DockerStackService stack = getProjectDockerStackService(projectId);
- dockerForKaravan.runProjectInDevMode(projectId, verbose, compile, stack, projectDevmodeImage, labels, envVars);
+ kubernetesService.runDevModeContainer(projectId, verbose, compile, projectDevmodeImage, deploymentFragment, labels, extendedEnvVars);
} else {
DockerComposeService compose = getProjectDockerComposeService(projectId);
- dockerForKaravan.runProjectInDevMode(projectId, verbose, compile, compose, files, projectDevmodeImage, labels, envVars);
+ dockerForKaravan.runProjectInDevMode(projectId, verbose, compile, compose, projectDevmodeImage, labels, extendedEnvVars);
}
return projectId;
} else {
@@ -153,17 +152,11 @@ public class ProjectService {
? tag
: Instant.now().toString().substring(0, 19).replace(":", "-");
var name = projectFolder.getProjectId() + "-builder";
- var session = authService.createAndSaveSession(name, false);
+ var session = authService.createAndSaveSession(name, false, true);
if (ConfigService.inKubernetes()) {
String podFragment = codeService.getBuilderPodFragment();
podFragment = codeService.substituteVariables(podFragment, Map.of( "projectId", projectFolder.getProjectId(), "tag", tag));
kubernetesService.runBuildProject(projectFolder.getProjectId(), podFragment, Map.of(ENV_VAR_BUILDER_SESSION_ID, session.getSessionId()));
- } else if (configService.inDockerSwarmMode()) {
- String stackFragment = codeService.getBuilderStackFragment(projectFolder.getProjectId(), tag);
- DockerStackService stack = DockerStackConverter.fromCode(stackFragment, name);
- stack.addEnvironment(ENV_VAR_RUN_IN_BUILD_MODE, "true");
- stack.addEnvironment(ENV_VAR_BUILDER_SESSION_ID, session.getSessionId());
- dockerForKaravan.runBuildProject(projectFolder, stack, tag);
} else {
Map<String, String> sshFiles = codeService.getSshFiles();
String script = codeService.getBuilderScript();
@@ -175,6 +168,29 @@ public class ProjectService {
}
}
+ /**
+ * Aligns the commited state of a project with a commit that has just been pushed,
+ * without touching the working copy of the files.
+ * <p>
+ * {@code commitedFiles} is the snapshot that was written to the Git working folder,
+ * so for every selected file name its content is exactly what has been commited.
+ */
+ private void updateCommitedState(String projectId, List<ProjectFile> commitedFiles, List<String> fileNames, String commitId, Long commitTime) {
+ karavanCache.saveProjectCommited(new ProjectFolderCommited(projectId, commitId, commitTime));
+ var commitedNames = new HashSet<>(fileNames);
+ commitedFiles.stream()
+ .filter(file -> commitedNames.contains(file.getName()))
+ .forEach(file -> {
+ var fileCommited = ProjectFileCommited.fromFile(file, commitId);
+ fileCommited.setCommitTime(commitTime);
+ karavanCache.saveProjectFileCommited(fileCommited);
+ });
+ // Files removed from the project are deleted from the repository by the same commit
+ karavanCache.getProjectFilesCommited(projectId).stream()
+ .filter(fileCommited -> commitedFiles.stream().noneMatch(file -> Objects.equals(file.getName(), fileCommited.getName())))
+ .forEach(fileCommited -> karavanCache.deleteProjectFileCommited(projectId, fileCommited.getName()));
+ }
+
public void importProject(String projectId) throws Exception {
LOGGER.info("Import project from Git " + projectId);
List<PathCommitDetails> pathCommitDetails = gitService.readProjectFromRepository(projectId);
@@ -222,12 +238,6 @@ public class ProjectService {
return DockerComposeConverter.fromCode(composeCode, projectId);
}
- public DockerStackService getProjectDockerStackService(String projectId) {
- String stackTemplate = codeService.getDockerStackFileForProject(projectId);
- String stackCode = codeService.replaceEnvWithRuntimeProperties(stackTemplate);
- return DockerStackConverter.fromCode(stackCode, projectId);
- }
-
private void modifyPropertyFileOnProjectCopy(ProjectFile propertyFile, ProjectFolder sourceProjectFolder, ProjectFolder projectFolder) {
String fileContent = propertyFile.getCode();
@@ -266,14 +276,16 @@ public class ProjectService {
throw new Exception("Project with id " + projectFolder.getProjectId() + " already exists");
} else {
karavanCache.saveProject(projectFolder, true);
+
ProjectFile appProp = codeService.generateApplicationProperties(projectFolder);
karavanCache.saveProjectFile(appProp, null, true);
+
+ ProjectFile projectMD = codeService.generateProjectMd(projectFolder);
+ karavanCache.saveProjectFile(projectMD, null, true);
+
if (!ConfigService.inKubernetes()) {
var port = getMaxPortMappedInProjects() + 1;
- ProjectFile projectCompose =
- configService.inDockerSwarmMode()
- ? codeService.createInitialProjectStack(projectFolder, port)
- : codeService.createInitialProjectCompose(projectFolder, port);
+ ProjectFile projectCompose = codeService.createInitialProjectCompose(projectFolder, port);
karavanCache.saveProjectFile(projectCompose, null, true);
} else {
ProjectFile projectDeployment = codeService.createInitialDeployment(projectFolder);
@@ -298,7 +310,6 @@ public class ProjectService {
// Copy files from the source and make necessary modifications
Map<String, ProjectFile> filesMap = karavanCache.getProjectFiles(sourceProjectId).stream()
.filter(f -> !Objects.equals(f.getName(), PROJECT_COMPOSE_FILENAME))
- .filter(f -> !Objects.equals(f.getName(), PROJECT_STACK_FILENAME))
.filter(f -> !Objects.equals(f.getName(), PROJECT_DEPLOYMENT_JKUBE_FILENAME))
.collect(Collectors.toMap(
f -> GroupedKey.create(projectFolder.getProjectId(), DEV, f.getName()),