From db14202996e6445e8997111465e0e3b5365e424d Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Fri, 20 Jan 2023 13:44:49 +0300 Subject: [PATCH] Move getMetricName() to its own file Signed-off-by: Alex Andreev --- .../horizontal-pod-autoscaler.api.ts | 2 +- .../get-hpa-metric-name.ts | 37 +++++++++++++++++++ .../get-hpa-metrics.injectable.ts | 26 +++---------- .../+config-autoscalers/hpa-details.tsx | 16 ++++---- 4 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 src/renderer/components/+config-autoscalers/get-hpa-metric-name.ts 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 762d3e6fae..ba5483da2c 100644 --- a/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts +++ b/src/common/k8s-api/endpoints/horizontal-pod-autoscaler.api.ts @@ -70,7 +70,7 @@ export interface V2Beta1ExternalMetricSource { targetValue?: string; metric?: { selector?: LabelSelector; - } + }; } export type ExternalMetricSource = diff --git a/src/renderer/components/+config-autoscalers/get-hpa-metric-name.ts b/src/renderer/components/+config-autoscalers/get-hpa-metric-name.ts new file mode 100644 index 0000000000..f92b249909 --- /dev/null +++ b/src/renderer/components/+config-autoscalers/get-hpa-metric-name.ts @@ -0,0 +1,37 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { HpaMetricType } from "../../../common/k8s-api/endpoints"; +import type { LabelSelector } from "../../../common/k8s-api/kube-object"; + +type MetricNames = Partial>; + +interface Metric extends MetricNames { + type: HpaMetricType; +} + +export function getMetricName(metric: Metric): string | undefined { + switch (metric.type) { + case HpaMetricType.Resource: + return metric.resource?.name; + case HpaMetricType.Pods: + return metric.pods?.metricName || metric.pods?.metric?.name; + case HpaMetricType.Object: + return metric.object?.metricName || metric.object?.metric?.name; + case HpaMetricType.External: + return metric.external?.metricName || metric.external?.metric?.name; + case HpaMetricType.ContainerResource: + return metric.containerResource?.name; + default: + return undefined; + } +} diff --git a/src/renderer/components/+config-autoscalers/get-hpa-metrics.injectable.ts b/src/renderer/components/+config-autoscalers/get-hpa-metrics.injectable.ts index e49ad92152..ac524f0cd5 100644 --- a/src/renderer/components/+config-autoscalers/get-hpa-metrics.injectable.ts +++ b/src/renderer/components/+config-autoscalers/get-hpa-metrics.injectable.ts @@ -5,9 +5,12 @@ import { getInjectable } from "@ogre-tools/injectable"; import type { HorizontalPodAutoscaler, HorizontalPodAutoscalerMetricSpec, HorizontalPodAutoscalerMetricStatus } from "../../../common/k8s-api/endpoints"; import { HpaMetricType } from "../../../common/k8s-api/endpoints"; +import { getMetricName } from "./get-hpa-metric-name"; import { HorizontalPodAutoscalerV1MetricParser } from "./hpa-v1-metric-parser"; import { HorizontalPodAutoscalerV2MetricParser } from "./hpa-v2-metric-parser"; +type Parser = HorizontalPodAutoscalerV1MetricParser | HorizontalPodAutoscalerV2MetricParser; + const getHorizontalPodAutoscalerMetrics = getInjectable({ id: "get-horizontal-pod-autoscaler-metrics", instantiate: () => (hpa: HorizontalPodAutoscaler) => { @@ -30,8 +33,8 @@ const getHorizontalPodAutoscalerMetrics = getInjectable({ && getMetricName(current) === getMetricName(metric), ); - const h2Values = getMetricValues(hpaV2Parser, currentMetric, metric); - const h1Values = getMetricValues(hpaV1Parser, currentMetric, metric); + const h2Values = getMetricValues(hpaV2Parser, currentMetric, metric); + const h1Values = getMetricValues(hpaV1Parser, currentMetric, metric); let values = h1Values; if (h2Values.current || h2Values.target) { @@ -43,7 +46,7 @@ const getHorizontalPodAutoscalerMetrics = getInjectable({ }, }); -function getMetricValues(parser: HorizontalPodAutoscalerV2MetricParser, current: HorizontalPodAutoscalerMetricStatus | undefined, target: HorizontalPodAutoscalerMetricSpec) { +function getMetricValues(parser: Type, current: HorizontalPodAutoscalerMetricStatus | undefined, target: HorizontalPodAutoscalerMetricSpec) { switch (target.type) { case HpaMetricType.Resource: return parser.getResource({ current: current?.resource, target: target.resource }); @@ -60,21 +63,4 @@ function getMetricValues(parser: HorizontalPodAutoscalerV2MetricParser, current: } } -function getMetricName(metric: HorizontalPodAutoscalerMetricSpec | HorizontalPodAutoscalerMetricStatus): string | undefined { - switch (metric.type) { - case HpaMetricType.Resource: - return metric.resource.name; - case HpaMetricType.Pods: - return metric.pods.metricName || metric.pods.metric?.name; - case HpaMetricType.Object: - return metric.object.metricName || metric.object.metric?.name; - case HpaMetricType.External: - return metric.external.metricName || metric.external.metric?.name; - case HpaMetricType.ContainerResource: - return metric.containerResource.name; - default: - return undefined; - } -} - export default getHorizontalPodAutoscalerMetrics; diff --git a/src/renderer/components/+config-autoscalers/hpa-details.tsx b/src/renderer/components/+config-autoscalers/hpa-details.tsx index 732cca4ea6..96c1a7e20b 100644 --- a/src/renderer/components/+config-autoscalers/hpa-details.tsx +++ b/src/renderer/components/+config-autoscalers/hpa-details.tsx @@ -23,6 +23,7 @@ import apiManagerInjectable from "../../../common/k8s-api/api-manager/manager.in import getDetailsUrlInjectable from "../kube-detail-params/get-details-url.injectable"; import loggerInjectable from "../../../common/logger.injectable"; import getHorizontalPodAutoscalerMetrics from "./get-hpa-metrics.injectable"; +import { getMetricName } from "./get-hpa-metric-name"; export interface HpaDetailsProps extends KubeObjectDetailsProps { } @@ -59,32 +60,31 @@ class NonInjectedHpaDetails extends React.Component { + const metricName = getMetricName(metric); + switch (metric.type) { case HpaMetricType.ContainerResource: // fallthrough case HpaMetricType.Resource: { const metricSpec = metric.resource ?? metric.containerResource; - const addition = metricSpec.targetAverageUtilization - ? " (as a percentage of request)" - : ""; - return `Resource ${metricSpec.name} on Pods${addition}`; + return `Resource ${metricSpec.name} on Pods`; } case HpaMetricType.Pods: - return `${metric.pods.metricName ?? metric.pods.metric?.name} on Pods`; + return `${metricName} on Pods`; case HpaMetricType.Object: { return ( <> - {metric.object.metricName || metric.object.metric?.name} + {metricName} {" "} - {this.renderTargetLink(metric.object.describedObject)} + {this.renderTargetLink(metric.object?.describedObject)} ); } case HpaMetricType.External: - return `${metric.external.metricName ?? metric.external.metric?.name} on ${JSON.stringify(metric.external.metricSelector ?? metric.external.metric?.selector)}`; + return `${metricName} on ${JSON.stringify(metric.external.metricSelector ?? metric.external.metric?.selector)}`; } };