minecluster/lib/routes/system-route.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

import { Router } from "express";
import k8s from "@kubernetes/client-node";
import { WARN } from "../util/logging.js";
import kc from "../k8s/k8s-config.js";
const router = Router();
import cairoAuthMiddleware from "./middlewares/auth-middleware.js";
router.use(cairoAuthMiddleware);
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
// Get Routes
router.get("/available", (req, res) => {
return res.json({ cpu: 8000, memory: 16000 });
// TODO Workaround to detect available
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;