[FEATURE] Adjust more server controllers

This commit is contained in:
Dunemask 2023-12-18 03:55:27 -07:00
parent 62c966a6bd
commit 37e3dc2ae9
16 changed files with 281 additions and 173 deletions

View file

@ -6,6 +6,7 @@ import {
scaleDeployment,
} from "./k8s-server-control.js";
import { ERR } from "../util/logging.js";
import ExpressClientError from "../util/ExpressClientError.js";
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
@ -13,61 +14,38 @@ const k8sMetrics = new k8s.Metrics(kc);
const k8sDeps = kc.makeApiClient(k8s.AppsV1Api);
const namespace = process.env.MCL_SERVER_NAMESPACE;
// Gets the all assets for the server
export async function getServer(req, res) {
const serverSpec = req.body;
if (!serverSpec) return res.sendStatus(400);
if (!serverSpec.name) return res.status(400).send("Server name required!");
const { name } = serverSpec;
getServerAssets(name)
.then((server) => res.status(200).json(server))
.catch((e) => res.status(500).send(e));
}
export async function startServer(req, res) {
const serverSpec = req.body;
if (!serverSpec) return res.sendStatus(400);
if (!serverSpec.name) return res.status(400).send("Server name required!");
export async function startServerContainer(serverSpec) {
const { name } = serverSpec;
try {
await scaleDeployment(name, true);
res.sendStatus(200);
} catch (e) {
ERR("SERVER CONTROL", e);
res.status(500).send(`Error updating server '${name}'!`);
throw new ExpressClientError({
c: 500,
m: `Error updating server '${name}'!\n`,
});
}
}
export async function stopServer(req, res) {
const serverSpec = req.body;
if (!serverSpec) return res.sendStatus(400);
if (!serverSpec.name) return res.status(400).send("Server name required!");
export async function stopServerContainer(serverSpec) {
const { name } = serverSpec;
try {
await scaleDeployment(name, false);
res.sendStatus(200);
} catch (e) {
ERR("SERVER CONTROL", e);
res.status(500).send(`Error updating server '${name}'!`);
throw new ExpressClientError({
c: 500,
m: `Error updating server '${name}'!`,
});
}
}
export function serverList(req, res) {
getDeployments()
.then((sd) => res.json(sd.map((s) => s.metadata.name.substring(4))))
.catch((e) => {
ERR("SERVER CONTROL", e);
res.status(500).send("Couldn't get server list");
});
}
export async function getServers(req, res) {
export async function getInstances() {
const serverDeployments = await getDeployments();
const podMetricsResponse = await k8sMetrics.getPodMetrics(namespace);
var name, metrics, started;
const servers = serverDeployments.map((s) => {
name = s.metadata.name.substring(4);
const serverInstances = serverDeployments.map((s) => {
name = s.metadata.annotations["minecluster.dunemask.net/server-name"];
metrics = null;
started = !!s.spec.replicas;
const pod = podMetricsResponse.items.find(({ metadata: md }) => {
@ -85,18 +63,22 @@ export async function getServers(req, res) {
memory: Math.ceil(podMems.reduce((a, b) => a + b)),
};
}
return { name, metrics, started };
});
return serverInstances;
}
export async function getNamespaceMetrics() {
const serverInstances = await getInstances();
var clusterMetrics = { cpu: 0, memory: 0 };
if (servers.length > 1) {
const clusterCpu = servers
const clusterCpu = serverInstances
.map(({ metrics }) => (metrics ? metrics.cpu : 0))
.reduce((a, b) => a + b);
const clusterMem = servers
const clusterMem = serverInstances
.map(({ metrics }) => (metrics ? metrics.memory : 0))
.reduce((a, b) => a + b);
clusterMetrics = { cpu: clusterCpu, memory: clusterMem };
}
res.json({ servers, clusterMetrics });
return clusterMetrics;
}