From ecdf6667cfc2381a9bb8469192d0e56052a272b4 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Mon, 16 Jan 2023 12:11:05 +0300 Subject: [PATCH] External target metrics Signed-off-by: Alex Andreev --- .../horizontal-pod-autoscaler.api.test.ts | 60 +++++++++++++++++++ .../horizontal-pod-autoscaler.api.ts | 17 +++++- 2 files changed, 74 insertions(+), 3 deletions(-) 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 1bbe1f0cc8..60f8457879 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 @@ -130,5 +130,65 @@ describe("HorizontalPodAutoscalerApi", () => { expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 10k"); }); + + it("should return correct external metrics with average value", () => { + const hpa = new HorizontalPodAutoscaler( + { + ...hpaV2, + spec: { + ...hpaV2.spec, + metrics: [ + { + type: HpaMetricType.External, + external: { + metric: { + name: "queue_messages_ready", + selector: { + matchLabels: {queue: 'worker_tasks'} + } + }, + target: { + type: "AverageValue", + averageValue: "30" + } + } + } + ] + } + } + ); + + expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 30 (avg)"); + }); + + it("should return correct external metrics with value", () => { + const hpa = new HorizontalPodAutoscaler( + { + ...hpaV2, + spec: { + ...hpaV2.spec, + metrics: [ + { + type: HpaMetricType.External, + external: { + metric: { + name: "queue_messages_ready", + selector: { + matchLabels: {queue: 'worker_tasks'} + } + }, + target: { + type: "Value", + value: "30" + } + } + } + ] + } + } + ); + + expect(hpa.getMetricValues(hpa.getMetrics()[0])).toEqual("unknown / 30"); + }); }); }); \ No newline at end of file diff --git a/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts b/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts index 9219fd3284..a9de7f928b 100644 --- a/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts +++ b/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts @@ -37,10 +37,21 @@ export interface ContainerResourceMetricSource { } export interface ExternalMetricSource { - metricName: string; + metricName?: string; metricSelector?: LabelSelector; targetAverageValue?: string; targetValue?: string; + + // autoscaling/v2 + metric?: { + name?: string; + selector?: LabelSelector; + }, + target?: { + type: string; + value?: string; + averageValue?: string; + } } export interface ObjectMetricSource { @@ -364,8 +375,8 @@ function getExternalMetricValue(currentMetric: ExternalMetricStatus | undefined, ?? currentMetric?.currentAverageValue ), target: ( - targetMetric?.targetValue - ?? targetMetric?.targetAverageValue + targetMetric?.target?.value + ?? `${targetMetric?.target?.averageValue} (avg)` ), }; }