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

Move getMetricName() to its own file

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2023-01-20 13:44:49 +03:00
parent fc1aa513f4
commit db14202996
4 changed files with 52 additions and 29 deletions

View File

@ -70,7 +70,7 @@ export interface V2Beta1ExternalMetricSource {
targetValue?: string;
metric?: {
selector?: LabelSelector;
}
};
}
export type ExternalMetricSource =

View File

@ -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<Record<"resource" | "pods" | "object" | "external" | "containerResource", {
name?: string;
metricName?: string;
metric?: {
name?: string;
selector?: LabelSelector;
};
}>>;
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;
}
}

View File

@ -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<HorizontalPodAutoscalerV2MetricParser>(hpaV2Parser, currentMetric, metric);
const h1Values = getMetricValues<HorizontalPodAutoscalerV1MetricParser>(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<Type extends Parser>(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;

View File

@ -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<HorizontalPodAutoscaler> {
}
@ -59,32 +60,31 @@ class NonInjectedHpaDetails extends React.Component<HpaDetailsProps & Dependenci
const { object: hpa } = this.props;
const renderName = (metric: HorizontalPodAutoscalerMetricSpec) => {
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)}`;
}
};