Skip to content

Commit b290b0a

Browse files
committed
Fix Kubernetes listeners
1 parent bca0d0e commit b290b0a

File tree

6 files changed

+57
-46
lines changed

6 files changed

+57
-46
lines changed

karavan-app/src/main/java/org/apache/camel/karavan/KaravanCache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public PodContainerStatus getPodContainerStatus(String projectId, String env, St
253253
return getPodContainerStatus(GroupedKey.create(projectId, env, containerName));
254254
}
255255

256+
public PodContainerStatus getPodContainerStatus(String containerName, String env) {
257+
return getPodContainerStatuses(env).stream().filter(el -> Objects.equals(el.getContainerName(), containerName)).findFirst().orElse(null);
258+
}
259+
256260
public PodContainerStatus getPodContainerStatus(String key) {
257261
return podContainerStatuses.get(key);
258262
}

karavan-app/src/main/java/org/apache/camel/karavan/api/ContainerResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class ContainerResource {
7070
@Produces(MediaType.APPLICATION_JSON)
7171
public List<PodContainerStatus> getAllContainerStatuses() throws Exception {
7272
return karavanCache.getPodContainerStatuses().stream()
73-
.sorted(Comparator.comparing(PodContainerStatus::getProjectId))
73+
.sorted(Comparator.comparing(PodContainerStatus::getProjectId, Comparator.nullsLast(String::compareTo)))
7474
.collect(Collectors.toList());
7575
}
7676

karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ private Pod getBuilderPod(String name, Map<String, String> labels, String config
175175
.withProtocol("TCP")
176176
.build();
177177

178-
179178
List<VolumeMount> volumeMounts = new ArrayList<>();
180179
volumeMounts.add(new VolumeMountBuilder().withName(BUILD_SCRIPT_VOLUME_NAME).withMountPath("/karavan/builder").withReadOnly(true).build());
181180
if (hasDockerConfigSecret) {
@@ -262,6 +261,9 @@ public void startDeployment(String resources, Map<String, String> labels) {
262261
list.getItems().forEach(item -> {
263262
if (labels != null ) {
264263
item.getMetadata().getLabels().putAll(labels);
264+
if (item instanceof Deployment deployment) {
265+
deployment.getSpec().getTemplate().getMetadata().getLabels().putAll(labels);
266+
}
265267
}
266268
client.resource(item).inNamespace(getNamespace()).serverSideApply();
267269
});

karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/KubernetesStatusService.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import jakarta.enterprise.inject.Produces;
3232
import jakarta.inject.Inject;
3333
import org.apache.camel.karavan.KaravanConstants;
34+
import org.apache.camel.karavan.model.ContainerType;
3435
import org.apache.camel.karavan.service.ConfigService;
3536
import org.eclipse.microprofile.config.inject.ConfigProperty;
3637
import org.eclipse.microprofile.health.HealthCheck;
@@ -39,9 +40,12 @@
3940
import org.jboss.logging.Logger;
4041

4142
import java.io.IOException;
42-
import java.util.*;
43+
import java.util.ArrayList;
44+
import java.util.List;
45+
import java.util.Map;
46+
import java.util.Optional;
4347

44-
import static org.apache.camel.karavan.KaravanConstants.CAMEL_PREFIX;
48+
import static org.apache.camel.karavan.KaravanConstants.LABEL_TYPE;
4549

4650
@Default
4751
@Readiness
@@ -91,21 +95,19 @@ public void startInformers() {
9195
stopInformers();
9296
LOGGER.info("Starting Kubernetes Informers");
9397

94-
Map<String, String> labels = getRuntimeLabels();
9598
KubernetesClient client = kubernetesClient();
9699

97-
SharedIndexInformer<Deployment> deploymentInformer = client.apps().deployments().inNamespace(getNamespace())
98-
.withLabels(labels).inform();
100+
String[] values = new String[]{ContainerType.project.name(), ContainerType.build.name(), ContainerType.devmode.name(), ContainerType.devservice.name(), ContainerType.internal.name(),};
101+
102+
SharedIndexInformer<Deployment> deploymentInformer = client.apps().deployments().inNamespace(getNamespace()).withLabelIn(LABEL_TYPE, values).inform();
99103
deploymentInformer.addEventHandlerWithResyncPeriod(new DeploymentEventHandler(this, eventBus), 30 * 1000L);
100104
informers.add(deploymentInformer);
101105

102-
SharedIndexInformer<Service> serviceInformer = client.services().inNamespace(getNamespace())
103-
.withLabels(labels).inform();
106+
SharedIndexInformer<Service> serviceInformer = client.services().inNamespace(getNamespace()).withLabelIn(LABEL_TYPE, values).inform();
104107
serviceInformer.addEventHandlerWithResyncPeriod(new ServiceEventHandler(this, eventBus), 30 * 1000L);
105108
informers.add(serviceInformer);
106109

107-
SharedIndexInformer<Pod> podRunInformer = client.pods().inNamespace(getNamespace())
108-
.withLabels(labels).inform();
110+
SharedIndexInformer<Pod> podRunInformer = client.pods().inNamespace(getNamespace()).withLabelIn(LABEL_TYPE, values).inform();
109111
podRunInformer.addEventHandlerWithResyncPeriod(new PodEventHandler( this, eventBus), 30 * 1000L);
110112
informers.add(podRunInformer);
111113

@@ -145,14 +147,10 @@ public String getNamespace() {
145147
return namespace;
146148
}
147149

148-
private Map<String, String> getRuntimeLabels() {
149-
Map<String, String> labels = new HashMap<>();
150-
labels.put(isOpenshift() ? "app.openshift.io/runtime" : "app.kubernetes.io/runtime", CAMEL_PREFIX);
151-
return labels;
152-
}
153-
154-
public boolean isOpenshift() {
155-
return isOpenShift.isPresent() && isOpenShift.get();
150+
public Deployment getDeployment(String name) {
151+
try (KubernetesClient client = kubernetesClient()) {
152+
return client.apps().deployments().inNamespace(getNamespace()).withName(name).get();
153+
}
156154
}
157155

158156
public String getCluster() {

karavan-app/src/main/java/org/apache/camel/karavan/kubernetes/PodEventHandler.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.fabric8.kubernetes.api.model.Pod;
2222
import io.fabric8.kubernetes.api.model.Quantity;
2323
import io.fabric8.kubernetes.api.model.ResourceRequirements;
24+
import io.fabric8.kubernetes.api.model.apps.Deployment;
2425
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
2526
import io.vertx.core.json.JsonObject;
2627
import io.vertx.mutiny.core.eventbus.EventBus;
@@ -103,15 +104,21 @@ public void onDelete(Pod pod, boolean deletedFinalStateUnknown) {
103104

104105

105106
public PodContainerStatus getPodStatus(Pod pod) {
106-
String deployment = pod.getMetadata().getLabels().get("app");
107-
String projectId = deployment != null ? deployment : pod.getMetadata().getLabels().get(LABEL_PROJECT_ID);
108-
String camel = deployment != null ? deployment : pod.getMetadata().getLabels().get(LABEL_KUBERNETES_RUNTIME);
109-
String runtime = deployment != null ? deployment : pod.getMetadata().getLabels().get(LABEL_CAMEL_RUNTIME);
107+
String appName = pod.getMetadata().getLabels().get("app");
108+
String projectId = pod.getMetadata().getLabels().get(LABEL_PROJECT_ID);
109+
String camel = pod.getMetadata().getLabels().get(LABEL_KUBERNETES_RUNTIME);
110+
String runtime = pod.getMetadata().getLabels().get(LABEL_CAMEL_RUNTIME);
110111
String type = pod.getMetadata().getLabels().get(LABEL_TYPE);
111112
String commit = pod.getMetadata().getAnnotations().get(ANNOTATION_COMMIT);
112-
ContainerType containerType = deployment != null
113-
? ContainerType.project
114-
: (type != null ? ContainerType.valueOf(type) : ContainerType.unknown);
113+
if (appName != null) {
114+
Deployment deployment = kubernetesStatusService.getDeployment(appName);
115+
projectId = deployment.getMetadata().getName();
116+
camel = deployment.getMetadata().getLabels().get(LABEL_KUBERNETES_RUNTIME);
117+
runtime = deployment.getMetadata().getLabels().get(LABEL_CAMEL_RUNTIME);
118+
type = deployment.getMetadata().getLabels().get(LABEL_TYPE);
119+
commit = deployment.getMetadata().getAnnotations().get(ANNOTATION_COMMIT);
120+
}
121+
ContainerType containerType = type != null ? ContainerType.valueOf(type) : ContainerType.unknown;
115122
try {
116123
boolean ready = pod.getStatus().getConditions().stream().anyMatch(c -> c.getType().equals("Ready") && c.getStatus().equals("True"));
117124
boolean running = Objects.equals(pod.getStatus().getPhase(), "Running");

karavan-app/src/main/java/org/apache/camel/karavan/scheduler/CamelStatusScheduler.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ public class CamelStatusScheduler {
5252
@Scheduled(every = "{karavan.camel.status.interval}", concurrentExecution = Scheduled.ConcurrentExecution.SKIP)
5353
public void collectCamelStatuses() {
5454
LOGGER.debug("Collect Camel Statuses");
55-
if (ConfigService.inKubernetes()) {
56-
karavanCache.getPodContainerStatuses(environment).stream()
57-
.filter(cs -> Objects.equals(cs.getLabels().get(LABEL_KUBERNETES_RUNTIME), CAMEL_PREFIX))
58-
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
59-
.forEach(cs -> {
60-
CamelStatusRequest csr = new CamelStatusRequest(cs.getProjectId(), cs.getContainerName());
61-
eventBus.publish(CMD_COLLECT_CAMEL_STATUS,
62-
JsonObject.mapFrom(Map.of("containerStatus", cs, "camelStatusRequest", csr))
63-
);
64-
});
65-
} else {
66-
karavanCache.getPodContainerStatuses(environment).stream()
67-
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
68-
.forEach(cs -> {
69-
CamelStatusRequest csr = new CamelStatusRequest(cs.getProjectId(), cs.getContainerName());
70-
eventBus.publish(CMD_COLLECT_CAMEL_STATUS,
71-
JsonObject.mapFrom(Map.of("containerStatus", cs, "camelStatusRequest", csr))
72-
);
73-
});
74-
}
55+
if (ConfigService.inKubernetes()) {
56+
karavanCache.getPodContainerStatuses(environment).stream()
57+
.filter(cs -> Objects.equals(cs.getLabels().get(LABEL_KUBERNETES_RUNTIME), CAMEL_PREFIX))
58+
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
59+
.forEach(cs -> {
60+
CamelStatusRequest csr = new CamelStatusRequest(cs.getProjectId(), cs.getContainerName());
61+
eventBus.publish(CMD_COLLECT_CAMEL_STATUS,
62+
JsonObject.mapFrom(Map.of("containerStatus", cs, "camelStatusRequest", csr))
63+
);
64+
});
65+
} else {
66+
karavanCache.getPodContainerStatuses(environment).stream()
67+
.filter(cs -> Objects.equals(cs.getCamelRuntime(), KaravanConstants.CamelRuntime.CAMEL_MAIN.getValue()))
68+
.forEach(cs -> {
69+
CamelStatusRequest csr = new CamelStatusRequest(cs.getProjectId(), cs.getContainerName());
70+
eventBus.publish(CMD_COLLECT_CAMEL_STATUS,
71+
JsonObject.mapFrom(Map.of("containerStatus", cs, "camelStatusRequest", csr))
72+
);
73+
});
74+
}
7575
}
7676
}

0 commit comments

Comments
 (0)