mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
First sketch of hpav2 metrics
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
c91a4cb5d0
commit
fbd5bf0075
@ -28,6 +28,12 @@ export interface ContainerResourceMetricSource {
|
|||||||
name: string;
|
name: string;
|
||||||
targetAverageUtilization?: number;
|
targetAverageUtilization?: number;
|
||||||
targetAverageValue?: string;
|
targetAverageValue?: string;
|
||||||
|
|
||||||
|
// autscaling/v2
|
||||||
|
target?: {
|
||||||
|
averageUtilization?: number;
|
||||||
|
type?: string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExternalMetricSource {
|
export interface ExternalMetricSource {
|
||||||
@ -46,15 +52,30 @@ export interface ObjectMetricSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface PodsMetricSource {
|
export interface PodsMetricSource {
|
||||||
metricName: string;
|
metricName?: string;
|
||||||
selector?: LabelSelector;
|
selector?: LabelSelector;
|
||||||
targetAverageValue: string;
|
targetAverageValue?: string;
|
||||||
|
|
||||||
|
// autoscaling/v2
|
||||||
|
metric?: {
|
||||||
|
name?: string;
|
||||||
|
}
|
||||||
|
target?: {
|
||||||
|
averageValue?: string;
|
||||||
|
type?: string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResourceMetricSource {
|
export interface ResourceMetricSource {
|
||||||
name: string;
|
name: string;
|
||||||
targetAverageUtilization?: number;
|
targetAverageUtilization?: number;
|
||||||
targetAverageValue?: string;
|
targetAverageValue?: string;
|
||||||
|
|
||||||
|
// autoscaling/v2
|
||||||
|
target?: {
|
||||||
|
averageUtilization?: number;
|
||||||
|
type?: string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BaseHorizontalPodAutoscalerMetricSpec {
|
export interface BaseHorizontalPodAutoscalerMetricSpec {
|
||||||
@ -206,10 +227,12 @@ export class HorizontalPodAutoscaler extends KubeObject<
|
|||||||
}
|
}
|
||||||
|
|
||||||
getMetrics() {
|
getMetrics() {
|
||||||
|
// console.log(this.spec)
|
||||||
return this.spec.metrics ?? [];
|
return this.spec.metrics ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentMetrics() {
|
getCurrentMetrics() {
|
||||||
|
// console.log(this.status)
|
||||||
return this.status?.currentMetrics ?? [];
|
return this.status?.currentMetrics ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,10 +240,35 @@ export class HorizontalPodAutoscaler extends KubeObject<
|
|||||||
const {
|
const {
|
||||||
current = "unknown",
|
current = "unknown",
|
||||||
target = "unknown",
|
target = "unknown",
|
||||||
} = getMetricCurrentTarget(metric, this.getCurrentMetrics());
|
} = this.getMetricsCurrentAndTarget(metric);
|
||||||
|
|
||||||
return `${current} / ${target}`;
|
return `${current} / ${target}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMetricsCurrentAndTarget(spec: HorizontalPodAutoscalerMetricSpec): MetricCurrentTarget {
|
||||||
|
const currentMetrics = this.getCurrentMetrics();
|
||||||
|
const currentMetric = currentMetrics.find(m => (
|
||||||
|
m.type === spec.type
|
||||||
|
&& getMetricName(m) === getMetricName(spec)
|
||||||
|
));
|
||||||
|
|
||||||
|
console.log(spec, currentMetrics)
|
||||||
|
|
||||||
|
switch (spec.type) {
|
||||||
|
case HpaMetricType.Resource:
|
||||||
|
return getResourceMetricValue(currentMetric?.resource, spec.resource);
|
||||||
|
case HpaMetricType.Pods:
|
||||||
|
return getPodsMetricValue(currentMetric?.pods, spec.pods);
|
||||||
|
case HpaMetricType.Object:
|
||||||
|
return getObjectMetricValue(currentMetric?.object, spec.object);
|
||||||
|
case HpaMetricType.External:
|
||||||
|
return getExternalMetricValue(currentMetric?.external, spec.external);
|
||||||
|
case HpaMetricType.ContainerResource:
|
||||||
|
return getContainerResourceMetricValue(currentMetric?.containerResource, spec.containerResource);
|
||||||
|
default:
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class HorizontalPodAutoscalerApi extends KubeApi<HorizontalPodAutoscaler> {
|
export class HorizontalPodAutoscalerApi extends KubeApi<HorizontalPodAutoscaler> {
|
||||||
@ -228,13 +276,13 @@ export class HorizontalPodAutoscalerApi extends KubeApi<HorizontalPodAutoscaler>
|
|||||||
super(deps, {
|
super(deps, {
|
||||||
...opts ?? {},
|
...opts ?? {},
|
||||||
objectConstructor: HorizontalPodAutoscaler,
|
objectConstructor: HorizontalPodAutoscaler,
|
||||||
checkPreferredVersion: true,
|
// checkPreferredVersion: true,
|
||||||
// Kubernetes < 1.26
|
// // Kubernetes < 1.26
|
||||||
fallbackApiBases: [
|
// fallbackApiBases: [
|
||||||
"/apis/autoscaling/v2beta2/horizontalpodautoscalers",
|
// "/apis/autoscaling/v2beta2/horizontalpodautoscalers",
|
||||||
"/apis/autoscaling/v2beta1/horizontalpodautoscalers",
|
// "/apis/autoscaling/v2beta1/horizontalpodautoscalers",
|
||||||
"/apis/autoscaling/v1/horizontalpodautoscalers",
|
// "/apis/autoscaling/v1/horizontalpodautoscalers",
|
||||||
],
|
// ],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -257,25 +305,37 @@ function getMetricName(metric: HorizontalPodAutoscalerMetricSpec | HorizontalPod
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getResourceMetricValue(currentMetric: ResourceMetricStatus | undefined, targetMetric: ResourceMetricSource): MetricCurrentTarget {
|
function getResourceMetricValue(currentMetric: ResourceMetricStatus | undefined, targetMetric: ResourceMetricSource): MetricCurrentTarget {
|
||||||
|
let target = "unknown";
|
||||||
|
|
||||||
|
if (targetMetric.target) {
|
||||||
|
target = targetMetric.target.averageUtilization ? `${targetMetric.target.averageUtilization}%` : "unknown";
|
||||||
|
} else {
|
||||||
|
target = typeof targetMetric?.targetAverageUtilization === "number"
|
||||||
|
? `${targetMetric.targetAverageUtilization}%`
|
||||||
|
: targetMetric?.targetAverageValue ?? "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
current: (
|
current: (
|
||||||
typeof currentMetric?.currentAverageUtilization === "number"
|
typeof currentMetric?.currentAverageUtilization === "number"
|
||||||
? `${currentMetric.currentAverageUtilization}%`
|
? `${currentMetric.currentAverageUtilization}%`
|
||||||
: currentMetric?.currentAverageValue
|
: currentMetric?.currentAverageValue
|
||||||
),
|
),
|
||||||
target: (
|
target,
|
||||||
typeof targetMetric?.targetAverageUtilization === "number"
|
|
||||||
? `${targetMetric.targetAverageUtilization}%`
|
|
||||||
: targetMetric?.targetAverageValue
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPodsMetricValue(currentMetric: PodsMetricStatus | undefined, targetMetric: PodsMetricSource): MetricCurrentTarget {
|
function getPodsMetricValue(currentMetric: PodsMetricStatus | undefined, targetMetric: PodsMetricSource): MetricCurrentTarget {
|
||||||
return {
|
return {
|
||||||
current: currentMetric?.currentAverageValue,
|
current: currentMetric?.currentAverageValue,
|
||||||
target: targetMetric?.targetAverageValue,
|
target: targetMetric?.target?.averageValue,
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// v1
|
||||||
|
// return {
|
||||||
|
// current: currentMetric?.currentAverageValue,
|
||||||
|
// target: targetMetric?.targetAverageValue,
|
||||||
|
// };
|
||||||
}
|
}
|
||||||
|
|
||||||
function getObjectMetricValue(currentMetric: ObjectMetricStatus | undefined, targetMetric: ObjectMetricSource): MetricCurrentTarget {
|
function getObjectMetricValue(currentMetric: ObjectMetricStatus | undefined, targetMetric: ObjectMetricSource): MetricCurrentTarget {
|
||||||
@ -305,38 +365,24 @@ function getExternalMetricValue(currentMetric: ExternalMetricStatus | undefined,
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getContainerResourceMetricValue(currentMetric: ContainerResourceMetricStatus | undefined, targetMetric: ContainerResourceMetricSource): MetricCurrentTarget {
|
function getContainerResourceMetricValue(currentMetric: ContainerResourceMetricStatus | undefined, targetMetric: ContainerResourceMetricSource): MetricCurrentTarget {
|
||||||
|
let target = "unknown";
|
||||||
|
|
||||||
|
if (targetMetric.target) {
|
||||||
|
// v2
|
||||||
|
target = targetMetric.target.averageUtilization ? `${targetMetric.target.averageUtilization}%` : "unknown";
|
||||||
|
} else {
|
||||||
|
target = typeof targetMetric?.targetAverageUtilization === "number"
|
||||||
|
? `${targetMetric.targetAverageUtilization}%`
|
||||||
|
: targetMetric?.targetAverageValue ?? "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
current: (
|
current: (
|
||||||
typeof currentMetric?.currentAverageUtilization === "number"
|
typeof currentMetric?.currentAverageUtilization === "number"
|
||||||
? `${currentMetric.currentAverageUtilization}%`
|
? `${currentMetric.currentAverageUtilization}%`
|
||||||
: currentMetric?.currentAverageValue
|
: currentMetric?.currentAverageValue
|
||||||
),
|
),
|
||||||
target: (
|
target,
|
||||||
typeof targetMetric?.targetAverageUtilization === "number"
|
|
||||||
? `${targetMetric.targetAverageUtilization}%`
|
|
||||||
: targetMetric?.targetAverageValue
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMetricCurrentTarget(spec: HorizontalPodAutoscalerMetricSpec, status: HorizontalPodAutoscalerMetricStatus[]): MetricCurrentTarget {
|
|
||||||
const currentMetric = status.find(m => (
|
|
||||||
m.type === spec.type
|
|
||||||
&& getMetricName(m) === getMetricName(spec)
|
|
||||||
));
|
|
||||||
|
|
||||||
switch (spec.type) {
|
|
||||||
case HpaMetricType.Resource:
|
|
||||||
return getResourceMetricValue(currentMetric?.resource, spec.resource);
|
|
||||||
case HpaMetricType.Pods:
|
|
||||||
return getPodsMetricValue(currentMetric?.pods, spec.pods);
|
|
||||||
case HpaMetricType.Object:
|
|
||||||
return getObjectMetricValue(currentMetric?.object, spec.object);
|
|
||||||
case HpaMetricType.External:
|
|
||||||
return getExternalMetricValue(currentMetric?.external, spec.external);
|
|
||||||
case HpaMetricType.ContainerResource:
|
|
||||||
return getContainerResourceMetricValue(currentMetric?.containerResource, spec.containerResource);
|
|
||||||
default:
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user