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

Fix external metirc parser output

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2023-01-18 14:49:00 +03:00
parent 6f06d3645b
commit ecd2e72327
2 changed files with 88 additions and 119 deletions

View File

@ -1,7 +1,7 @@
import type { DiContainer } from "@ogre-tools/injectable"; import type { DiContainer } from "@ogre-tools/injectable";
import getHorizontalPodAutoscalerMetrics from "../../../renderer/components/+config-autoscalers/get-hpa-metrics.injectable"; import getHorizontalPodAutoscalerMetrics from "./get-hpa-metrics.injectable";
import { getDiForUnitTesting } from "../../../renderer/getDiForUnitTesting"; import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import { HorizontalPodAutoscaler, HpaMetricType } from "../endpoints"; import { HorizontalPodAutoscaler, HpaMetricType } from "../../../common/k8s-api/endpoints";
const hpaV2 = { const hpaV2 = {
apiVersion: "autoscaling/v2", apiVersion: "autoscaling/v2",
@ -23,26 +23,6 @@ const hpaV2 = {
} }
} }
const hpaV1 = {
apiVersion: "autoscaling/v1",
kind: "HorizontalPodAutoscaler",
metadata: {
name: "hpav1",
resourceVersion: "1",
uid: "hpav1",
namespace: "default",
selfLink: "/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/hpav1",
},
spec: {
maxReplicas: 10,
scaleTargetRef: {
kind: "Deployment",
name: "hpav1deployment",
apiVersion: "apps/v1",
}
}
}
const hpaV2Beta1 = { const hpaV2Beta1 = {
apiVersion: "autoscaling/v2beta1", apiVersion: "autoscaling/v2beta1",
kind: "HorizontalPodAutoscaler", kind: "HorizontalPodAutoscaler",
@ -63,10 +43,10 @@ const hpaV2Beta1 = {
}, },
} }
const hpaV2Beta2 = { // const hpaV2Beta2 = {
...hpaV2Beta1, // ...hpaV2Beta1,
apiVersion: "autoscaling/v2beta2", // apiVersion: "autoscaling/v2beta2",
} // }
describe("getHorizontalPodAutoscalerMetrics", () => { describe("getHorizontalPodAutoscalerMetrics", () => {
let di: DiContainer; let di: DiContainer;
@ -572,7 +552,7 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
} }
); );
expect(getMetrics(hpa)[0]).toEqual("10 / 30 (avg)"); expect(getMetrics(hpa)[0]).toEqual("10 (avg) / 30 (avg)");
}); });
it("should return unknown current metrics if names are different", () => { it("should return unknown current metrics if names are different", () => {
@ -623,18 +603,18 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
}); });
}); });
describe("HPA v1", () => { describe("HPA v2beta1", () => {
it("should return correct empty metrics", () => { it("should return correct empty metrics", () => {
const hpa = new HorizontalPodAutoscaler(hpaV1); const hpa = new HorizontalPodAutoscaler(hpaV2Beta1);
expect(getMetrics(hpa)).toHaveLength(0); expect(getMetrics(hpa)).toHaveLength(0);
}); });
it("should return correct resource metrics", () => { it("should return correct resource metrics", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Resource, type: HpaMetricType.Resource,
@ -652,9 +632,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct resource metrics with current metrics", () => { it("should return correct resource metrics with current metrics", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Resource, type: HpaMetricType.Resource,
@ -685,9 +665,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct resource metrics with current value", () => { it("should return correct resource metrics with current value", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Resource, type: HpaMetricType.Resource,
@ -719,9 +699,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct container resource metrics", () => { it("should return correct container resource metrics", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.ContainerResource, type: HpaMetricType.ContainerResource,
@ -742,9 +722,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct container resource metrics with current utilization value", () => { it("should return correct container resource metrics with current utilization value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.ContainerResource, type: HpaMetricType.ContainerResource,
@ -778,9 +758,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct pod metrics", () => { it("should return correct pod metrics", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Pods, type: HpaMetricType.Pods,
@ -800,9 +780,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct pod metrics with current values", () => { it("should return correct pod metrics with current values", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Pods, type: HpaMetricType.Pods,
@ -836,9 +816,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct object metrics", () => { it("should return correct object metrics", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Object, type: HpaMetricType.Object,
@ -858,9 +838,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct object metrics with average value", () => { it("should return correct object metrics with average value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Object, type: HpaMetricType.Object,
@ -880,9 +860,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct object metrics with current value", () => { it("should return correct object metrics with current value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Object, type: HpaMetricType.Object,
@ -915,9 +895,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct external metrics with average value", () => { it("should return correct external metrics with average value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.External, type: HpaMetricType.External,
@ -938,9 +918,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct external metrics with value", () => { it("should return correct external metrics with value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.External, type: HpaMetricType.External,
@ -961,9 +941,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct external metrics with current value", () => { it("should return correct external metrics with current value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.External, type: HpaMetricType.External,
@ -997,9 +977,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return correct external metrics with current average value", () => { it("should return correct external metrics with current average value", () => {
const hpa = new HorizontalPodAutoscaler( const hpa = new HorizontalPodAutoscaler(
{ {
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.External, type: HpaMetricType.External,
@ -1032,9 +1012,9 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
it("should return unknown current metrics if metric names are different", () => { it("should return unknown current metrics if metric names are different", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV1, ...hpaV2Beta1,
spec: { spec: {
...hpaV1.spec, ...hpaV2Beta1.spec,
metrics: [ metrics: [
{ {
type: HpaMetricType.Resource, type: HpaMetricType.Resource,
@ -1064,73 +1044,60 @@ describe("getHorizontalPodAutoscalerMetrics", () => {
}); });
}); });
describe("HPA v2beta1", () => { describe("HPA v1", () => {
it("should use metric parser for hpa autoscaling/v2", () => { it("should show target cpu utilization percentage", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV2Beta1, apiVersion: "autoscaling/v1",
kind: "HorizontalPodAutoscaler",
metadata: {
name: "hpav1",
resourceVersion: "1",
uid: "hpav1",
namespace: "default",
selfLink: "/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/hpav1",
},
spec: { spec: {
...hpaV2Beta1.spec, maxReplicas: 10,
metrics: [ scaleTargetRef: {
{ kind: "Deployment",
type: HpaMetricType.Resource, name: "hpav1deployment",
resource: { apiVersion: "apps/v1",
name: "cpu", },
target: { targetCPUUtilizationPercentage: 80
type: "Utilization",
averageUtilization: 50
}
}
}
]
} }
}); });
expect(getMetrics(hpa)[0]).toEqual("unknown / 50%"); expect(getMetrics(hpa)[0]).toEqual("unknown / 80%");
}); });
it("should return unknown metrics for invalid v2 metric format", () => { it("should show current and target cpu utilization percentage", () => {
const hpa = new HorizontalPodAutoscaler({ const hpa = new HorizontalPodAutoscaler({
...hpaV2Beta1, apiVersion: "autoscaling/v1",
kind: "HorizontalPodAutoscaler",
metadata: {
name: "hpav1",
resourceVersion: "1",
uid: "hpav1",
namespace: "default",
selfLink: "/apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/hpav1",
},
spec: { spec: {
...hpaV2Beta1.spec, maxReplicas: 10,
metrics: [ scaleTargetRef: {
{ kind: "Deployment",
type: HpaMetricType.Resource, name: "hpav1deployment",
resource: { apiVersion: "apps/v1",
name: "cpu", },
targetAverageUtilization: 50 targetCPUUtilizationPercentage: 80
} },
} status: {
] currentReplicas: 1,
desiredReplicas: 10,
currentCPUUtilizationPercentage: 10
} }
}); });
expect(getMetrics(hpa)[0]).toEqual("unknown / unknown"); expect(getMetrics(hpa)[0]).toEqual("10% / 80%");
});
});
describe("HPA v2beta2", () => {
it("should use metric parser for hpa autoscaling/v2", () => {
const hpa = new HorizontalPodAutoscaler({
...hpaV2Beta2,
spec: {
...hpaV2Beta2.spec,
metrics: [
{
type: HpaMetricType.Resource,
resource: {
name: "cpu",
target: {
type: "Utilization",
averageUtilization: 50
}
}
}
]
}
});
expect(getMetrics(hpa)[0]).toEqual("unknown / 50%");
}); });
}); });
}); });

View File

@ -35,14 +35,16 @@ export class HorizontalPodAutoscalerV2MetricParser {
} }
public getExternal({ current, target }: { current: ExternalMetricStatus | undefined, target: ExternalMetricSource }): MetricCurrentTarget { public getExternal({ current, target }: { current: ExternalMetricStatus | undefined, target: ExternalMetricSource }): MetricCurrentTarget {
const currentAverage = current?.current?.averageValue ? `${current?.current?.averageValue} (avg)` : undefined;
const targetAverage = target?.target?.averageValue ? `${target?.target?.averageValue} (avg)` : undefined;
return { return {
current: ( current: (
current?.current?.value current?.current?.value
?? current?.current?.averageValue ?? currentAverage
), ),
target: ( target: (
target?.target?.value target?.target?.value
?? `${target?.target?.averageValue ?? "unknown"} (avg)` ?? targetAverage
), ),
}; };
} }
@ -51,11 +53,11 @@ export class HorizontalPodAutoscalerV2MetricParser {
return { return {
current: ( current: (
current?.current?.averageValue current?.current?.averageValue
?? current?.current?.averageUtilization ? `${current?.current?.averageUtilization}%` : "unknown" ?? current?.current?.averageUtilization ? `${current?.current?.averageUtilization}%` : undefined
), ),
target: ( target: (
target?.target?.averageValue target?.target?.averageValue
?? target?.target?.averageUtilization ? `${target?.target?.averageUtilization}%` : "unknown" ?? target?.target?.averageUtilization ? `${target?.target?.averageUtilization}%` : undefined
), ),
}; };
} }