update resource data format,

add names to pci and cpu list resources
This commit is contained in:
Arthur Lu 2023-09-08 20:48:33 +00:00
parent 45006171ee
commit a366918256
3 changed files with 74 additions and 42 deletions

View File

@ -23,7 +23,7 @@
"display": false "display": false
}, },
"cores": { "cores": {
"title": "vCPU", "name": "vCPU",
"type": "numeric", "type": "numeric",
"multiplier": 1, "multiplier": 1,
"base": 1024, "base": 1024,
@ -32,7 +32,7 @@
"display": true "display": true
}, },
"memory": { "memory": {
"title": "RAM", "name": "RAM",
"type": "numeric", "type": "numeric",
"multiplier": 1048576, "multiplier": 1048576,
"base": 1024, "base": 1024,
@ -41,7 +41,7 @@
"display": true "display": true
}, },
"swap": { "swap": {
"title": "SWAP", "name": "SWAP",
"type": "numeric", "type": "numeric",
"multiplier": 1048576, "multiplier": 1048576,
"base": 1024, "base": 1024,
@ -50,7 +50,7 @@
"display": true "display": true
}, },
"local": { "local": {
"title": "local", "name": "local",
"type": "storage", "type": "storage",
"multiplier": 1, "multiplier": 1,
"base": 1024, "base": 1024,
@ -65,7 +65,7 @@
"display": true "display": true
}, },
"cephpl": { "cephpl": {
"title": "cephpl", "name": "cephpl",
"type": "storage", "type": "storage",
"multiplier": 1, "multiplier": 1,
"base": 1024, "base": 1024,
@ -80,8 +80,8 @@
"display": true "display": true
}, },
"network": { "network": {
"title": "Network", "name": "Network",
"type": "network", "type": "numeric",
"multiplier": 1000000, "multiplier": 1000000,
"base": 1000, "base": 1000,
"compact": true, "compact": true,
@ -122,22 +122,48 @@
"users": { "users": {
"exampleuser@examplepool": { "exampleuser@examplepool": {
"resources": { "resources": {
"max": {
"cpu": [ "cpu": [
"kvm64", {
"host" "match": "kvm64",
], "name": "kvm64",
"cores": 128, "max": 1
"memory": 131072, },
"swap": 131072, {
"local": 1099511627776, "match": "host",
"cephpl": 1099511627776, "name": "host",
"network": 100000, "max": 1
"pci": [
"Device Name Matcher 1",
"Device Name Matcher 2"
]
} }
],
"cores": {
"max": 128
},
"memory": {
"max": 131072
},
"swap": {
"max": 131072
},
"local": {
"max": 1099511627776
},
"cephpl": {
"max": 1099511627776
},
"network": {
"max": 100000
},
"pci": [
{
"match": "Device Name Matcher 1",
"name": "Device Display Name",
"max": 1
},
{
"match": "Device Name Matcher 2",
"name": "Device Display Name",
"max": 1
}
]
}, },
"nodes": [ "nodes": [
"examplenode1", "examplenode1",

View File

@ -44,11 +44,11 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => {
return; return;
} }
// get remaining user resources // get remaining user resources
const userAvailPci = (await getUserResources(req, req.cookies.username)).avail.pci; const userAvailPci = (await getUserResources(req, req.cookies.username)).pci;
// get node avail devices // get node avail devices
let nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies); let nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies);
nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => { nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => {
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail); return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
})); }));
res.status(200).send(nodeAvailPci); res.status(200).send(nodeAvailPci);
res.end(); res.end();

View File

@ -49,21 +49,25 @@ export async function getUserResources (req, username) {
const db = global.db; const db = global.db;
const dbResources = db.getGlobalConfig().resources; const dbResources = db.getGlobalConfig().resources;
const used = await getUsedResources(req, dbResources); const used = await getUsedResources(req, dbResources);
const max = db.getUserConfig(username).resources.max; const userResources = db.getUserConfig(username).resources;
const avail = {}; Object.keys(userResources).forEach((k) => {
Object.keys(max).forEach((k) => {
if (dbResources[k] && dbResources[k].type === "list") { if (dbResources[k] && dbResources[k].type === "list") {
avail[k] = structuredClone(max[k]); userResources[k].forEach((listResource) => {
listResource.used = 0;
listResource.avail = listResource.max;
});
used[k].forEach((usedDeviceName) => { used[k].forEach((usedDeviceName) => {
const index = avail[k].findIndex((maxElement) => usedDeviceName.includes(maxElement)); const index = userResources[k].findIndex((availEelement) => usedDeviceName.includes(availEelement.match));
avail[k].splice(index, 1); userResources[k][index].used++;
userResources[k][index].avail--;
}); });
} }
else { else {
avail[k] = max[k] - used[k]; userResources[k].used = used[k];
userResources[k].avail = userResources[k].max - used[k];
} }
}); });
return { used, max, avail, resources: dbResources }; return userResources;
} }
/** /**
@ -74,24 +78,26 @@ export async function getUserResources (req, username) {
* @returns {boolean} true if the available resources can fullfill the requested resources, false otherwise. * @returns {boolean} true if the available resources can fullfill the requested resources, false otherwise.
*/ */
export async function approveResources (req, username, request) { export async function approveResources (req, username, request) {
const user = await getUserResources(req, username); const db = global.db;
const avail = user.avail; const dbResources = db.getGlobalConfig().resources;
const resources = user.resources; const userResources = await getUserResources(req, username);
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 userResources)) { // if requested resource is not in avail, block
approved = false; approved = false;
} }
else if (resources[key].type === "list") { else if (dbResources[key].type === "list") { // if the resource type is list, check if the requested resource exists in the list
const inAvail = avail[key].some(availElem => request[key].includes(availElem)); const index = userResources[key].findIndex((availElement) => request[key].includes(availElement.match));
if (inAvail !== resources[key].whitelist) { // if no matching resource when index == -1, then remaining is -1 otherwise use the remaining value
const avail = index === -1 ? false : userResources[key][index].avail > 0;
if (avail !== dbResources[key].whitelist) {
approved = false; approved = false;
} }
} }
else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block else if (isNaN(userResources[key].avail) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
approved = false; approved = false;
} }
else if (avail[key] - request[key] < 0) { // if the avail resources is less than the requested resources, block else if (userResources[key].avail - request[key] < 0) { // if the avail resources is less than the requested resources, block
approved = false; approved = false;
} }
}); });