diff --git a/src/common/k8s-api/__tests__/horizontal-pod-autoscaler.api.test.ts b/src/common/k8s-api/__tests__/horizontal-pod-autoscaler.api.test.ts index 90f48a7ff0..d5d9a32847 100644 --- a/src/common/k8s-api/__tests__/horizontal-pod-autoscaler.api.test.ts +++ b/src/common/k8s-api/__tests__/horizontal-pod-autoscaler.api.test.ts @@ -1,3 +1,6 @@ +import type { DiContainer } from "@ogre-tools/injectable"; +import getHorizontalPodAutoscalerMetrics from "../../../renderer/components/+config-autoscalers/get-hpa-metrics.injectable"; +import { getDiForUnitTesting } from "../../../renderer/getDiForUnitTesting"; import { HorizontalPodAutoscaler, HpaMetricType } from "../endpoints"; const hpaV2 = { @@ -20,12 +23,21 @@ const hpaV2 = { } } -describe("HorizontalPodAutoscalerApi", () => { +describe("getHorizontalPodAutoscalerMetrics", () => { + let di: DiContainer; + let getMetrics: (hpa: HorizontalPodAutoscaler) => string[]; + + beforeEach(() => { + di = getDiForUnitTesting(); + + getMetrics = di.inject(getHorizontalPodAutoscalerMetrics); + }); + describe("HPA v2", () => { it("should return correct empty metrics", () => { const hpa = new HorizontalPodAutoscaler(hpaV2); - expect(hpa.getMetrics()).toHaveLength(0); + expect(getMetrics(hpa)).toHaveLength(0); }); it("should return correct resource metrics", () => { @@ -48,7 +60,7 @@ describe("HorizontalPodAutoscalerApi", () => { } }); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 50%"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 50%"); }); it("should return correct resource metrics with current metrics", () => { @@ -87,7 +99,7 @@ describe("HorizontalPodAutoscalerApi", () => { } }); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("10% / 50%"); + expect(getMetrics(hpa)[0]).toEqual("10% / 50%"); }); it("should return correct container resource metrics", () => { @@ -113,7 +125,7 @@ describe("HorizontalPodAutoscalerApi", () => { } ); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 60%"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 60%"); }); it("should return correct pod metrics", () => { @@ -140,7 +152,7 @@ describe("HorizontalPodAutoscalerApi", () => { } ); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 1k"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 1k"); }); it("should return correct object metrics", () => { @@ -167,7 +179,7 @@ describe("HorizontalPodAutoscalerApi", () => { } ); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 10k"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 10k"); }); it("should return correct external metrics with average value", () => { @@ -197,7 +209,7 @@ describe("HorizontalPodAutoscalerApi", () => { } ); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 30 (avg)"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 30 (avg)"); }); it("should return correct external metrics with value", () => { @@ -227,7 +239,7 @@ describe("HorizontalPodAutoscalerApi", () => { } ); - expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 30"); + expect(getMetrics(hpa)[0]).toEqual("unknown / 30"); }); }); }); \ No newline at end of file diff --git a/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.injectable.ts b/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.injectable.ts new file mode 100644 index 0000000000..e34a3ebd2f --- /dev/null +++ b/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.injectable.ts @@ -0,0 +1,11 @@ +import { getInjectable } from "@ogre-tools/injectable"; +import { HorizontalPodAutoscalerV2MetricParser } from "./hpa-v2-metric-parser"; + +const horizonalPodAutoscalerV2MetricParser = getInjectable({ + id: "horizontal-pod-autoscaler-v2-metric-parser", + instantiate: () => { + return new HorizontalPodAutoscalerV2MetricParser(); + }, +}) + +export default horizonalPodAutoscalerV2MetricParser; \ No newline at end of file diff --git a/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.ts b/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.ts new file mode 100644 index 0000000000..b513ce6a0f --- /dev/null +++ b/src/renderer/components/+config-autoscalers/hpa-v2-metric-parser.ts @@ -0,0 +1,62 @@ +import type { ContainerResourceMetricSource, ContainerResourceMetricStatus, ExternalMetricSource, ExternalMetricStatus, MetricCurrentTarget, ObjectMetricSource, ObjectMetricStatus, PodsMetricSource, PodsMetricStatus, ResourceMetricSource, ResourceMetricStatus } from "../../../common/k8s-api/endpoints"; + +export class HorizontalPodAutoscalerV2MetricParser { + public getResource({ current, target }: { current: ResourceMetricStatus | undefined, target: ResourceMetricSource }): MetricCurrentTarget { + return { + current: ( + typeof current?.current?.averageUtilization === "number" + ? `${current.current?.averageUtilization}%` + : current?.current?.averageValue + ), + target: typeof target?.target?.averageUtilization === "number" + ? `${target.target.averageUtilization}%` + : target?.target?.averageValue + }; + } + + public getPods({ current, target }: { current: PodsMetricStatus | undefined, target: PodsMetricSource }): MetricCurrentTarget { + return { + current: current?.current?.averageValue, + target: target?.target?.averageValue, + } + } + + public getObject({ current, target }: { current: ObjectMetricStatus | undefined, target: ObjectMetricSource }): MetricCurrentTarget { + return { + current: ( + current?.current?.value + ?? current?.current?.averageValue + ), + target: ( + target?.target?.value + ?? target?.target?.averageValue + ) + }; + } + + public getExternal({ current, target }: { current: ExternalMetricStatus | undefined, target: ExternalMetricSource }): MetricCurrentTarget { + return { + current: ( + current?.current?.value + ?? current?.current?.averageValue ? `${current?.current?.averageValue} (avg)` : undefined + ), + target: ( + target?.target?.value + ?? `${target?.target?.averageValue ?? "unknown"} (avg)` + ), + }; + } + + public getContainerResource({ current, target }: { current: ContainerResourceMetricStatus | undefined, target: ContainerResourceMetricSource }): MetricCurrentTarget { + return { + current: ( + current?.current?.averageValue + ?? current?.current?.averageUtilization ? `${current?.current?.averageUtilization}%` : "unknown" + ), + target: ( + target?.target?.averageValue + ?? target?.target?.averageUtilization ? `${target?.target?.averageUtilization}%` : "unknown" + ), + }; + } +} \ No newline at end of file