implement proc type in /instance/resources

This commit is contained in:
Arthur Lu 2023-06-14 04:49:43 +00:00
parent 8d7d952e21
commit e9386f5204
3 changed files with 46 additions and 18 deletions

View File

@ -1,37 +1,45 @@
{ {
"application": { "application": {
"pveAPI": "https://pve.mydomain/api2/json", "pveAPI": "https://pve.mydoamin/api2/json",
"pveAPIToken": { "pveAPIToken": {
"user": "proxmoxaas-api", "user": "proxmoxaas-api",
"realm": "pve", "realm": "pve",
"id": "token", "id": "token",
"uuid": "secret-key" "uuid": "token-secret-value"
}, },
"listenPort": 80, "listenPort": 80,
"hostname": "client.mydomain", "hostname": "client.mydomain",
"domain": "mydomain" "domain": "mydomain"
}, },
"resources": { "resources": {
"cpu": {
"type": "list",
"blacklist": false,
"display": false
},
"cores": { "cores": {
"type": "numeric", "type": "numeric",
"multiplier": 1, "multiplier": 1,
"base": 1024, "base": 1024,
"compact": false, "compact": false,
"unit": "Cores" "unit": "Cores",
"display": true
}, },
"memory": { "memory": {
"type": "numeric", "type": "numeric",
"multiplier": 1048576, "multiplier": 1048576,
"base": 1024, "base": 1024,
"compact": true, "compact": true,
"unit": "B" "unit": "B",
"display": true
}, },
"swap": { "swap": {
"type": "numeric", "type": "numeric",
"multiplier": 1048576, "multiplier": 1048576,
"base": 1024, "base": 1024,
"compact": true, "compact": true,
"unit": "B" "unit": "B",
"display": true
}, },
"local": { "local": {
"type": "storage", "type": "storage",
@ -44,7 +52,8 @@
"mp", "mp",
"sata", "sata",
"unused" "unused"
] ],
"display": true
}, },
"cephpl": { "cephpl": {
"type": "storage", "type": "storage",
@ -57,20 +66,23 @@
"mp", "mp",
"sata", "sata",
"unused" "unused"
] ],
"display": true
}, },
"network": { "network": {
"type": "network", "type": "network",
"multiplier": 1000000, "multiplier": 1000000,
"base": 1000, "base": 1000,
"compact": true, "compact": true,
"unit": "B/s" "unit": "B/s",
"display": true
} }
}, },
"users": { "users": {
"exampleuser@realm": { "exampleuser@authrealm": {
"resources": { "resources": {
"max": { "max": {
"cpu": ["kvm64", "host"],
"cores": 0, "cores": 0,
"memory": 0, "memory": 0,
"swap": 0, "swap": 0,
@ -80,16 +92,16 @@
} }
}, },
"nodes": [ "nodes": [
"node-0-id", "node1",
"node-1-id" "node2"
], ],
"instances": { "instances": {
"vmid": { "vmid": {
"min": 100, "min": 100,
"max": 199 "max": 199
}, },
"pool": "examplepool", "pool": "exampleuserpool",
"vlan": "100", "vlan": "10",
"templates": { "templates": {
"lxc": { "lxc": {
"net0": { "net0": {

View File

@ -624,8 +624,9 @@ app.get("/api/instance/pci", async (req, res) => {
* - node: String - vm host node id * - node: String - vm host node id
* - type: String - vm type (lxc, qemu) * - type: String - vm type (lxc, qemu)
* - vmid: Number - vm id number * - vmid: Number - vm id number
* - cores: Number - new number of cores for instance * - proctype: String - vm processor type
* - memory: Number - new amount of memory for instance * - cores: Number, optional - number of processor cores for instance
* - memory: Number - amount of memory for instance
* - swap: Number, optional - new amount of swap for instance * - swap: Number, optional - new amount of swap for instance
* responses: * responses:
* - 200: PVE Task Object * - 200: PVE Task Object
@ -647,6 +648,9 @@ app.post("/api/instance/resources", async (req, res) => {
if (req.body.type === "lxc") { if (req.body.type === "lxc") {
request.swap = Number(req.body.swap) - Number(currentConfig.data.data.swap); request.swap = Number(req.body.swap) - Number(currentConfig.data.data.swap);
} }
else if (req.body.type === "qemu") {
request.cpu = req.body.proctype;
}
// check resource approval // check resource approval
if (!await approveResources(req, req.cookies.username, request)) { if (!await approveResources(req, req.cookies.username, request)) {
res.status(500).send({ request: request, error: `Could not fulfil request.` }); res.status(500).send({ request: request, error: `Could not fulfil request.` });
@ -658,6 +662,10 @@ app.post("/api/instance/resources", async (req, res) => {
if (req.body.type === "lxc") { if (req.body.type === "lxc") {
action.swap = Number(req.body.swap); action.swap = Number(req.body.swap);
} }
else if (req.body.type === "qemu") {
action.cpu = req.body.proctype;
}
console.log(action)
action = JSON.stringify(action); action = JSON.stringify(action);
let method = req.body.type === "qemu" ? "POST" : "PUT"; let method = req.body.type === "qemu" ? "POST" : "PUT";
// commit action // commit action

View File

@ -35,16 +35,24 @@ export async function getUserResources(req, username) {
Object.keys(max).forEach((k) => { Object.keys(max).forEach((k) => {
avail[k] = max[k] - used[k]; avail[k] = max[k] - used[k];
}); });
return { used: used, max: max, avail: avail, units: dbResources }; return { used: used, max: max, avail: avail, resources: dbResources };
} }
export async function approveResources(req, username, request) { export async function approveResources(req, username, request) {
let avail = (await getUserResources(req, username)).avail; let user = await getUserResources(req, username)
let avail = user.avail;
let resources = user.resources;
let max = user.max;
let approved = true; let approved = true;
Object.keys(request).forEach((key) => { Object.keys(request).forEach((key) => {
if (!(key in avail)) { // if requested resource is not in avail, block if (!(key in avail)) { // if requested resource is not in avail, block
approved = false; approved = false;
} }
else if (resources[key].type === "list") {
if (max[key].includes(request[key]) != resources[key].whitelist) {
approved = false;
}
}
else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
approved = false; approved = false;
} }