From 7dda39d82b2095f4d9a3650432aa23f38cfe06dd Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Mon, 10 Jun 2024 17:19:27 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20fr=C3=A9do?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/services/impl/HelmAppsService.java | 104 +++++++++++------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/onyxia-api/src/main/java/fr/insee/onyxia/api/services/impl/HelmAppsService.java b/onyxia-api/src/main/java/fr/insee/onyxia/api/services/impl/HelmAppsService.java index 74445b85..0cbca3df 100644 --- a/onyxia-api/src/main/java/fr/insee/onyxia/api/services/impl/HelmAppsService.java +++ b/onyxia-api/src/main/java/fr/insee/onyxia/api/services/impl/HelmAppsService.java @@ -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; @@ -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( @@ -354,33 +352,36 @@ public Service getUserServiceDetails(Region region, Project project, User user, KubernetesClient client = kubernetesClientProvider.getUserClient(region, user); List 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 manifests = new ArrayList(hasMetadata); + List manifests = new ArrayList(hasMetadata); res.setTemplates(manifests); List> podInfoList = new ArrayList<>(); - + List pods = client.pods().inNamespace(namespace).list().getItems(); for (Pod pod : pods) { Map 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> getContainers(Pod pod) { List> containersInfo = new ArrayList<>(); - List containers = pod.getSpec().getContainers(); - List initContainers = pod.getSpec().getInitContainers(); + List containers = pod.getSpec().getContainers(); + List initContainers = + pod.getSpec().getInitContainers(); - for (Container container : containers) { + for (io.fabric8.kubernetes.api.model.Container container : containers) { Map containerInfo = new HashMap<>(); containerInfo.put("name", container.getName()); containerInfo.put("image", container.getImage()); @@ -390,7 +391,7 @@ private List> getContainers(Pod pod) { containersInfo.add(containerInfo); } - for (Container initContainer : initContainers) { + for (io.fabric8.kubernetes.api.model.Container initContainer : initContainers) { Map initContainerInfo = new HashMap<>(); initContainerInfo.put("name", initContainer.getName()); initContainerInfo.put("image", initContainer.getImage()); @@ -403,7 +404,8 @@ private List> getContainers(Pod pod) { return containersInfo; } - private List> getOwnerReferences(HasMetadata resource, KubernetesClient client) { + private List> getOwnerReferences( + HasMetadata resource, KubernetesClient client) { List> owners = new ArrayList<>(); List ownerReferences = resource.getMetadata().getOwnerReferences(); @@ -413,12 +415,14 @@ private List> 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 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); } @@ -428,7 +432,8 @@ private List> 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(); @@ -445,27 +450,48 @@ private HasMetadata fetchOwnerResource(String namespace, String kind, String nam } } - private List> filterPodsByTopLevelOwnerReferences(List> podInfoList, List resourceList, String namespace,KubernetesClient client) { - Set resourceNamesAndKinds = resourceList.stream() - .map(resource -> resource.getKind() + "/" + resource.getMetadata().getName()) - .collect(Collectors.toSet()); + private List> filterPodsByTopLevelOwnerReferences( + List> podInfoList, + List resourceList, + String namespace, + KubernetesClient client) { + Set resourceNamesAndKinds = + resourceList.stream() + .map( + resource -> + resource.getKind() + "/" + resource.getMetadata().getName()) + .collect(Collectors.toSet()); return podInfoList.stream() - .filter(podInfo -> { - List> owners = (List>) podInfo.get("owners"); - return owners.stream() - .anyMatch(owner -> resourceNamesAndKinds.contains(findTopLevelOwner((String) owner.get("kind"), (String) owner.get("name"), namespace, client))); - }) + .filter( + podInfo -> { + List> owners = + (List>) 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