diff --git a/src/common/k8s-api/api-base.ts b/src/common/k8s-api/api-base.ts new file mode 100644 index 0000000000..e511dd8454 --- /dev/null +++ b/src/common/k8s-api/api-base.ts @@ -0,0 +1,40 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { JsonApi } from "./json-api"; +import { apiPrefix, isDebugging, isDevelopment } from "../vars"; +import { appEventBus } from "../app-event-bus/event-bus"; + +export let apiBase: JsonApi; + +if (typeof window === "undefined") { + appEventBus.addListener((event) => { + if (event.name !== "lens-proxy" && event.action !== "listen") return; + + const params = event.params as { port?: number }; + + if (!params.port) return; + + apiBase = new JsonApi({ + serverAddress: `http://127.0.0.1:${params.port}`, + apiBase: apiPrefix, + debug: isDevelopment || isDebugging, + }, { + headers: { + "Host": `localhost:${params.port}`, + }, + }); + }); +} else { + apiBase = new JsonApi({ + serverAddress: `http://127.0.0.1:${window.location.port}`, + apiBase: apiPrefix, + debug: isDevelopment || isDebugging, + }, { + headers: { + "Host": window.location.host, + }, + }); +} diff --git a/src/common/k8s-api/api-kube.ts b/src/common/k8s-api/api-kube.ts new file mode 100644 index 0000000000..5ff8478694 --- /dev/null +++ b/src/common/k8s-api/api-kube.ts @@ -0,0 +1,20 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { isClusterPageContext } from "../utils"; +import { KubeJsonApi } from "./kube-json-api"; +import { apiKubePrefix, isDevelopment } from "../vars"; + +export const apiKube = isClusterPageContext() + ? new KubeJsonApi({ + serverAddress: `http://127.0.0.1:${window.location.port}`, + apiBase: apiKubePrefix, + debug: isDevelopment, + }, { + headers: { + "Host": window.location.host, + }, + }) + : undefined; diff --git a/src/common/k8s-api/index.ts b/src/common/k8s-api/index.ts index 441cb98687..0d47a643f3 100644 --- a/src/common/k8s-api/index.ts +++ b/src/common/k8s-api/index.ts @@ -3,58 +3,5 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { JsonApi } from "./json-api"; -import { KubeJsonApi } from "./kube-json-api"; -import { apiKubePrefix, apiPrefix, isDebugging, isDevelopment } from "../../common/vars"; -import { isClusterPageContext } from "../utils/cluster-id-url-parsing"; -import { appEventBus } from "../app-event-bus/event-bus"; - -let apiBase: JsonApi; -let apiKube: KubeJsonApi; - -if (typeof window === "undefined") { - appEventBus.addListener((event) => { - if (event.name !== "lens-proxy" && event.action !== "listen") return; - - const params = event.params as { port?: number }; - - if (!params.port) return; - - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${params.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": `localhost:${params.port}`, - }, - }); - }); -} else { - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${window.location.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": window.location.host, - }, - }); -} - -if (isClusterPageContext()) { - apiKube = new KubeJsonApi({ - serverAddress: `http://127.0.0.1:${window.location.port}`, - apiBase: apiKubePrefix, - debug: isDevelopment, - }, { - headers: { - "Host": window.location.host, - }, - }); -} - -export { - apiBase, - apiKube, -}; +export { apiBase } from "./api-base"; +export { apiKube } from "./api-kube"; diff --git a/src/common/k8s-api/kube-json-api.ts b/src/common/k8s-api/kube-json-api.ts index bc578641c3..30131545fe 100644 --- a/src/common/k8s-api/kube-json-api.ts +++ b/src/common/k8s-api/kube-json-api.ts @@ -5,8 +5,8 @@ import { JsonApi, JsonApiData, JsonApiError } from "./json-api"; import type { Response } from "node-fetch"; -import { LensProxy } from "../../main/lens-proxy"; import { apiKubePrefix, isDebugging } from "../vars"; +import { apiBase } from "./api-base"; export interface KubeJsonApiListMetadata { resourceVersion: string; @@ -57,15 +57,15 @@ export interface KubeJsonApiError extends JsonApiError { export class KubeJsonApi extends JsonApi { static forCluster(clusterId: string): KubeJsonApi { - const port = LensProxy.getInstance().port; + const url = new URL(apiBase.config.serverAddress); return new this({ - serverAddress: `http://127.0.0.1:${port}`, + serverAddress: apiBase.config.serverAddress, apiBase: apiKubePrefix, debug: isDebugging, }, { headers: { - "Host": `${clusterId}.localhost:${port}`, + "Host": `${clusterId}.localhost:${url.port}`, }, }); }