1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Use the served and storage version of a CRD instead of the first

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-10-08 12:03:59 -04:00
parent 7193231422
commit 67407bcb2d
2 changed files with 31 additions and 6 deletions

View File

@ -19,10 +19,11 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { KubeObject } from "../kube-object"; import { KubeCreationError, KubeObject } from "../kube-object";
import { KubeApi } from "../kube-api"; import { KubeApi } from "../kube-api";
import { crdResourcesURL } from "../../routes"; import { crdResourcesURL } from "../../routes";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing"; import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { KubeJsonApiData } from "../kube-json-api";
type AdditionalPrinterColumnsCommon = { type AdditionalPrinterColumnsCommon = {
name: string; name: string;
@ -42,7 +43,10 @@ type AdditionalPrinterColumnsV1Beta = AdditionalPrinterColumnsCommon & {
export interface CustomResourceDefinition { export interface CustomResourceDefinition {
spec: { spec: {
group: string; group: string;
version?: string; // deprecated in v1 api /**
* This is deprecated for apiextensions.k8s.io/v1 but used previously
*/
version?: string;
names: { names: {
plural: string; plural: string;
singular: string; singular: string;
@ -88,6 +92,14 @@ export class CustomResourceDefinition extends KubeObject {
static namespaced = false; static namespaced = false;
static apiBase = "/apis/apiextensions.k8s.io/v1/customresourcedefinitions"; static apiBase = "/apis/apiextensions.k8s.io/v1/customresourcedefinitions";
constructor(data: KubeJsonApiData) {
super(data);
if (!data.spec || typeof data.spec !== "object") {
throw new KubeCreationError("Cannot create a CustomResourceDefinition from an object without spec", data);
}
}
getResourceUrl() { getResourceUrl() {
return crdResourcesURL({ return crdResourcesURL({
params: { params: {
@ -126,8 +138,22 @@ export class CustomResourceDefinition extends KubeObject {
} }
getVersion() { getVersion() {
// v1 has removed the spec.version property, if it is present it must match the first version // Prefer the modern `versions` over the legacy `version`
return this.spec.versions?.[0]?.name ?? this.spec.version; for (const version of this.spec.versions || []) {
/**
* If the version is not served then 404 errors will occur
* We should also prefer the storage version
*/
if (version.served && version.storage) {
return version.name;
}
}
if (this.spec.version) {
return this.spec.version;
}
throw new Error(`Failed to find a version for CustomResourceDefinition ${this.metadata.name}`);
} }
isNamespaced() { isNamespaced() {

View File

@ -27,8 +27,7 @@ import { stringify } from "querystring";
import { EventEmitter } from "../../common/event-emitter"; import { EventEmitter } from "../../common/event-emitter";
import logger from "../../common/logger"; import logger from "../../common/logger";
export interface JsonApiData { export type JsonApiData = Record<string, any>;
}
export interface JsonApiError { export interface JsonApiError {
code?: number; code?: number;