diff --git a/src/common/k8s-api/index.ts b/src/common/k8s-api/index.ts index 791ee09285..e464c307d7 100644 --- a/src/common/k8s-api/index.ts +++ b/src/common/k8s-api/index.ts @@ -23,19 +23,28 @@ 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 "../event-bus"; let apiBase: JsonApi; let apiKube: KubeJsonApi; if (typeof window === "undefined") { - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${process.env.LENS_PROXY_PORT}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": `localhost:${process.env.LENS_PROXY_PORT}` - } + 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({ diff --git a/src/common/k8s-api/kube-api.ts b/src/common/k8s-api/kube-api.ts index 1131119a3b..79c750dae7 100644 --- a/src/common/k8s-api/kube-api.ts +++ b/src/common/k8s-api/kube-api.ts @@ -96,13 +96,14 @@ export interface IKubeApiCluster { } export function forCluster(cluster: IKubeApiCluster, kubeClass: KubeObjectConstructor): KubeApi { + const url = new URL(apiBase.config.serverAddress); const request = new KubeJsonApi({ serverAddress: apiBase.config.serverAddress, apiBase: apiKubePrefix, debug: isDevelopment, }, { headers: { - "Host": apiBase.config.serverAddress + "Host": `${cluster.metadata.uid}.localhost:${url.port}` } }); diff --git a/src/main/lens-proxy.ts b/src/main/lens-proxy.ts index 353ed935e7..3cd5182677 100644 --- a/src/main/lens-proxy.ts +++ b/src/main/lens-proxy.ts @@ -30,6 +30,7 @@ import logger from "./logger"; import { Singleton } from "../common/utils"; import type { Cluster } from "./cluster"; import type { ProxyApiRequestArgs } from "./proxy-functions"; +import { appEventBus } from "../common/event-bus"; type GetClusterForRequest = (req: http.IncomingMessage) => Cluster | null; @@ -97,7 +98,7 @@ export class LensProxy extends Singleton { }); this.port = port; - process.env.LENS_PROXY_PORT = port.toString(); + appEventBus.emit({ name: "lens-proxy", action: "listen", params: { port }}); resolve(); }) .once("error", (error) => {