feat(kubernetes): add kubernetes tool (#1929)
Co-authored-by: oussama Dahmaz <dahmaz@MacBook-Pro-de-odahmaz.local>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import type { ResourceParser } from "./resource-parser";
|
||||
|
||||
export class CpuResourceParser implements ResourceParser {
|
||||
private readonly billionthsCore = 1_000_000_000;
|
||||
private readonly millionthsCore = 1_000_000;
|
||||
private readonly MiliCore = 1_000;
|
||||
private readonly ThousandCore = 1_000;
|
||||
|
||||
parse(value: string): number {
|
||||
if (!value.length) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
value = value.replace(/,/g, "").trim();
|
||||
|
||||
const [, numericValue, unit = ""] = /^([0-9.]+)\s*([a-zA-Z]*)$/.exec(value) ?? [];
|
||||
|
||||
if (numericValue === undefined) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const parsedValue = parseFloat(numericValue);
|
||||
|
||||
if (isNaN(parsedValue)) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
switch (unit.toLowerCase()) {
|
||||
case "n": // nano-cores (billionths of a core)
|
||||
return parsedValue / this.billionthsCore; // 1 NanoCPU = 1/1,000,000,000 cores
|
||||
case "u": // micro-cores (millionths of a core)
|
||||
return parsedValue / this.millionthsCore; // 1 MicroCPU = 1/1,000,000 cores
|
||||
case "m": // milli-cores
|
||||
return parsedValue / this.MiliCore; // 1 milli-core = 1/1000 cores
|
||||
case "k": // thousands of cores
|
||||
return parsedValue * this.ThousandCore; // 1 thousand-core = 1000 cores
|
||||
default: // cores (no unit)
|
||||
return parsedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { ResourceParser } from "./resource-parser";
|
||||
|
||||
export class MemoryResourceParser implements ResourceParser {
|
||||
private readonly binaryMultipliers: Record<string, number> = {
|
||||
ki: 1024,
|
||||
mi: 1024 ** 2,
|
||||
gi: 1024 ** 3,
|
||||
ti: 1024 ** 4,
|
||||
pi: 1024 ** 5,
|
||||
} as const;
|
||||
|
||||
private readonly decimalMultipliers: Record<string, number> = {
|
||||
k: 1000,
|
||||
m: 1000 ** 2,
|
||||
g: 1000 ** 3,
|
||||
t: 1000 ** 4,
|
||||
p: 1000 ** 5,
|
||||
} as const;
|
||||
|
||||
parse(value: string): number {
|
||||
if (!value.length) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
value = value.replace(/,/g, "").trim();
|
||||
|
||||
const [, numericValue, unit = ""] = /^([0-9.]+)\s*([a-zA-Z]*)$/.exec(value) ?? [];
|
||||
|
||||
if (!numericValue) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const parsedValue = parseFloat(numericValue);
|
||||
|
||||
if (isNaN(parsedValue)) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const unitLower = unit.toLowerCase();
|
||||
|
||||
// Handle binary units (Ki, Mi, Gi, etc.)
|
||||
if (unitLower in this.binaryMultipliers) {
|
||||
const multiplier = this.binaryMultipliers[unitLower];
|
||||
const giMultiplier = this.binaryMultipliers.gi;
|
||||
|
||||
if (multiplier !== undefined && giMultiplier !== undefined) {
|
||||
return (parsedValue * multiplier) / giMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle decimal units (K, M, G, etc.)
|
||||
if (unitLower in this.decimalMultipliers) {
|
||||
const multiplier = this.decimalMultipliers[unitLower];
|
||||
const giMultiplier = this.binaryMultipliers.gi;
|
||||
|
||||
if (multiplier !== undefined && giMultiplier !== undefined) {
|
||||
return (parsedValue * multiplier) / giMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
// No unit or unrecognized unit, assume bytes and convert to GiB
|
||||
const giMultiplier = this.binaryMultipliers.gi;
|
||||
if (giMultiplier !== undefined) {
|
||||
return parsedValue / giMultiplier;
|
||||
}
|
||||
|
||||
return NaN; // Return NaN if giMultiplier is undefined
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface ResourceParser {
|
||||
parse(value: string): number;
|
||||
}
|
||||
Reference in New Issue
Block a user