diff --git a/src/common/k8s-api/apis.ts b/src/common/k8s-api/apis.ts new file mode 100644 index 0000000000..441cb98687 --- /dev/null +++ b/src/common/k8s-api/apis.ts @@ -0,0 +1,60 @@ +/** + * 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 { 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, +}; diff --git a/src/common/k8s-api/index.ts b/src/common/k8s-api/index.ts index 441cb98687..7d20c21de4 100644 --- a/src/common/k8s-api/index.ts +++ b/src/common/k8s-api/index.ts @@ -3,58 +3,4 @@ * 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, apiKube } from "./apis"; diff --git a/src/common/k8s-api/kube-json-api.ts b/src/common/k8s-api/kube-json-api.ts index bc578641c3..6fe253a643 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 "./apis"; 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}`, }, }); }