1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/+custom-resources/crd-resource-details.tsx
Sebastian Malton dc60517e48 Fix custom resources not having a fallback details component
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2021-08-03 11:22:37 -04:00

115 lines
3.8 KiB
TypeScript

/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import "./crd-resource-details.scss";
import React from "react";
import jsonPath from "jsonpath";
import { observer } from "mobx-react";
import { cssNames } from "../../utils";
import { Badge } from "../badge";
import { DrawerItem } from "../drawer";
import type { KubeObjectDetailsProps } from "../kube-object";
import { KubeObjectMeta } from "../kube-object/kube-object-meta";
import { Input } from "../input";
import type { AdditionalPrinterColumnsV1, CustomResourceDefinition } from "../../api/endpoints/crd.api";
import { parseJsonPath } from "../../utils/jsonPath";
import type { KubeObject, KubeObjectMetadata, KubeObjectStatus } from "../../api/kube-object";
interface Props extends KubeObjectDetailsProps<KubeObject> {
crd: CustomResourceDefinition;
}
function convertSpecValue(value: any): any {
if (Array.isArray(value)) {
return value.map(convertSpecValue);
}
if (typeof value === "object") {
return (
<Input
readOnly
multiLine
theme="round-black"
className="box grow"
value={JSON.stringify(value, null, 2)}
/>
);
}
return value;
}
@observer
export class CrdResourceDetails extends React.Component<Props> {
renderAdditionalColumns(resource: KubeObject, columns: AdditionalPrinterColumnsV1[]) {
return columns.map(({ name, jsonPath: jp }) => (
<DrawerItem key={name} name={name} renderBoolean>
{convertSpecValue(jsonPath.value(resource, parseJsonPath(jp.slice(1))))}
</DrawerItem>
));
}
renderStatus(crd: KubeObject<KubeObjectMetadata, KubeObjectStatus, any>, columns: AdditionalPrinterColumnsV1[]) {
const showStatus = !columns.find(column => column.name == "Status") && crd.status?.conditions;
if (!showStatus) {
return null;
}
const conditions = crd.status.conditions
.filter(({ type, reason }) => type || reason)
.map(({ type, reason, message, status }) => ({ kind: type || reason, message, status }))
.map(({ kind, message, status }, index) => (
<Badge
key={kind + index} label={kind}
disabled={status === "False"}
className={kind.toLowerCase()}
tooltip={message}
/>
));
return (
<DrawerItem name="Status" className="status" labelsOnly>
{conditions}
</DrawerItem>
);
}
render() {
const { props: { object, crd } } = this;
if (!object || !crd) {
return null;
}
const extraColumns = crd.getPrinterColumns();
return (
<div className={cssNames("CrdResourceDetails", crd.getResourceKind())}>
<KubeObjectMeta object={object} />
{this.renderAdditionalColumns(object, extraColumns)}
{this.renderStatus(object, extraColumns)}
</div>
);
}
}