1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix cpu units not being correctly handled

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-07-29 15:38:28 -04:00
parent bcecb75d14
commit ccb8e7a02e
3 changed files with 45 additions and 13 deletions

View File

@ -3,18 +3,36 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { TypedRegEx } from "typed-regex";
// Helper to convert CPU K8S units to numbers
const thousand = 1000;
const million = thousand * thousand;
const shortBillion = thousand * million;
const unitConverters = new Map([
["m", 1000 ** -1], // milli
["", 1000 ** 0], // no units
["k", 1000 ** 1],
["M", 1000 ** 2],
["G", 1000 ** 3],
["P", 1000 ** 4],
["T", 1000 ** 5],
["E", 1000 ** 6],
]);
export function cpuUnitsToNumber(cpu: string) {
const cpuNum = parseInt(cpu);
const cpuUnitsRegex = TypedRegEx("^(?<digits>\\d+)(?<unit>.*)$");
if (cpu.includes("m")) return cpuNum / thousand;
if (cpu.includes("u")) return cpuNum / million;
if (cpu.includes("n")) return cpuNum / shortBillion;
export function cpuUnitsToNumber(value: string) {
const match = cpuUnitsRegex.captures(value);
return parseFloat(cpu);
if (!match) {
return undefined;
}
const { digits, unit } = match;
const convertion = unitConverters.get(unit);
if (convertion === undefined) {
return undefined;
}
return parseInt(digits) * convertion;
}

View File

@ -19,7 +19,7 @@ import logger from "../../../common/logger";
export interface ResourceQuotaDetailsProps extends KubeObjectDetailsProps<ResourceQuota> {
}
function transformUnit(name: string, value: string): number {
function transformUnit(name: string, value: string): number | undefined {
if (name.includes("memory") || name.includes("storage")) {
return unitsToBytes(value);
}
@ -40,7 +40,21 @@ function renderQuotas(quota: ResourceQuota): JSX.Element[] {
const rawCurrent = used[name] ?? "0";
const current = transformUnit(name, rawCurrent);
const max = transformUnit(name, rawMax);
const usage = max === 0 ? 100 : Math.ceil(current / max * 100); // special case 0 max as always 100% usage
if (current === undefined || max === undefined) {
return (
<div key={name} className={cssNames("param", kebabCase(name))}>
<span className="title">{name}</span>
<span className="value">
{`${rawCurrent} / ${rawMax}`}
</span>
</div>
);
}
const usage = max === 0
? 100 // special case 0 max as always 100% usage
: current / max * 100;
return (
<div key={name} className={cssNames("param", kebabCase(name))}>
@ -53,7 +67,7 @@ function renderQuotas(quota: ResourceQuota): JSX.Element[] {
value={current}
tooltip={(
<p>
{`Set: ${rawMax}. Usage: ${usage}%`}
{`Set: ${rawMax}. Usage: ${+usage.toFixed(2)}%`}
</p>
)}
/>

View File

@ -82,7 +82,7 @@ export class PodStore extends KubeObjectStore<Pod, PodApi> {
}
return {
cpu: total.cpu + cpuUnitsToNumber(cpu),
cpu: total.cpu + (cpuUnitsToNumber(cpu) ?? 0),
memory: total.memory + unitsToBytes(memory),
};
}, empty);