feat(kubernetes): add kubernetes tool (#1929)

Co-authored-by: oussama Dahmaz <dahmaz@MacBook-Pro-de-odahmaz.local>
This commit is contained in:
oussama Dahmaz
2025-03-09 17:46:15 +01:00
committed by GitHub
parent f6f0d7c72b
commit f19aa29607
67 changed files with 3897 additions and 67 deletions

View File

@@ -24,6 +24,12 @@ vi.mock("@homarr/redis", () => ({
}),
}));
vi.mock("@homarr/docker/env", () => ({
env: {
ENABLE_DOCKER: true,
},
}));
const createSessionWithPermissions = (...permissions: GroupPermissionKey[]) =>
({
user: {

View File

@@ -0,0 +1,56 @@
import { describe, expect, it } from "vitest";
import { CpuResourceParser } from "../../../kubernetes/resource-parser/cpu-resource-parser";
describe("CpuResourceParser", () => {
const parser = new CpuResourceParser();
it("should return NaN for empty or invalid input", () => {
expect(parser.parse("")).toBeNaN();
expect(parser.parse(" ")).toBeNaN();
expect(parser.parse("abc")).toBeNaN();
});
it("should parse CPU values without a unit (cores)", () => {
expect(parser.parse("1")).toBe(1);
expect(parser.parse("2.5")).toBe(2.5);
expect(parser.parse("10")).toBe(10);
});
it("should parse CPU values with milli-core unit ('m')", () => {
expect(parser.parse("500m")).toBe(0.5); // 500 milli-cores = 0.5 cores
expect(parser.parse("250m")).toBe(0.25);
expect(parser.parse("1000m")).toBe(1);
});
it("should parse CPU values with kilo-core unit ('k')", () => {
expect(parser.parse("1k")).toBe(1000); // 1 kilo-core = 1000 cores
expect(parser.parse("2k")).toBe(2000);
expect(parser.parse("0.5k")).toBe(500);
});
it("should parse CPU values with nano-core unit ('n')", () => {
// Adjust the expected values for nano-cores to account for floating-point precision
expect(parser.parse("1000000000n")).toBe(1); // 1 NanoCPU = 1/1,000,000,000 cores
expect(parser.parse("500000000n")).toBe(0.5);
expect(parser.parse("0.000000001n")).toBe(0.000000000000000001); // Tiny value
});
it("should parse CPU values with micro-core unit ('u')", () => {
// Adjust the expected values for micro-cores to account for floating-point precision
expect(parser.parse("1000000u")).toBe(1); // 1 MicroCPU = 1/1,000,000 cores
expect(parser.parse("500000u")).toBe(0.5);
expect(parser.parse("0.000001u")).toBe(0.000000000001); // Tiny value
});
it("should handle input with commas", () => {
expect(parser.parse("1,000")).toBe(1000); // 1,000 cores
expect(parser.parse("1,500m")).toBe(1.5); // 1,500 milli-cores = 1.5 cores
});
it("should ignore leading and trailing whitespace", () => {
expect(parser.parse(" 1 ")).toBe(1);
expect(parser.parse(" 500m ")).toBe(0.5);
expect(parser.parse(" 2k ")).toBe(2000);
});
});

View File

@@ -0,0 +1,61 @@
import { describe, expect, it } from "vitest";
import { MemoryResourceParser } from "../../../kubernetes/resource-parser/memory-resource-parser";
const BYTES_IN_GIB = 1024 ** 3; // 1 GiB in bytes
const BYTES_IN_MIB = 1024 ** 2; // 1 MiB in bytes
const BYTES_IN_KIB = 1024; // 1 KiB in bytes
const KI = "Ki";
const MI = "Mi";
const GI = "Gi";
const TI = "Ti";
const PI = "Pi";
describe("MemoryResourceParser", () => {
const parser = new MemoryResourceParser();
it("should parse values without units as bytes and convert to GiB", () => {
expect(parser.parse("1073741824")).toBe(1); // 1 GiB
expect(parser.parse("2147483648")).toBe(2); // 2 GiB
});
it("should parse binary units (Ki, Mi, Gi, Ti, Pi) into GiB", () => {
expect(parser.parse(`1024${KI}`)).toBeCloseTo(1 / 1024); // 1 MiB = 1/1024 GiB
expect(parser.parse(`1${MI}`)).toBeCloseTo(1 / 1024); // 1 MiB = 1/1024 GiB
expect(parser.parse(`1${GI}`)).toBe(1); // 1 GiB
expect(parser.parse(`1${TI}`)).toBe(BYTES_IN_KIB); // 1 TiB = 1024 GiB
expect(parser.parse(`1${PI}`)).toBe(BYTES_IN_MIB); // 1 PiB = 1024^2 GiB
});
it("should parse decimal units (K, M, G, T, P) into GiB", () => {
expect(parser.parse("1000K")).toBeCloseTo(1000 / BYTES_IN_GIB); // 1000 KB
expect(parser.parse("1M")).toBeCloseTo(1 / BYTES_IN_KIB); // 1 MB = 1/1024 GiB
expect(parser.parse("1G")).toBeCloseTo(0.9313225746154785); // 1 GB ≈ 0.931 GiB
expect(parser.parse("1T")).toBeCloseTo(931.3225746154785); // 1 TB ≈ 931.32 GiB
expect(parser.parse("1P")).toBeCloseTo(931322.5746154785); // 1 PB ≈ 931,322.57 GiB
});
it("should handle invalid input and return NaN", () => {
expect(parser.parse("")).toBeNaN();
expect(parser.parse(" ")).toBeNaN();
expect(parser.parse("abc")).toBeNaN();
});
it("should handle commas in input and convert to GiB", () => {
expect(parser.parse("1,073,741,824")).toBe(1); // 1 GiB
expect(parser.parse("1,024Ki")).toBeCloseTo(1 / BYTES_IN_KIB); // 1 MiB
});
it("should handle lowercase and uppercase units", () => {
expect(parser.parse("1ki")).toBeCloseTo(1 / BYTES_IN_KIB); // 1 MiB
expect(parser.parse("1KI")).toBeCloseTo(1 / BYTES_IN_KIB);
expect(parser.parse("1Mi")).toBeCloseTo(1 / BYTES_IN_KIB);
expect(parser.parse("1m")).toBeCloseTo(1 / BYTES_IN_KIB);
});
it("should assume bytes for unrecognized or no units and convert to GiB", () => {
expect(parser.parse("1073741824")).toBe(1); // 1 GiB
expect(parser.parse("42")).toBeCloseTo(42 / BYTES_IN_GIB); // 42 bytes in GiB
expect(parser.parse("42unknown")).toBeCloseTo(42 / BYTES_IN_GIB); // Invalid unit = bytes
});
});