Skip to content

Commit

Permalink
Fix frédo
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Jun 10, 2024
1 parent 40cd760 commit 7dda39d
Showing 1 changed file with 65 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@
import fr.insee.onyxia.model.project.Project;
import fr.insee.onyxia.model.region.Region;
import fr.insee.onyxia.model.service.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration;
import io.github.inseefrlab.helmwrapper.model.HelmInstaller;
import io.github.inseefrlab.helmwrapper.model.HelmLs;
import io.github.inseefrlab.helmwrapper.model.HelmReleaseInfo;
import io.github.inseefrlab.helmwrapper.service.HelmInstallService;
import io.github.inseefrlab.helmwrapper.service.HelmInstallService.MultipleServiceFound;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
Expand Down Expand Up @@ -331,19 +331,17 @@ public Service getUserService(Region region, Project project, User user, String
}

@Override
public Service getUserServiceDetails(Region region, Project project, User user, String serviceId)
public Service getUserServiceDetails(
Region region, Project project, User user, String serviceId)
throws MultipleServiceFound, ParseException {
if (serviceId.startsWith("/")) {
serviceId = serviceId.substring(1);
}
String namespace = kubernetesService.determineNamespaceAndCreateIfNeeded(region, project, user);
String namespace =
kubernetesService.determineNamespaceAndCreateIfNeeded(region, project, user);
HelmLs result =
getHelmInstallService()
.getAppById(
getHelmConfiguration(region, user),
serviceId,
namespace
);
.getAppById(getHelmConfiguration(region, user), serviceId, namespace);
HelmReleaseInfo helmReleaseInfo =
getHelmInstallService()
.getAll(
Expand All @@ -354,33 +352,36 @@ public Service getUserServiceDetails(Region region, Project project, User user,
KubernetesClient client = kubernetesClientProvider.getUserClient(region, user);
List<HasMetadata> hasMetadata;
try (InputStream inputStream =
new ByteArrayInputStream(helmReleaseInfo.getManifest().getBytes(StandardCharsets.UTF_8))) {
new ByteArrayInputStream(
helmReleaseInfo.getManifest().getBytes(StandardCharsets.UTF_8))) {
hasMetadata = client.load(inputStream).items();
} catch (IOException e) {
throw new RuntimeException("Exception during loading manifest", e);
}
List<Object> manifests = new ArrayList<Object>(hasMetadata);
List<Object> manifests = new ArrayList<Object>(hasMetadata);
res.setTemplates(manifests);
List<Map<String, Object>> podInfoList = new ArrayList<>();

List<Pod> pods = client.pods().inNamespace(namespace).list().getItems();
for (Pod pod : pods) {
Map<String, Object> podInfo = new HashMap<>();
podInfo.put("podName", pod.getMetadata().getName());
podInfo.put("owners", getOwnerReferences(pod,client));
podInfo.put("owners", getOwnerReferences(pod, client));
podInfo.put("containers", getContainers(pod));
podInfoList.add(podInfo);
}
res.setPodsAndOwners(filterPodsByTopLevelOwnerReferences(podInfoList, hasMetadata, namespace, client));
res.setPodsAndOwners(
filterPodsByTopLevelOwnerReferences(podInfoList, hasMetadata, namespace, client));
return res;
}

private List<Map<String, Object>> getContainers(Pod pod) {
List<Map<String, Object>> containersInfo = new ArrayList<>();
List<Container> containers = pod.getSpec().getContainers();
List<Container> initContainers = pod.getSpec().getInitContainers();
List<io.fabric8.kubernetes.api.model.Container> containers = pod.getSpec().getContainers();
List<io.fabric8.kubernetes.api.model.Container> initContainers =
pod.getSpec().getInitContainers();

for (Container container : containers) {
for (io.fabric8.kubernetes.api.model.Container container : containers) {
Map<String, Object> containerInfo = new HashMap<>();
containerInfo.put("name", container.getName());
containerInfo.put("image", container.getImage());
Expand All @@ -390,7 +391,7 @@ private List<Map<String, Object>> getContainers(Pod pod) {
containersInfo.add(containerInfo);
}

for (Container initContainer : initContainers) {
for (io.fabric8.kubernetes.api.model.Container initContainer : initContainers) {
Map<String, Object> initContainerInfo = new HashMap<>();
initContainerInfo.put("name", initContainer.getName());
initContainerInfo.put("image", initContainer.getImage());
Expand All @@ -403,7 +404,8 @@ private List<Map<String, Object>> getContainers(Pod pod) {
return containersInfo;
}

private List<Map<String, Object>> getOwnerReferences(HasMetadata resource, KubernetesClient client) {
private List<Map<String, Object>> getOwnerReferences(
HasMetadata resource, KubernetesClient client) {
List<Map<String, Object>> owners = new ArrayList<>();

List<OwnerReference> ownerReferences = resource.getMetadata().getOwnerReferences();
Expand All @@ -413,12 +415,14 @@ private List<Map<String, Object>> getOwnerReferences(HasMetadata resource, Kuber
String name = ownerReference.getName();

// Fetch the owner resource
HasMetadata ownerResource = fetchOwnerResource(resource.getMetadata().getNamespace(), kind, name, client);
HasMetadata ownerResource =
fetchOwnerResource(
resource.getMetadata().getNamespace(), kind, name, client);
if (ownerResource != null) {
Map<String, Object> ownerInfo = new HashMap<>();
ownerInfo.put("kind", kind);
ownerInfo.put("name", name);
ownerInfo.put("owners", getOwnerReferences(ownerResource,client));
ownerInfo.put("owners", getOwnerReferences(ownerResource, client));

owners.add(ownerInfo);
}
Expand All @@ -428,7 +432,8 @@ private List<Map<String, Object>> getOwnerReferences(HasMetadata resource, Kuber
return owners;
}

private HasMetadata fetchOwnerResource(String namespace, String kind, String name, KubernetesClient client) {
private HasMetadata fetchOwnerResource(
String namespace, String kind, String name, KubernetesClient client) {
switch (kind) {
case "ReplicaSet":
return client.apps().replicaSets().inNamespace(namespace).withName(name).get();
Expand All @@ -445,27 +450,48 @@ private HasMetadata fetchOwnerResource(String namespace, String kind, String nam
}
}

private List<Map<String, Object>> filterPodsByTopLevelOwnerReferences(List<Map<String, Object>> podInfoList, List<HasMetadata> resourceList, String namespace,KubernetesClient client) {
Set<String> resourceNamesAndKinds = resourceList.stream()
.map(resource -> resource.getKind() + "/" + resource.getMetadata().getName())
.collect(Collectors.toSet());
private List<Map<String, Object>> filterPodsByTopLevelOwnerReferences(
List<Map<String, Object>> podInfoList,
List<HasMetadata> resourceList,
String namespace,
KubernetesClient client) {
Set<String> resourceNamesAndKinds =
resourceList.stream()
.map(
resource ->
resource.getKind() + "/" + resource.getMetadata().getName())
.collect(Collectors.toSet());

return podInfoList.stream()
.filter(podInfo -> {
List<Map<String, Object>> owners = (List<Map<String, Object>>) podInfo.get("owners");
return owners.stream()
.anyMatch(owner -> resourceNamesAndKinds.contains(findTopLevelOwner((String) owner.get("kind"), (String) owner.get("name"), namespace, client)));
})
.filter(
podInfo -> {
List<Map<String, Object>> owners =
(List<Map<String, Object>>) podInfo.get("owners");
return owners.stream()
.anyMatch(
owner ->
resourceNamesAndKinds.contains(
findTopLevelOwner(
(String) owner.get("kind"),
(String) owner.get("name"),
namespace,
client)));
})
.collect(Collectors.toList());
}

private String findTopLevelOwner(String kind, String name, String namespace,KubernetesClient client) {
HasMetadata resource = fetchOwnerResource(namespace, kind, name,client);
private String findTopLevelOwner(
String kind, String name, String namespace, KubernetesClient client) {
HasMetadata resource = fetchOwnerResource(namespace, kind, name, client);
while (resource != null && !resource.getMetadata().getOwnerReferences().isEmpty()) {
OwnerReference ownerReference = resource.getMetadata().getOwnerReferences().get(0);
resource = fetchOwnerResource(namespace, ownerReference.getKind(), ownerReference.getName(),client);
resource =
fetchOwnerResource(
namespace, ownerReference.getKind(), ownerReference.getName(), client);
}
return resource != null ? resource.getKind() + "/" + resource.getMetadata().getName() : null;
return resource != null
? resource.getKind() + "/" + resource.getMetadata().getName()
: null;
}

@Override
Expand Down

0 comments on commit 7dda39d

Please sign in to comment.