28 lines
1,022 B
JavaScript
28 lines
1,022 B
JavaScript
|
import { Router } from "express";
|
||
|
import k8s from "@kubernetes/client-node";
|
||
|
import { WARN } from "../util/logging.js";
|
||
|
const router = Router();
|
||
|
const kc = new k8s.KubeConfig();
|
||
|
kc.loadFromDefault();
|
||
|
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
|
||
|
// Get Routes
|
||
|
router.get("/available", (req, res) => {
|
||
|
k8sApi.listNode().then((nodeRes) => {
|
||
|
const nodeAllocatable = nodeRes.body.items.map((i) => i.status.allocatable);
|
||
|
const nodeResources = nodeAllocatable.map(({ cpu, memory }) => ({
|
||
|
cpu,
|
||
|
memory,
|
||
|
}));
|
||
|
const { cpu: clusterCpu, memory: clusterMemory } = nodeResources[0];
|
||
|
const isIdentical = ({ cpu, memory }) =>
|
||
|
clusterMemory === memory && clusterCpu === cpu;
|
||
|
if (!nodeResources.every(isIdentical))
|
||
|
WARN("ROUTES", "Warning, node resources were non-consistent");
|
||
|
const availableCpu = parseInt(clusterCpu) * 1000;
|
||
|
const availableMemory = parseInt(clusterMemory) / 1024;
|
||
|
res.json({ cpu: availableCpu, memory: availableMemory });
|
||
|
});
|
||
|
});
|
||
|
|
||
|
export default router;
|