mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix KubeJsonApi.forCluster throwing on renderer (#5034)
This commit is contained in:
parent
d9ad420fb0
commit
9203367c60
40
src/common/k8s-api/api-base.ts
Normal file
40
src/common/k8s-api/api-base.ts
Normal file
@ -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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
20
src/common/k8s-api/api-kube.ts
Normal file
20
src/common/k8s-api/api-kube.ts
Normal file
@ -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;
|
||||||
@ -3,58 +3,5 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { JsonApi } from "./json-api";
|
export { apiBase } from "./api-base";
|
||||||
import { KubeJsonApi } from "./kube-json-api";
|
export { apiKube } from "./api-kube";
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
import { JsonApi, JsonApiData, JsonApiError } from "./json-api";
|
import { JsonApi, JsonApiData, JsonApiError } from "./json-api";
|
||||||
import type { Response } from "node-fetch";
|
import type { Response } from "node-fetch";
|
||||||
import { LensProxy } from "../../main/lens-proxy";
|
|
||||||
import { apiKubePrefix, isDebugging } from "../vars";
|
import { apiKubePrefix, isDebugging } from "../vars";
|
||||||
|
import { apiBase } from "./api-base";
|
||||||
|
|
||||||
export interface KubeJsonApiListMetadata {
|
export interface KubeJsonApiListMetadata {
|
||||||
resourceVersion: string;
|
resourceVersion: string;
|
||||||
@ -57,15 +57,15 @@ export interface KubeJsonApiError extends JsonApiError {
|
|||||||
|
|
||||||
export class KubeJsonApi extends JsonApi<KubeJsonApiData> {
|
export class KubeJsonApi extends JsonApi<KubeJsonApiData> {
|
||||||
static forCluster(clusterId: string): KubeJsonApi {
|
static forCluster(clusterId: string): KubeJsonApi {
|
||||||
const port = LensProxy.getInstance().port;
|
const url = new URL(apiBase.config.serverAddress);
|
||||||
|
|
||||||
return new this({
|
return new this({
|
||||||
serverAddress: `http://127.0.0.1:${port}`,
|
serverAddress: apiBase.config.serverAddress,
|
||||||
apiBase: apiKubePrefix,
|
apiBase: apiKubePrefix,
|
||||||
debug: isDebugging,
|
debug: isDebugging,
|
||||||
}, {
|
}, {
|
||||||
headers: {
|
headers: {
|
||||||
"Host": `${clusterId}.localhost:${port}`,
|
"Host": `${clusterId}.localhost:${url.port}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user