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:
parent
fc1aa513f4
commit
db14202996
@ -70,7 +70,7 @@ export interface V2Beta1ExternalMetricSource {
|
|||||||
targetValue?: string;
|
targetValue?: string;
|
||||||
metric?: {
|
metric?: {
|
||||||
selector?: LabelSelector;
|
selector?: LabelSelector;
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ExternalMetricSource =
|
export type ExternalMetricSource =
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,9 +5,12 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import type { HorizontalPodAutoscaler, HorizontalPodAutoscalerMetricSpec, HorizontalPodAutoscalerMetricStatus } from "../../../common/k8s-api/endpoints";
|
import type { HorizontalPodAutoscaler, HorizontalPodAutoscalerMetricSpec, HorizontalPodAutoscalerMetricStatus } from "../../../common/k8s-api/endpoints";
|
||||||
import { HpaMetricType } 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 { HorizontalPodAutoscalerV1MetricParser } from "./hpa-v1-metric-parser";
|
||||||
import { HorizontalPodAutoscalerV2MetricParser } from "./hpa-v2-metric-parser";
|
import { HorizontalPodAutoscalerV2MetricParser } from "./hpa-v2-metric-parser";
|
||||||
|
|
||||||
|
type Parser = HorizontalPodAutoscalerV1MetricParser | HorizontalPodAutoscalerV2MetricParser;
|
||||||
|
|
||||||
const getHorizontalPodAutoscalerMetrics = getInjectable({
|
const getHorizontalPodAutoscalerMetrics = getInjectable({
|
||||||
id: "get-horizontal-pod-autoscaler-metrics",
|
id: "get-horizontal-pod-autoscaler-metrics",
|
||||||
instantiate: () => (hpa: HorizontalPodAutoscaler) => {
|
instantiate: () => (hpa: HorizontalPodAutoscaler) => {
|
||||||
@ -30,8 +33,8 @@ const getHorizontalPodAutoscalerMetrics = getInjectable({
|
|||||||
&& getMetricName(current) === getMetricName(metric),
|
&& getMetricName(current) === getMetricName(metric),
|
||||||
);
|
);
|
||||||
|
|
||||||
const h2Values = getMetricValues(hpaV2Parser, currentMetric, metric);
|
const h2Values = getMetricValues<HorizontalPodAutoscalerV2MetricParser>(hpaV2Parser, currentMetric, metric);
|
||||||
const h1Values = getMetricValues(hpaV1Parser, currentMetric, metric);
|
const h1Values = getMetricValues<HorizontalPodAutoscalerV1MetricParser>(hpaV1Parser, currentMetric, metric);
|
||||||
let values = h1Values;
|
let values = h1Values;
|
||||||
|
|
||||||
if (h2Values.current || h2Values.target) {
|
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) {
|
switch (target.type) {
|
||||||
case HpaMetricType.Resource:
|
case HpaMetricType.Resource:
|
||||||
return parser.getResource({ current: current?.resource, target: target.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;
|
export default getHorizontalPodAutoscalerMetrics;
|
||||||
|
|||||||
@ -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 getDetailsUrlInjectable from "../kube-detail-params/get-details-url.injectable";
|
||||||
import loggerInjectable from "../../../common/logger.injectable";
|
import loggerInjectable from "../../../common/logger.injectable";
|
||||||
import getHorizontalPodAutoscalerMetrics from "./get-hpa-metrics.injectable";
|
import getHorizontalPodAutoscalerMetrics from "./get-hpa-metrics.injectable";
|
||||||
|
import { getMetricName } from "./get-hpa-metric-name";
|
||||||
|
|
||||||
export interface HpaDetailsProps extends KubeObjectDetailsProps<HorizontalPodAutoscaler> {
|
export interface HpaDetailsProps extends KubeObjectDetailsProps<HorizontalPodAutoscaler> {
|
||||||
}
|
}
|
||||||
@ -59,32 +60,31 @@ class NonInjectedHpaDetails extends React.Component<HpaDetailsProps & Dependenci
|
|||||||
const { object: hpa } = this.props;
|
const { object: hpa } = this.props;
|
||||||
|
|
||||||
const renderName = (metric: HorizontalPodAutoscalerMetricSpec) => {
|
const renderName = (metric: HorizontalPodAutoscalerMetricSpec) => {
|
||||||
|
const metricName = getMetricName(metric);
|
||||||
|
|
||||||
switch (metric.type) {
|
switch (metric.type) {
|
||||||
case HpaMetricType.ContainerResource:
|
case HpaMetricType.ContainerResource:
|
||||||
|
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case HpaMetricType.Resource: {
|
case HpaMetricType.Resource: {
|
||||||
const metricSpec = metric.resource ?? metric.containerResource;
|
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:
|
case HpaMetricType.Pods:
|
||||||
return `${metric.pods.metricName ?? metric.pods.metric?.name} on Pods`;
|
return `${metricName} on Pods`;
|
||||||
|
|
||||||
case HpaMetricType.Object: {
|
case HpaMetricType.Object: {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{metric.object.metricName || metric.object.metric?.name}
|
{metricName}
|
||||||
{" "}
|
{" "}
|
||||||
{this.renderTargetLink(metric.object.describedObject)}
|
{this.renderTargetLink(metric.object?.describedObject)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case HpaMetricType.External:
|
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)}`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user