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

Add metric parser for HPA v2

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2023-01-18 09:38:58 +03:00
parent 674833fb4c
commit 7f70c49a5c
3 changed files with 94 additions and 9 deletions

View File

@ -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");
});
});
});

View File

@ -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;

View File

@ -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"
),
};
}
}