diff --git a/src/renderer/api/kube-api-parse.ts b/src/renderer/api/kube-api-parse.ts index 0745dc71eb..df75eea2c4 100644 --- a/src/renderer/api/kube-api-parse.ts +++ b/src/renderer/api/kube-api-parse.ts @@ -1,5 +1,15 @@ // Parse kube-api path and get api-version, group, etc. + +import type { KubeObject } from "./kube-object"; import { splitArray } from "../../common/utils"; +import { apiManager } from "./api-manager"; + +export interface IKubeObjectRef { + kind: string; + apiVersion: string; + name: string; + namespace?: string; +} export interface IKubeApiLinkRef { apiPrefix?: string; @@ -9,13 +19,13 @@ export interface IKubeApiLinkRef { namespace?: string; } -export interface IKubeApiLinkBase extends IKubeApiLinkRef { +export interface IKubeApiParsed extends IKubeApiLinkRef { apiBase: string; apiGroup: string; apiVersionWithGroup: string; } -export function parseApi(path: string): IKubeApiLinkBase { +export function parseKubeApi(path: string): IKubeApiParsed { path = new URL(path, location.origin).pathname; const [, prefix, ...parts] = path.split("/"); const apiPrefix = `/${prefix}`; @@ -91,7 +101,7 @@ export function parseApi(path: string): IKubeApiLinkBase { }; } -export function createApiLink(ref: IKubeApiLinkRef): string { +export function createKubeApiURL(ref: IKubeApiLinkRef): string { const { apiPrefix = "/apis", resource, apiVersion, name } = ref; let { namespace } = ref; if (namespace) { @@ -101,3 +111,36 @@ export function createApiLink(ref: IKubeApiLinkRef): string { .filter(v => v) .join("/") } + +export function lookupApiLink(ref: IKubeObjectRef, parentObject: KubeObject): string { + const { + kind, apiVersion, name, + namespace = parentObject.getNs() + } = ref; + + // search in registered apis by 'kind' & 'apiVersion' + const api = apiManager.getApi(api => api.kind === kind && api.apiVersionWithGroup == apiVersion) + if (api) { + return api.getUrl({ namespace, name }) + } + + // lookup api by generated resource link + const apiPrefixes = ["/apis", "/api"]; + const resource = kind.toLowerCase() + kind.endsWith("s") ? "es" : "s"; + for (const apiPrefix of apiPrefixes) { + const apiLink = createKubeApiURL({ apiPrefix, apiVersion, name, namespace, resource }); + if (apiManager.getApi(apiLink)) { + return apiLink; + } + } + + // resolve by kind only (hpa's might use refs to older versions of resources for example) + const apiByKind = apiManager.getApi(api => api.kind === kind); + if (apiByKind) { + return apiByKind.getUrl({ name, namespace }) + } + + // otherwise generate link with default prefix + // resource still might exists in k8s, but api is not registered in the app + return createKubeApiURL({ apiVersion, name, namespace, resource }) +} diff --git a/src/renderer/api/kube-api-parse_test.ts b/src/renderer/api/kube-api-parse_test.ts index 03f53ae34d..b33c833bfa 100644 --- a/src/renderer/api/kube-api-parse_test.ts +++ b/src/renderer/api/kube-api-parse_test.ts @@ -1,8 +1,8 @@ -import { IKubeApiLinkBase, parseApi } from "./kube-api-parse"; +import { IKubeApiParsed, parseKubeApi } from "./kube-api-parse"; interface KubeApi_Parse_Test { url: string; - expected: Required; + expected: Required; } const tests: KubeApi_Parse_Test[] = [ @@ -116,7 +116,7 @@ describe.only("parseApi unit tests", () => { for (const i in tests) { const { url: tUrl, expected:tExpect} = tests[i]; test(`test #${parseInt(i)+1}`, () => { - expect(parseApi(tUrl)).toStrictEqual(tExpect); + expect(parseKubeApi(tUrl)).toStrictEqual(tExpect); }); } }); diff --git a/src/renderer/api/kube-api.ts b/src/renderer/api/kube-api.ts index 7075477771..480abb4a4f 100644 --- a/src/renderer/api/kube-api.ts +++ b/src/renderer/api/kube-api.ts @@ -7,14 +7,7 @@ import { KubeJsonApi, KubeJsonApiData, KubeJsonApiDataList } from "./kube-json-a import { apiKube } from "./index"; import { kubeWatchApi } from "./kube-watch-api"; import { apiManager } from "./api-manager"; -import { createApiLink, parseApi } from "./kube-api-parse"; - -export interface IKubeObjectRef { - kind: string; - apiVersion: string; - name: string; - namespace?: string; -} +import { createKubeApiURL, parseKubeApi } from "./kube-api-parse"; export interface IKubeApiOptions { kind: string; // resource type within api-group, e.g. "Namespace" @@ -33,8 +26,7 @@ export interface IKubeApiQueryParams { } export class KubeApi { - static parseApi = parseApi; - static createLink = createApiLink; + static parseApi = parseKubeApi; static watchAll(...apis: KubeApi[]) { const disposers = apis.map(api => api.watch()); @@ -92,7 +84,7 @@ export class KubeApi { getUrl({ name = "", namespace = "" } = {}, query?: Partial) { const { apiPrefix, apiVersionWithGroup, apiResource } = this; - const resourcePath = KubeApi.createLink({ + const resourcePath = createKubeApiURL({ apiPrefix: apiPrefix, apiVersion: apiVersionWithGroup, resource: apiResource, @@ -182,35 +174,4 @@ export class KubeApi { } } -export function lookupApiLink(ref: IKubeObjectRef, parentObject: KubeObject): string { - const { - kind, apiVersion, name, - namespace = parentObject.getNs() - } = ref; - - // search in registered apis by 'kind' & 'apiVersion' - const api = apiManager.getApi(api => api.kind === kind && api.apiVersionWithGroup == apiVersion) - if (api) { - return api.getUrl({ namespace, name }) - } - - // lookup api by generated resource link - const apiPrefixes = ["/apis", "/api"]; - const resource = kind.toLowerCase() + kind.endsWith("s") ? "es" : "s"; - for (const apiPrefix of apiPrefixes) { - const apiLink = KubeApi.createLink({ apiPrefix, apiVersion, name, namespace, resource }); - if (apiManager.getApi(apiLink)) { - return apiLink; - } - } - - // resolve by kind only (hpa's might use refs to older versions of resources for example) - const apiByKind = apiManager.getApi(api => api.kind === kind); - if (apiByKind) { - return apiByKind.getUrl({ name, namespace }) - } - - // otherwise generate link with default prefix - // resource still might exists in k8s, but api is not registered in the app - return KubeApi.createLink({ apiVersion, name, namespace, resource }) -} +export * from "./kube-api-parse" \ No newline at end of file