88 lines
2.8 KiB
Java
88 lines
2.8 KiB
Java
package tk.antoine.roux.infrastructure.out;
|
|
|
|
import io.kubernetes.client.openapi.ApiException;
|
|
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
|
import io.kubernetes.client.openapi.models.V1Node;
|
|
import io.kubernetes.client.openapi.models.V1NodeList;
|
|
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
|
import io.vavr.collection.List;
|
|
import io.vavr.control.Option;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Service;
|
|
import tk.antoine.roux.domain.model.Node;
|
|
import tk.antoine.roux.domain.model.Node.NodeBuilder;
|
|
import tk.antoine.roux.domain.ResourceLister;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
import java.util.regex.Pattern;
|
|
|
|
@Service
|
|
public class NodeLister implements ResourceLister<Node> {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
|
|
|
private final CoreV1Api coreV1Api;
|
|
|
|
public NodeLister(CoreV1Api coreV1Api) {
|
|
this.coreV1Api = coreV1Api;
|
|
}
|
|
|
|
@Override
|
|
public List<Node> listAll() {
|
|
try {
|
|
V1NodeList v1NodeList = coreV1Api.listNode(
|
|
null, null, null, null, null, null,
|
|
null, null, null, null
|
|
);
|
|
return List.ofAll(v1NodeList.getItems())
|
|
.map(NodeLister::buildNodeFromV1Node);
|
|
} catch (ApiException exception) {
|
|
log.warn("Node listing failed", exception);
|
|
return List.empty();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<Node> listAllByName(String namePrefix) {
|
|
try {
|
|
V1NodeList v1NodeList = coreV1Api.listNode(
|
|
null, null, null, null, null, null,
|
|
null, null, null, null
|
|
);
|
|
return List.ofAll(v1NodeList.getItems())
|
|
.map(NodeLister::buildNodeFromV1Node)
|
|
.filter(node -> node.name().startsWith(namePrefix));
|
|
} catch (ApiException exception) {
|
|
log.warn("Node listing failed", exception);
|
|
return List.empty();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<Node> listAllByRegex(Pattern regex) {
|
|
try {
|
|
V1NodeList v1NodeList = coreV1Api.listNode(
|
|
null, null, null, null, null, null,
|
|
null, null, null, null
|
|
);
|
|
return List.ofAll(v1NodeList.getItems())
|
|
.map(NodeLister::buildNodeFromV1Node)
|
|
.filter(node -> regex.matcher(node.name()).find());
|
|
} catch (ApiException exception) {
|
|
log.warn("Node listing failed", exception);
|
|
return List.empty();
|
|
}
|
|
}
|
|
|
|
private static Node buildNodeFromV1Node(V1Node v1Node) {
|
|
NodeBuilder nodeBuilder = Node.builder();
|
|
|
|
Option.of(v1Node.getMetadata())
|
|
.map(V1ObjectMeta::getName)
|
|
.map(nodeBuilder::withName);
|
|
|
|
return nodeBuilder.build();
|
|
}
|
|
}
|