[FEATURE] Adjust more server controllers
This commit is contained in:
parent
62c966a6bd
commit
37e3dc2ae9
16 changed files with 281 additions and 173 deletions
|
@ -4,6 +4,8 @@ import k8s from "@kubernetes/client-node";
|
|||
import yaml from "js-yaml";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import ExpressClientError from "../util/ExpressClientError.js";
|
||||
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromDefault();
|
||||
const k8sDeps = kc.makeApiClient(k8s.AppsV1Api);
|
||||
|
@ -12,27 +14,9 @@ const namespace = process.env.MCL_SERVER_NAMESPACE;
|
|||
|
||||
const loadYaml = (f) => yaml.load(fs.readFileSync(path.resolve(f), "utf8"));
|
||||
|
||||
function payloadFilter(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
const { name, url, version, serverType, difficulty, gamemode, memory } =
|
||||
serverSpec;
|
||||
if (!name) return res.status(400).send("Server name is required!");
|
||||
if (!url) return res.status(400).send("Server url is required!");
|
||||
if (!version) return res.status(400).send("Server version is required!");
|
||||
if (!difficulty)
|
||||
return res.status(400).send("Server difficulty is required!");
|
||||
if (!serverType) return res.status(400).send("Server type is required!");
|
||||
if (!gamemode) return res.status(400).send("Server Gamemode is required!");
|
||||
if (!memory) return res.status(400).send("Memory is required!");
|
||||
req.body.name = req.body.name.toLowerCase();
|
||||
return "filtered";
|
||||
}
|
||||
|
||||
function createRconSecret(serverSpec) {
|
||||
const { name } = serverSpec;
|
||||
const rconYaml = loadYaml("lib/k8s/configs/rcon-secret.yml");
|
||||
|
||||
// TODO: Dyamic rconPassword
|
||||
const rconPassword = bcrypt.hashSync(uuidv4(), 10);
|
||||
rconYaml.data["rcon-password"] = Buffer.from(rconPassword).toString("base64");
|
||||
|
@ -84,7 +68,6 @@ function getServerContainer(serverSpec) {
|
|||
motd,
|
||||
maxPlayers,
|
||||
seed,
|
||||
modpack,
|
||||
ops,
|
||||
whitelist,
|
||||
} = serverSpec;
|
||||
|
@ -195,17 +178,15 @@ function createRconService(serverSpec) {
|
|||
return rconSvcYaml;
|
||||
}
|
||||
|
||||
export default async function createServer(req, res) {
|
||||
if (payloadFilter(req, res) !== "filtered") return;
|
||||
const serverSpec = req.body;
|
||||
export default async function createServerResources(serverSpec) {
|
||||
const deploymentRes = await k8sDeps.listNamespacedDeployment(namespace);
|
||||
const deployments = deploymentRes.body.items.map((i) => i.metadata.name);
|
||||
if (deployments.includes(`mcl-${serverSpec.name}`))
|
||||
return res.status(409).send("Server already exists!");
|
||||
throw new ExpressClientError("Server already exists!", { c: 409 });
|
||||
const pvcRes = await k8sCore.listNamespacedPersistentVolumeClaim(namespace);
|
||||
const pvcs = pvcRes.body.items.map((i) => i.metadata.name);
|
||||
if (pvcs.includes(`mcl-${serverSpec.name}-volume`))
|
||||
return res.status(409).send("Server PVC already exists!");
|
||||
throw new ExpressClientError("Server PVC already exists!", { c: 409 });
|
||||
const rconSecret = createRconSecret(serverSpec);
|
||||
const serverVolume = createServerVolume(serverSpec);
|
||||
const serverDeploy = createServerDeploy(serverSpec);
|
||||
|
@ -216,6 +197,4 @@ export default async function createServer(req, res) {
|
|||
k8sCore.createNamespacedService(namespace, serverService);
|
||||
k8sCore.createNamespacedService(namespace, rconService);
|
||||
k8sDeps.createNamespacedDeployment(namespace, serverDeploy);
|
||||
|
||||
res.sendStatus(200);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue