mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Move deactivating a cluster into injectable IPC (#7356)
* Move activating cluster into injectable IPC Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move deactivating a cluster into injectable IPC Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type error Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
497f63fde6
commit
ae9a8c3c41
@ -6,15 +6,11 @@
|
||||
import type { CatalogEntityActionContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus, CatalogCategorySpec } from "../catalog";
|
||||
import { CatalogEntity, CatalogCategory, categoryVersion } from "../catalog/catalog-entity";
|
||||
import { broadcastMessage } from "../ipc";
|
||||
import { app } from "electron";
|
||||
import type { CatalogEntityConstructor, CatalogEntitySpec } from "../catalog/catalog-entity";
|
||||
import { IpcRendererNavigationEvents } from "../ipc/navigation-events";
|
||||
import { requestClusterDisconnection } from "../../renderer/ipc";
|
||||
import KubeClusterCategoryIcon from "./icons/kubernetes.svg";
|
||||
import getClusterByIdInjectable from "../cluster-store/get-by-id.injectable";
|
||||
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||
import clusterConnectionInjectable from "../../main/cluster/cluster-connection.injectable";
|
||||
import { requestClusterActivationInjectionToken } from "../../features/cluster/activation/common/request-token";
|
||||
import { requestClusterActivationInjectionToken, requestClusterDeactivationInjectionToken } from "../../features/cluster/activation/common/request-token";
|
||||
|
||||
export interface KubernetesClusterPrometheusMetrics {
|
||||
address?: {
|
||||
@ -87,21 +83,10 @@ export class KubernetesCluster<
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (app) {
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
const cluster = getClusterById(this.getId());
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
const requestClusterDeactivation = di.inject(requestClusterDeactivationInjectionToken);
|
||||
|
||||
if (!cluster) {
|
||||
return;
|
||||
}
|
||||
|
||||
const connectionCluster = di.inject(clusterConnectionInjectable, cluster);
|
||||
|
||||
connectionCluster.disconnect();
|
||||
} else {
|
||||
await requestClusterDisconnection(this.getId(), false);
|
||||
}
|
||||
await requestClusterDeactivation(this.getId());
|
||||
}
|
||||
|
||||
async onRun(context: CatalogEntityActionContext) {
|
||||
@ -135,7 +120,7 @@ export class KubernetesCluster<
|
||||
title: "Disconnect",
|
||||
icon: "link_off",
|
||||
onClick: () => {
|
||||
requestClusterDisconnection(this.getId());
|
||||
this.disconnect();
|
||||
broadcastMessage(
|
||||
IpcRendererNavigationEvents.NAVIGATE_IN_APP,
|
||||
"/catalog",
|
||||
|
||||
@ -3,12 +3,16 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { clusterFrameMap } from "./cluster-frames";
|
||||
import { observable } from "mobx";
|
||||
|
||||
export interface ClusterFrameInfo {
|
||||
frameId: number;
|
||||
processId: number;
|
||||
}
|
||||
|
||||
const clusterFramesInjectable = getInjectable({
|
||||
id: "cluster-frames",
|
||||
instantiate: () => clusterFrameMap,
|
||||
causesSideEffects: true,
|
||||
instantiate: () => observable.map<string, ClusterFrameInfo>(),
|
||||
});
|
||||
|
||||
export default clusterFramesInjectable;
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { observable } from "mobx";
|
||||
|
||||
export interface ClusterFrameInfo {
|
||||
frameId: number;
|
||||
processId: number;
|
||||
}
|
||||
|
||||
export const clusterFrameMap = observable.map<string, ClusterFrameInfo>();
|
||||
@ -5,7 +5,6 @@
|
||||
|
||||
export const clusterSetFrameIdHandler = "cluster:set-frame-id";
|
||||
export const clusterVisibilityHandler = "cluster:visibility";
|
||||
export const clusterDisconnectHandler = "cluster:disconnect";
|
||||
export const clusterStates = "cluster:states";
|
||||
|
||||
/**
|
||||
|
||||
@ -9,13 +9,12 @@
|
||||
|
||||
import { ipcMain, ipcRenderer, webContents } from "electron";
|
||||
import { toJS } from "../utils/toJS";
|
||||
import type { ClusterFrameInfo } from "../cluster-frames";
|
||||
import { clusterFrameMap } from "../cluster-frames";
|
||||
import type { Disposer } from "@k8slens/utilities";
|
||||
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||
import ipcRendererInjectable from "../../renderer/utils/channel/ipc-renderer.injectable";
|
||||
import loggerInjectable from "../logger.injectable";
|
||||
import ipcMainInjectionToken from "./ipc-main-injection-token";
|
||||
import clusterFramesInjectable from "../cluster-frames.injectable";
|
||||
|
||||
export const broadcastMainChannel = "ipc:broadcast-main";
|
||||
|
||||
@ -29,10 +28,6 @@ export function ipcMainHandle(channel: string, listener: (event: Electron.IpcMai
|
||||
});
|
||||
}
|
||||
|
||||
function getSubFrames(): ClusterFrameInfo[] {
|
||||
return Array.from(clusterFrameMap.values());
|
||||
}
|
||||
|
||||
export async function broadcastMessage(channel: string, ...args: any[]): Promise<void> {
|
||||
if (ipcRenderer) {
|
||||
return ipcRenderer.invoke(broadcastMainChannel, channel, ...args.map(sanitizePayload));
|
||||
@ -44,12 +39,12 @@ export async function broadcastMessage(channel: string, ...args: any[]): Promise
|
||||
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
const logger = di.inject(loggerInjectable);
|
||||
const clusterFrames = di.inject(clusterFramesInjectable);
|
||||
|
||||
ipcMain.listeners(channel).forEach((func) => func({
|
||||
processId: undefined, frameId: undefined, sender: undefined, senderFrame: undefined,
|
||||
}, ...args));
|
||||
|
||||
const subFrames = getSubFrames();
|
||||
const views = webContents.getAllWebContents();
|
||||
|
||||
if (!views || !Array.isArray(views) || views.length === 0) return;
|
||||
@ -75,7 +70,7 @@ export async function broadcastMessage(channel: string, ...args: any[]): Promise
|
||||
}
|
||||
|
||||
// Send message to subFrames of views.
|
||||
for (const frameInfo of subFrames) {
|
||||
for (const frameInfo of clusterFrames.values()) {
|
||||
logger.silly(`[IPC]: broadcasting "${channel}" to subframe "frameInfo.processId"=${frameInfo.processId} "frameInfo.frameId"=${frameInfo.frameId}`, { args });
|
||||
|
||||
try {
|
||||
|
||||
@ -16,3 +16,5 @@ export interface ActivateCluster {
|
||||
}
|
||||
|
||||
export const activateClusterChannel = getRequestChannel<ActivateCluster, void>("activate-cluster");
|
||||
|
||||
export const deactivateClusterChannel = getRequestChannel<ClusterId, void>("deactivate-cluster");
|
||||
|
||||
@ -5,10 +5,16 @@
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { ChannelRequester } from "../../../../common/utils/channel/request-from-channel-injection-token";
|
||||
import type { activateClusterChannel } from "./channels";
|
||||
import type { activateClusterChannel, deactivateClusterChannel } from "./channels";
|
||||
|
||||
export type RequestClusterActivation = ChannelRequester<typeof activateClusterChannel>;
|
||||
|
||||
export const requestClusterActivationInjectionToken = getInjectionToken<RequestClusterActivation>({
|
||||
id: "request-cluster-activation-token",
|
||||
});
|
||||
|
||||
export type RequestClusterDeactivation = ChannelRequester<typeof deactivateClusterChannel>;
|
||||
|
||||
export const requestClusterDeactivationInjectionToken = getInjectionToken<RequestClusterDeactivation>({
|
||||
id: "request-cluster-deactivation-token",
|
||||
});
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
|
||||
import { deactivateClusterChannel } from "../common/channels";
|
||||
import requestClusterDeactivationInjectable from "./request-deactivation.injectable";
|
||||
|
||||
const clusterDeactivationRequestChannelListenerInjectable = getRequestChannelListenerInjectable({
|
||||
channel: deactivateClusterChannel,
|
||||
handler: (di) => di.inject(requestClusterDeactivationInjectable),
|
||||
});
|
||||
|
||||
export default clusterDeactivationRequestChannelListenerInjectable;
|
||||
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
|
||||
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
|
||||
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
|
||||
import clusterConnectionInjectable from "../../../../main/cluster/cluster-connection.injectable";
|
||||
import { requestClusterDeactivationInjectionToken } from "../common/request-token";
|
||||
|
||||
const requestClusterDeactivationInjectable = getInjectable({
|
||||
id: "request-cluster-deactivation",
|
||||
instantiate: (di) => {
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
const clusterFrames = di.inject(clusterFramesInjectable);
|
||||
const emitAppEvent = di.inject(emitAppEventInjectable);
|
||||
|
||||
return async (clusterId) => {
|
||||
emitAppEvent({ name: "cluster", action: "stop" });
|
||||
|
||||
const cluster = getClusterById(clusterId);
|
||||
|
||||
if (!cluster) {
|
||||
return;
|
||||
}
|
||||
|
||||
const connection = di.inject(clusterConnectionInjectable, cluster);
|
||||
|
||||
connection.disconnect();
|
||||
clusterFrames.delete(clusterId);
|
||||
};
|
||||
},
|
||||
injectionToken: requestClusterDeactivationInjectionToken,
|
||||
});
|
||||
|
||||
export default requestClusterDeactivationInjectable;
|
||||
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
|
||||
import { deactivateClusterChannel } from "../common/channels";
|
||||
import { requestClusterDeactivationInjectionToken } from "../common/request-token";
|
||||
|
||||
const requestClusterDeactivationInjectable = getInjectable({
|
||||
id: "request-cluster-deactivation",
|
||||
instantiate: (di) => {
|
||||
const requestFromChannel = di.inject(requestFromChannelInjectable);
|
||||
|
||||
return (clusterId) => requestFromChannel(deactivateClusterChannel, clusterId);
|
||||
},
|
||||
injectionToken: requestClusterDeactivationInjectionToken,
|
||||
});
|
||||
|
||||
export default requestClusterDeactivationInjectable;
|
||||
@ -8,10 +8,9 @@ import loggerInjectable from "../../../../common/logger.injectable";
|
||||
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
|
||||
import { onLoadOfApplicationInjectionToken } from "@k8slens/application";
|
||||
import applicationMenuItemCompositeInjectable from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
|
||||
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
|
||||
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
|
||||
import pushCatalogToRendererInjectable from "../../../catalog-sync-to-renderer/push-catalog-to-renderer.injectable";
|
||||
import clusterConnectionInjectable from "../../../cluster/cluster-connection.injectable";
|
||||
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
|
||||
|
||||
const setupIpcMainHandlersInjectable = getInjectable({
|
||||
id: "setup-ipc-main-handlers",
|
||||
@ -19,21 +18,15 @@ const setupIpcMainHandlersInjectable = getInjectable({
|
||||
instantiate: (di) => ({
|
||||
run: () => {
|
||||
const logger = di.inject(loggerInjectable);
|
||||
const applicationMenuItemComposite = di.inject(applicationMenuItemCompositeInjectable);
|
||||
const pushCatalogToRenderer = di.inject(pushCatalogToRendererInjectable);
|
||||
const clusterStore = di.inject(clusterStoreInjectable);
|
||||
const emitAppEvent = di.inject(emitAppEventInjectable);
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
|
||||
logger.debug("[APP-MAIN] initializing ipc main handlers");
|
||||
|
||||
setupIpcMainHandlers({
|
||||
applicationMenuItemComposite,
|
||||
pushCatalogToRenderer,
|
||||
clusterStore,
|
||||
emitAppEvent,
|
||||
getClusterById,
|
||||
getClusterConnection: (cluster) => di.inject(clusterConnectionInjectable, cluster),
|
||||
applicationMenuItemComposite: di.inject(applicationMenuItemCompositeInjectable),
|
||||
pushCatalogToRenderer: di.inject(pushCatalogToRendererInjectable),
|
||||
clusterStore: di.inject(clusterStoreInjectable),
|
||||
getClusterById: di.inject(getClusterByIdInjectable),
|
||||
clusterFrames: di.inject(clusterFramesInjectable),
|
||||
});
|
||||
},
|
||||
}),
|
||||
|
||||
@ -4,62 +4,43 @@
|
||||
*/
|
||||
import type { IpcMainInvokeEvent } from "electron";
|
||||
import { BrowserWindow, Menu } from "electron";
|
||||
import { clusterFrameMap } from "../../../../common/cluster-frames";
|
||||
import { clusterSetFrameIdHandler, clusterDisconnectHandler, clusterStates } from "../../../../common/ipc/cluster";
|
||||
import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable";
|
||||
import { clusterSetFrameIdHandler, clusterStates } from "../../../../common/ipc/cluster";
|
||||
import type { ClusterId } from "../../../../common/cluster-types";
|
||||
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
|
||||
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
|
||||
import type { IComputedValue } from "mobx";
|
||||
import type { IComputedValue, ObservableMap } from "mobx";
|
||||
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
|
||||
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
|
||||
import type { ApplicationMenuItemTypes } from "../../../../features/application-menu/main/menu-items/application-menu-item-injection-token";
|
||||
import type { Composite } from "../../../../common/utils/composite/get-composite/get-composite";
|
||||
import { getApplicationMenuTemplate } from "../../../../features/application-menu/main/populate-application-menu.injectable";
|
||||
import type { MenuItemRoot } from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
|
||||
import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.injectable";
|
||||
import type { GetClusterById } from "../../../../common/cluster-store/get-by-id.injectable";
|
||||
import type { Cluster } from "../../../../common/cluster/cluster";
|
||||
import type { ClusterConnection } from "../../../cluster/cluster-connection.injectable";
|
||||
interface Dependencies {
|
||||
applicationMenuItemComposite: IComputedValue<Composite<ApplicationMenuItemTypes | MenuItemRoot>>;
|
||||
clusterStore: ClusterStore;
|
||||
emitAppEvent: EmitAppEvent;
|
||||
getClusterById: GetClusterById;
|
||||
pushCatalogToRenderer: () => void;
|
||||
getClusterConnection: (cluster: Cluster) => ClusterConnection;
|
||||
clusterFrames: ObservableMap<string, ClusterFrameInfo>;
|
||||
}
|
||||
|
||||
export const setupIpcMainHandlers = ({
|
||||
applicationMenuItemComposite,
|
||||
clusterStore,
|
||||
emitAppEvent,
|
||||
getClusterById,
|
||||
pushCatalogToRenderer,
|
||||
getClusterConnection,
|
||||
clusterFrames,
|
||||
}: Dependencies) => {
|
||||
ipcMainHandle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
|
||||
const cluster = getClusterById(clusterId);
|
||||
|
||||
if (cluster) {
|
||||
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
|
||||
clusterFrames.set(cluster.id, { frameId: event.frameId, processId: event.processId });
|
||||
pushCatalogToRenderer();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMainHandle(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
|
||||
emitAppEvent({ name: "cluster", action: "stop" });
|
||||
const cluster = getClusterById(clusterId);
|
||||
|
||||
if (!cluster) {
|
||||
return;
|
||||
}
|
||||
|
||||
const clusterConnection = getClusterConnection(cluster);
|
||||
|
||||
clusterConnection.disconnect();
|
||||
clusterFrameMap.delete(cluster.id);
|
||||
});
|
||||
|
||||
ipcMainHandle(windowActionHandleChannel, (event, action) => handleWindowAction(action));
|
||||
|
||||
ipcMainOn(windowLocationChangedChannel, () => onLocationChange());
|
||||
|
||||
@ -16,9 +16,7 @@ import setupMainWindowVisibilityAfterActivationInjectable from "./electron-app/r
|
||||
import setupDeviceShutdownInjectable from "./electron-app/runnables/setup-device-shutdown.injectable";
|
||||
import setupApplicationNameInjectable from "./electron-app/runnables/setup-application-name.injectable";
|
||||
import setupRunnablesBeforeClosingOfApplicationInjectable from "./electron-app/runnables/setup-runnables-before-closing-of-application.injectable";
|
||||
import clusterFramesInjectable from "../common/cluster-frames.injectable";
|
||||
import type { ClusterFrameInfo } from "../common/cluster-frames";
|
||||
import { observable, runInAction } from "mobx";
|
||||
import { runInAction } from "mobx";
|
||||
import broadcastMessageInjectable from "../common/ipc/broadcast-message.injectable";
|
||||
import electronQuitAndInstallUpdateInjectable from "./electron-app/features/electron-quit-and-install-update.injectable";
|
||||
import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable";
|
||||
@ -65,8 +63,6 @@ export function getDiForUnitTesting() {
|
||||
overrideElectronFeatures(di);
|
||||
getOverrideFsWithFakes()(di);
|
||||
|
||||
di.override(clusterFramesInjectable, () => observable.map<string, ClusterFrameInfo>());
|
||||
|
||||
di.override(broadcastMessageInjectable, () => (channel) => {
|
||||
throw new Error(`Tried to broadcast message to channel "${channel}" over IPC without explicit override.`);
|
||||
});
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window.injectable";
|
||||
import createElectronWindowForInjectable from "./create-electron-window.injectable";
|
||||
import type { ClusterFrameInfo } from "../../../../common/cluster-frames";
|
||||
import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable";
|
||||
|
||||
export interface ElectronWindow {
|
||||
show: () => void;
|
||||
|
||||
@ -8,7 +8,7 @@ import clusterFramesInjectable from "../../../common/cluster-frames.injectable";
|
||||
import type { MessageChannel } from "../../../common/utils/channel/message-channel-listener-injection-token";
|
||||
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
|
||||
import type { DiContainer } from "@ogre-tools/injectable";
|
||||
import type { ClusterFrameInfo } from "../../../common/cluster-frames";
|
||||
import type { ClusterFrameInfo } from "../../../common/cluster-frames.injectable";
|
||||
|
||||
describe("message-to-channel", () => {
|
||||
let di: DiContainer;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { clusterDisconnectHandler, clusterSetFrameIdHandler, clusterStates } from "../../common/ipc/cluster";
|
||||
import { clusterSetFrameIdHandler, clusterStates } from "../../common/ipc/cluster";
|
||||
import type { ClusterId, ClusterState } from "../../common/cluster-types";
|
||||
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel, type WindowAction } from "../../common/ipc/window";
|
||||
import { extensionDiscoveryStateChannel, extensionLoaderFromMainChannel } from "../../common/ipc/extension-handling";
|
||||
@ -46,10 +46,6 @@ export function requestSetClusterFrameId(clusterId: ClusterId): Promise<void> {
|
||||
return requestMain(clusterSetFrameIdHandler, clusterId);
|
||||
}
|
||||
|
||||
export function requestClusterDisconnection(clusterId: ClusterId, force?: boolean): Promise<void> {
|
||||
return requestMain(clusterDisconnectHandler, clusterId, force);
|
||||
}
|
||||
|
||||
export function requestInitialClusterStates(): Promise<{ id: string; state: ClusterState }[]> {
|
||||
return requestMain(clusterStates);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user