diff --git a/src/renderer/api/kube-api-versioned.ts b/src/renderer/api/kube-api-versioned.ts index ccd5244022..f6aa94c089 100644 --- a/src/renderer/api/kube-api-versioned.ts +++ b/src/renderer/api/kube-api-versioned.ts @@ -1,16 +1,35 @@ import { stringify } from "querystring"; import { KubeObject } from "./kube-object"; import { createKubeApiURL } from "./kube-api-parse"; -import { KubeApi, IKubeApiQueryParams } from "./kube-api"; +import { KubeApi, IKubeApiQueryParams, IKubeApiOptions } from "./kube-api"; +import { apiManager } from "./api-manager"; export class VersionedKubeApi extends KubeApi { private preferredVersion?: string; + constructor(opts: IKubeApiOptions) { + super(opts); + + this.getPreferredVersion().then(() => { + if (this.apiBase != opts.apiBase) + apiManager.registerApi(this.apiBase, this); + }); + } + + // override this property to make read-write + apiBase: string + async getPreferredVersion() { if (this.preferredVersion) return; - const apiGroupVersion = await this.request.get<{ preferredVersion: { version: string; }; }>(`${this.apiPrefix}/${this.apiGroup}`); + const apiGroupVersion = await this.request.get<{ preferredVersion?: { version: string; }; }>(`${this.apiPrefix}/${this.apiGroup}`); + + if (!apiGroupVersion?.preferredVersion) return; + this.preferredVersion = apiGroupVersion.preferredVersion.version; + + // update apiBase + this.apiBase = this.getUrl(); } async list({ namespace = "" } = {}, query?: IKubeApiQueryParams): Promise {