/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./hpa-details.scss"; import React from "react"; import { observer } from "mobx-react"; import { Link } from "react-router-dom"; import { DrawerItem, DrawerTitle } from "../drawer"; import { Badge } from "../badge"; import type { KubeObjectDetailsProps } from "../kube-object-details"; import { cssNames } from "../../utils"; import { HorizontalPodAutoscaler, HpaMetricType, IHpaMetric } from "../../../common/k8s-api/endpoints/hpa.api"; import { Table, TableCell, TableHead, TableRow } from "../table"; import { apiManager } from "../../../common/k8s-api/api-manager"; import { KubeObjectMeta } from "../kube-object-meta"; import { getDetailsUrl } from "../kube-detail-params"; import logger from "../../../common/logger"; export interface HpaDetailsProps extends KubeObjectDetailsProps { } @observer export class HpaDetails extends React.Component { renderMetrics() { const { object: hpa } = this.props; const renderName = (metric: IHpaMetric) => { switch (metric.type) { case HpaMetricType.Resource: { const addition = metric.resource.targetAverageUtilization ? "(as a percentage of request)" : ""; return <>Resource {metric.resource.name} on Pods {addition}; } case HpaMetricType.Pods: return <>{metric.pods.metricName} on Pods; case HpaMetricType.Object: { const { target } = metric.object; const { kind, name } = target; const objectUrl = getDetailsUrl(apiManager.lookupApiLink(target, hpa)); return ( <> {metric.object.metricName} on{" "} {kind}/{name} ); } case HpaMetricType.External: return ( <> {metric.external.metricName} on{" "} {JSON.stringify(metric.external.selector)} ); } }; return ( Name Current / Target { hpa.getMetrics() .map((metric, index) => ( {renderName(metric)} {hpa.getMetricValues(metric)} )) }
); } render() { const { object: hpa } = this.props; if (!hpa) { return null; } if (!(hpa instanceof HorizontalPodAutoscaler)) { logger.error("[HpaDetails]: passed object that is not an instanceof HorizontalPodAutoscaler", hpa); return null; } const { scaleTargetRef } = hpa.spec; return (
{scaleTargetRef && ( {scaleTargetRef.kind}/{scaleTargetRef.name} )} {hpa.getMinPods()} {hpa.getMaxPods()} {hpa.getReplicas()} {hpa.getConditions().map(({ type, tooltip, isReady }) => { if (!isReady) return null; return ( ); })}
{this.renderMetrics()}
); } }