mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Update CRD api to use preferred version and implement v1 differences
Signed-off-by: Trevor Nichols <trevor@nichols.id.au>
This commit is contained in:
parent
5788c44303
commit
b1d15d44fe
@ -1,13 +1,29 @@
|
|||||||
import { KubeObject } from "../kube-object";
|
import { KubeObject } from "../kube-object";
|
||||||
import { KubeApi } from "../kube-api";
|
import { VersionedKubeApi } from "../kube-api-versioned";
|
||||||
import { crdResourcesURL } from "../../components/+custom-resources/crd.route";
|
import { crdResourcesURL } from "../../components/+custom-resources/crd.route";
|
||||||
|
|
||||||
|
|
||||||
|
type AdditionalPrinterColumnsCommon = {
|
||||||
|
name: string;
|
||||||
|
type: "integer" | "number" | "string" | "boolean" | "date";
|
||||||
|
priority: number;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type AdditionalPrinterColumnsV1 = AdditionalPrinterColumnsCommon & {
|
||||||
|
jsonPath: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type AdditionalPrinterColumnsV1Beta = AdditionalPrinterColumnsCommon & {
|
||||||
|
JSONPath: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class CustomResourceDefinition extends KubeObject {
|
export class CustomResourceDefinition extends KubeObject {
|
||||||
static kind = "CustomResourceDefinition";
|
static kind = "CustomResourceDefinition";
|
||||||
|
|
||||||
spec: {
|
spec: {
|
||||||
group: string;
|
group: string;
|
||||||
version: string;
|
version?: string;
|
||||||
names: {
|
names: {
|
||||||
plural: string;
|
plural: string;
|
||||||
singular: string;
|
singular: string;
|
||||||
@ -20,18 +36,13 @@ export class CustomResourceDefinition extends KubeObject {
|
|||||||
name: string;
|
name: string;
|
||||||
served: boolean;
|
served: boolean;
|
||||||
storage: boolean;
|
storage: boolean;
|
||||||
|
additionalPrinterColumns?: AdditionalPrinterColumnsV1[]
|
||||||
}[];
|
}[];
|
||||||
conversion: {
|
conversion: {
|
||||||
strategy?: string;
|
strategy?: string;
|
||||||
webhook?: any;
|
webhook?: any;
|
||||||
};
|
};
|
||||||
additionalPrinterColumns?: {
|
additionalPrinterColumns?: AdditionalPrinterColumnsV1Beta[];
|
||||||
name: string;
|
|
||||||
type: "integer" | "number" | "string" | "boolean" | "date";
|
|
||||||
priority: number;
|
|
||||||
description: string;
|
|
||||||
JSONPath: string;
|
|
||||||
}[];
|
|
||||||
}
|
}
|
||||||
status: {
|
status: {
|
||||||
conditions: {
|
conditions: {
|
||||||
@ -61,8 +72,8 @@ export class CustomResourceDefinition extends KubeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getResourceApiBase() {
|
getResourceApiBase() {
|
||||||
const { version, group } = this.spec;
|
const { group } = this.spec;
|
||||||
return `/apis/${group}/${version}/${this.getPluralName()}`
|
return `/apis/${group}/${this.getVersion()}/${this.getPluralName()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
getPluralName() {
|
getPluralName() {
|
||||||
@ -87,7 +98,7 @@ export class CustomResourceDefinition extends KubeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getVersion() {
|
getVersion() {
|
||||||
return this.spec.version;
|
return this.spec.version ?? this.spec.versions.find(a => a.storage)?.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
isNamespaced() {
|
isNamespaced() {
|
||||||
@ -107,7 +118,9 @@ export class CustomResourceDefinition extends KubeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getPrinterColumns(ignorePriority = true) {
|
getPrinterColumns(ignorePriority = true) {
|
||||||
const columns = this.spec.additionalPrinterColumns || [];
|
const columns = this.spec.versions.find(a => this.getVersion() == a.name)?.additionalPrinterColumns
|
||||||
|
?? this.spec.additionalPrinterColumns?.map(({JSONPath, ...rest}) => ({ ...rest, jsonPath: JSONPath })) // map to V1 shape
|
||||||
|
?? [];
|
||||||
return columns
|
return columns
|
||||||
.filter(column => column.name != "Age")
|
.filter(column => column.name != "Age")
|
||||||
.filter(column => ignorePriority ? true : !column.priority);
|
.filter(column => ignorePriority ? true : !column.priority);
|
||||||
@ -130,16 +143,10 @@ export class CustomResourceDefinition extends KubeObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const crdBetaApi = new KubeApi<CustomResourceDefinition>({
|
export const crdApi = new VersionedKubeApi<CustomResourceDefinition>({
|
||||||
kind: CustomResourceDefinition.kind,
|
|
||||||
apiBase: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions",
|
|
||||||
isNamespaced: false,
|
|
||||||
objectConstructor: CustomResourceDefinition,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const crdApi = new KubeApi<CustomResourceDefinition>({
|
|
||||||
kind: CustomResourceDefinition.kind,
|
kind: CustomResourceDefinition.kind,
|
||||||
apiBase: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions",
|
apiBase: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions",
|
||||||
isNamespaced: false,
|
isNamespaced: false,
|
||||||
objectConstructor: CustomResourceDefinition,
|
objectConstructor: CustomResourceDefinition
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
37
src/renderer/api/kube-api-versioned.ts
Normal file
37
src/renderer/api/kube-api-versioned.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { stringify } from "querystring";
|
||||||
|
import { KubeObject } from "./kube-object";
|
||||||
|
import { createKubeApiURL } from "./kube-api-parse";
|
||||||
|
import { KubeApi, IKubeApiQueryParams } from "./kube-api";
|
||||||
|
|
||||||
|
export class VersionedKubeApi<T extends KubeObject = any> extends KubeApi<T> {
|
||||||
|
private preferredVersion?: string;
|
||||||
|
|
||||||
|
async getPreferredVersion() {
|
||||||
|
if (this.preferredVersion) return;
|
||||||
|
|
||||||
|
const apiGroupVersion = await this.request.get<{ preferredVersion: { version: string; }; }>(`${this.apiPrefix}/${this.apiGroup}`);
|
||||||
|
this.preferredVersion = apiGroupVersion.preferredVersion.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
async list({ namespace = "" } = {}, query?: IKubeApiQueryParams): Promise<T[]> {
|
||||||
|
await this.getPreferredVersion();
|
||||||
|
return await super.list({namespace}, query);
|
||||||
|
}
|
||||||
|
async get({ name = "", namespace = "default" } = {}, query?: IKubeApiQueryParams): Promise<T> {
|
||||||
|
await this.getPreferredVersion();
|
||||||
|
return super.get({ name, namespace }, query);
|
||||||
|
}
|
||||||
|
|
||||||
|
getUrl({ name = "", namespace = "" } = {}, query?: Partial<IKubeApiQueryParams>) {
|
||||||
|
const { apiPrefix, apiGroup, apiVersion, apiResource, preferredVersion, isNamespaced } = this;
|
||||||
|
|
||||||
|
const resourcePath = createKubeApiURL({
|
||||||
|
apiPrefix: apiPrefix,
|
||||||
|
apiVersion: `${apiGroup}/${preferredVersion ?? apiVersion}`,
|
||||||
|
resource: apiResource,
|
||||||
|
namespace: isNamespaced ? namespace : undefined,
|
||||||
|
name: name,
|
||||||
|
});
|
||||||
|
return resourcePath + (query ? `?` + stringify(query) : "");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -102,13 +102,13 @@ export class CRDDetails extends React.Component<Props> {
|
|||||||
</TableHead>
|
</TableHead>
|
||||||
{
|
{
|
||||||
printerColumns.map((column, index) => {
|
printerColumns.map((column, index) => {
|
||||||
const { name, type, JSONPath } = column;
|
const { name, type, jsonPath } = column;
|
||||||
return (
|
return (
|
||||||
<TableRow key={index}>
|
<TableRow key={index}>
|
||||||
<TableCell className="name">{name}</TableCell>
|
<TableCell className="name">{name}</TableCell>
|
||||||
<TableCell className="type">{type}</TableCell>
|
<TableCell className="type">{type}</TableCell>
|
||||||
<TableCell className="json-path">
|
<TableCell className="json-path">
|
||||||
<Badge label={JSONPath}/>
|
<Badge label={jsonPath}/>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -57,7 +57,7 @@ export class CrdResourceDetails extends React.Component<Props> {
|
|||||||
<KubeObjectMeta object={object}/>
|
<KubeObjectMeta object={object}/>
|
||||||
{extraColumns.map(column => {
|
{extraColumns.map(column => {
|
||||||
const { name } = column;
|
const { name } = column;
|
||||||
const value = jsonPath.query(object, column.JSONPath.slice(1));
|
const value = jsonPath.query(object, (column.jsonPath).slice(1));
|
||||||
return (
|
return (
|
||||||
<DrawerItem key={name} name={name}>
|
<DrawerItem key={name} name={name}>
|
||||||
<CrdColumnValue value={value} />
|
<CrdColumnValue value={value} />
|
||||||
|
|||||||
@ -57,7 +57,7 @@ export class CrdResources extends React.Component<Props> {
|
|||||||
[sortBy.age]: (item: KubeObject) => item.metadata.creationTimestamp,
|
[sortBy.age]: (item: KubeObject) => item.metadata.creationTimestamp,
|
||||||
}
|
}
|
||||||
extraColumns.forEach(column => {
|
extraColumns.forEach(column => {
|
||||||
sortingCallbacks[column.name] = (item: KubeObject) => jsonPath.query(item, column.JSONPath.slice(1))
|
sortingCallbacks[column.name] = (item: KubeObject) => jsonPath.query(item, column.jsonPath.slice(1))
|
||||||
})
|
})
|
||||||
// todo: merge extra columns and other params to predefined view
|
// todo: merge extra columns and other params to predefined view
|
||||||
const { List } = apiManager.getViews(crd.getResourceApiBase());
|
const { List } = apiManager.getViews(crd.getResourceApiBase());
|
||||||
@ -88,9 +88,9 @@ export class CrdResources extends React.Component<Props> {
|
|||||||
renderTableContents={(crdInstance: KubeObject) => [
|
renderTableContents={(crdInstance: KubeObject) => [
|
||||||
crdInstance.getName(),
|
crdInstance.getName(),
|
||||||
isNamespaced && crdInstance.getNs(),
|
isNamespaced && crdInstance.getNs(),
|
||||||
...extraColumns.map(column =>
|
...extraColumns.map(column => {
|
||||||
jsonPath.query(crdInstance, column.JSONPath.slice(1))
|
return jsonPath.query(crdInstance, (column.jsonPath).slice(1))
|
||||||
),
|
}),
|
||||||
crdInstance.getAge(),
|
crdInstance.getAge(),
|
||||||
]}
|
]}
|
||||||
renderItemMenu={(item: KubeObject) => {
|
renderItemMenu={(item: KubeObject) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user