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 93cf6d1873
commit 8912ae5f3c
3 changed files with 74 additions and 42 deletions

View File

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

View File

@ -44,11 +44,11 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => {
return;
}
// 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
let nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies);
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.end();

View File

@ -49,21 +49,25 @@ export async function getUserResources (req, username) {
const db = global.db;
const dbResources = db.getGlobalConfig().resources;
const used = await getUsedResources(req, dbResources);
const max = db.getUserConfig(username).resources.max;
const avail = {};
Object.keys(max).forEach((k) => {
const userResources = db.getUserConfig(username).resources;
Object.keys(userResources).forEach((k) => {
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) => {
const index = avail[k].findIndex((maxElement) => usedDeviceName.includes(maxElement));
avail[k].splice(index, 1);
const index = userResources[k].findIndex((availEelement) => usedDeviceName.includes(availEelement.match));
userResources[k][index].used++;
userResources[k][index].avail--;
});
}
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.
*/
export async function approveResources (req, username, request) {
const user = await getUserResources(req, username);
const avail = user.avail;
const resources = user.resources;
const db = global.db;
const dbResources = db.getGlobalConfig().resources;
const userResources = await getUserResources(req, username);
let approved = true;
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;
}
else if (resources[key].type === "list") {
const inAvail = avail[key].some(availElem => request[key].includes(availElem));
if (inAvail !== resources[key].whitelist) {
else if (dbResources[key].type === "list") { // if the resource type is list, check if the requested resource exists in the list
const index = userResources[key].findIndex((availElement) => request[key].includes(availElement.match));
// 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;
}
}
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;
}
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;
}
});