diff --git a/src/renderer/api/__tests__/crd.test.ts b/src/renderer/api/__tests__/crd.test.ts new file mode 100644 index 0000000000..faa6334e37 --- /dev/null +++ b/src/renderer/api/__tests__/crd.test.ts @@ -0,0 +1,91 @@ +import { CustomResourceDefinition } from "../endpoints"; +import { IKubeObjectMetadata } from "../kube-object"; + +describe("Crds", () => { + describe("getVersion", () => { + it("should get the first version name from the list of versions", () => { + const crd = new CustomResourceDefinition({ + apiVersion: "foo", + kind: "CustomResourceDefinition", + metadata: {} as IKubeObjectMetadata, + }); + + crd.spec = { + versions: [ + { + name: "123", + served: false, + storage: false, + } + ] + } as any; + + expect(crd.getVersion()).toBe("123"); + }); + + it("should get the first version name from the list of versions (length 2)", () => { + const crd = new CustomResourceDefinition({ + apiVersion: "foo", + kind: "CustomResourceDefinition", + metadata: {} as IKubeObjectMetadata, + }); + + crd.spec = { + versions: [ + { + name: "123", + served: false, + storage: false, + }, + { + name: "1234", + served: false, + storage: false, + } + ] + } as any; + + expect(crd.getVersion()).toBe("123"); + }); + + it("should get the first version name from the list of versions (length 2) even with version field", () => { + const crd = new CustomResourceDefinition({ + apiVersion: "foo", + kind: "CustomResourceDefinition", + metadata: {} as IKubeObjectMetadata, + }); + + crd.spec = { + version: "abc", + versions: [ + { + name: "123", + served: false, + storage: false, + }, + { + name: "1234", + served: false, + storage: false, + } + ] + } as any; + + expect(crd.getVersion()).toBe("123"); + }); + + it("should get the first version name from the version field", () => { + const crd = new CustomResourceDefinition({ + apiVersion: "foo", + kind: "CustomResourceDefinition", + metadata: {} as IKubeObjectMetadata, + }); + + crd.spec = { + version: "abc" + } as any; + + expect(crd.getVersion()).toBe("abc"); + }); + }); +}); diff --git a/src/renderer/api/endpoints/crd.api.ts b/src/renderer/api/endpoints/crd.api.ts index ad7d9d67ca..f51df634fe 100644 --- a/src/renderer/api/endpoints/crd.api.ts +++ b/src/renderer/api/endpoints/crd.api.ts @@ -33,7 +33,7 @@ export class CustomResourceDefinition extends KubeObject { }; scope: "Namespaced" | "Cluster" | string; validation?: any; - versions: { + versions?: { name: string; served: boolean; storage: boolean; @@ -103,7 +103,7 @@ export class CustomResourceDefinition extends KubeObject { getVersion() { // v1 has removed the spec.version property, if it is present it must match the first version - return this.spec.versions[0]?.name ?? this.spec.version; + return this.spec.versions?.[0]?.name ?? this.spec.version; } isNamespaced() { @@ -123,7 +123,7 @@ export class CustomResourceDefinition extends KubeObject { } getPrinterColumns(ignorePriority = true): AdditionalPrinterColumnsV1[] { - const columns = this.spec.versions.find(a => this.getVersion() == a.name)?.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 ?? [];