110 lines
3.5 KiB
Java
110 lines
3.5 KiB
Java
package dev.dinauer.service;
|
|
|
|
import dev.dinauer.utils.ClientProvider;
|
|
import io.fabric8.kubernetes.api.model.Pod;
|
|
import io.fabric8.kubernetes.api.model.apps.DaemonSet;
|
|
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
|
|
import io.fabric8.kubernetes.client.Watch;
|
|
import io.fabric8.kubernetes.client.Watcher;
|
|
import io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
@ApplicationScoped
|
|
public class PodService implements ResourceService<Pod>
|
|
{
|
|
@Inject
|
|
ClientProvider clientProvider;
|
|
|
|
@Override
|
|
public void delete(String name, String namespace)
|
|
{
|
|
Optional<Pod> podOptional = findOptionalByNameAndNamespace(name, namespace);
|
|
if(podOptional.isPresent())
|
|
{
|
|
Pod pod = podOptional.get();
|
|
clientProvider.getClient()
|
|
.pods()
|
|
.inNamespace(pod.getMetadata().getNamespace())
|
|
.withName(pod.getMetadata().getName())
|
|
.delete();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Pod findByNameAndNamespace(String name, String namespace)
|
|
{
|
|
return clientProvider.getClient().pods().inNamespace(namespace).withName(name).get();
|
|
}
|
|
|
|
public List<Pod> findByNamespace(String namespace)
|
|
{
|
|
if (namespace.equals("_all"))
|
|
{
|
|
return clientProvider.getClient().pods().inAnyNamespace().list().getItems();
|
|
}
|
|
return clientProvider.getClient().pods().inNamespace(namespace).list().getItems();
|
|
}
|
|
|
|
public List<Pod> findByStatefulSet(String name, String namespace)
|
|
{
|
|
try(AppsAPIGroupDSL apps = clientProvider.getClient().apps())
|
|
{
|
|
StatefulSet set = apps.statefulSets().inNamespace(namespace).withName(name).get();
|
|
if (set != null)
|
|
{
|
|
return findByNamespaceAndLabels(namespace, set.getSpec().getSelector().getMatchLabels());
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public List<Pod> findByNamespaceAndLabels(String namespace, Map<String, String> labels)
|
|
{
|
|
return clientProvider.getClient().pods().inNamespace(namespace).withLabels(labels).list().getItems();
|
|
}
|
|
|
|
public List<Pod> findByLabels(Map<String, String> labels)
|
|
{
|
|
return clientProvider.getClient().pods().inAnyNamespace().withLabels(labels).list().getItems();
|
|
}
|
|
|
|
public List<Pod> findAll()
|
|
{
|
|
return clientProvider.getClient().pods().inAnyNamespace().list().getItems();
|
|
}
|
|
|
|
@Override
|
|
public Optional<Pod> findOptionalByNameAndNamespace(String name, String namespace)
|
|
{
|
|
return Optional.ofNullable(findByNameAndNamespace(name, namespace));
|
|
}
|
|
|
|
@Override
|
|
public Watch watch(String namespace, Watcher<Pod> watcher)
|
|
{
|
|
String version = clientProvider.getClient().pods().inAnyNamespace().list().getMetadata().getResourceVersion();
|
|
if (namespace.equals("_all"))
|
|
{
|
|
return clientProvider.getClient().pods().inAnyNamespace().withResourceVersion(version).watch(watcher);
|
|
}
|
|
return clientProvider.getClient().pods().inNamespace(namespace).withResourceVersion(version).watch(watcher);
|
|
}
|
|
|
|
public Optional<Pod> findPodById(String id)
|
|
{
|
|
for(Pod pod : clientProvider.getClient().pods().inAnyNamespace().list().getItems())
|
|
{
|
|
if(pod.getMetadata().getUid().equals(id))
|
|
{
|
|
return Optional.of(pod);
|
|
}
|
|
}
|
|
return Optional.empty();
|
|
}
|
|
}
|