1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Move activating cluster into injectable IPC (#7355)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-03-15 08:26:28 -04:00 committed by GitHub
parent 121a50ecf3
commit 497f63fde6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 122 additions and 38 deletions

View File

@ -9,11 +9,12 @@ import { broadcastMessage } from "../ipc";
import { app } from "electron";
import type { CatalogEntityConstructor, CatalogEntitySpec } from "../catalog/catalog-entity";
import { IpcRendererNavigationEvents } from "../ipc/navigation-events";
import { requestClusterActivation, requestClusterDisconnection } from "../../renderer/ipc";
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";
export interface KubernetesClusterPrometheusMetrics {
address?: {
@ -77,21 +78,12 @@ export class KubernetesCluster<
public readonly kind = KubernetesCluster.kind;
async connect(): Promise<void> {
if (app) {
const di = getLegacyGlobalDiForExtensionApi();
const getClusterById = di.inject(getClusterByIdInjectable);
const cluster = getClusterById(this.getId());
const requestClusterActivation = di.inject(requestClusterActivationInjectionToken);
if (!cluster) {
return;
}
const connectionCluster = di.inject(clusterConnectionInjectable, cluster);
await connectionCluster.activate();
} else {
await requestClusterActivation(this.getId(), false);
}
await requestClusterActivation({
clusterId: this.getId(),
});
}
async disconnect(): Promise<void> {

View File

@ -3,7 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export const clusterActivateHandler = "cluster:activate";
export const clusterSetFrameIdHandler = "cluster:set-frame-id";
export const clusterVisibilityHandler = "cluster:visibility";
export const clusterDisconnectHandler = "cluster:disconnect";

View File

@ -10,6 +10,10 @@ export interface RequestFromChannel {
<Response>(channel: RequestChannel<void, Response>): Promise<Awaited<Response>>;
}
export type ChannelRequester<Channel> = Channel extends RequestChannel<infer Request, infer Response>
? (req: Request) => Promise<Awaited<Response>>
: never;
export const requestFromChannelInjectionToken = getInjectionToken<RequestFromChannel>({
id: "request-from-request-channel",
});

View File

@ -0,0 +1,18 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId } from "../../../../common/cluster-types";
import { getRequestChannel } from "../../../../common/utils/channel/get-request-channel";
export interface ActivateCluster {
clusterId: ClusterId;
/**
* @default false
*/
force?: boolean;
}
export const activateClusterChannel = getRequestChannel<ActivateCluster, void>("activate-cluster");

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { ChannelRequester } from "../../../../common/utils/channel/request-from-channel-injection-token";
import type { activateClusterChannel } from "./channels";
export type RequestClusterActivation = ChannelRequester<typeof activateClusterChannel>;
export const requestClusterActivationInjectionToken = getInjectionToken<RequestClusterActivation>({
id: "request-cluster-activation-token",
});

View File

@ -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 { activateClusterChannel } from "../common/channels";
import requestClusterActivationInjectable from "./request-activation.injectable";
const activateClusterRequestChannelListenerInjectable = getRequestChannelListenerInjectable({
channel: activateClusterChannel,
handler: (di) => di.inject(requestClusterActivationInjectable),
});
export default activateClusterRequestChannelListenerInjectable;

View File

@ -0,0 +1,30 @@
/**
* 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 getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import clusterConnectionInjectable from "../../../../main/cluster/cluster-connection.injectable";
import { requestClusterActivationInjectionToken } from "../common/request-token";
const requestClusterActivationInjectable = getInjectable({
id: "request-cluster-activation",
instantiate: (di) => {
const getClusterById = di.inject(getClusterByIdInjectable);
return async ({ clusterId, force = false }) => {
const cluster = getClusterById(clusterId);
if (!cluster) {
return;
}
const connection = di.inject(clusterConnectionInjectable, cluster);
await connection.activate(force);
};
},
injectionToken: requestClusterActivationInjectionToken,
});
export default requestClusterActivationInjectable;

View 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 { getInjectable } from "@ogre-tools/injectable";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import { activateClusterChannel } from "../common/channels";
import { requestClusterActivationInjectionToken } from "../common/request-token";
const requestClusterActivationInjectable = getInjectable({
id: "request-cluster-activation",
instantiate: (di) => {
const requestFromChannel = di.inject(requestFromChannelInjectable);
return (req) => requestFromChannel(activateClusterChannel, req);
},
injectionToken: requestClusterActivationInjectionToken,
});
export default requestClusterActivationInjectable;

View File

@ -5,7 +5,7 @@
import type { IpcMainInvokeEvent } from "electron";
import { BrowserWindow, Menu } from "electron";
import { clusterFrameMap } from "../../../../common/cluster-frames";
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterDisconnectHandler, clusterStates } from "../../../../common/ipc/cluster";
import { clusterSetFrameIdHandler, clusterDisconnectHandler, 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";
@ -37,18 +37,6 @@ export const setupIpcMainHandlers = ({
pushCatalogToRenderer,
getClusterConnection,
}: Dependencies) => {
ipcMainHandle(clusterActivateHandler, async (event, clusterId: ClusterId, force = false) => {
const cluster = getClusterById(clusterId);
if (!cluster) {
return;
}
const clusterConnection = getClusterConnection(cluster);
await clusterConnection.activate(force);
});
ipcMainHandle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
const cluster = getClusterById(clusterId);

View File

@ -17,11 +17,12 @@ import { Icon } from "../icon";
import { Spinner } from "../spinner";
import type { KubeAuthUpdate } from "../../../common/cluster-types";
import type { CatalogEntityRegistry } from "../../api/catalog/entity/registry";
import { requestClusterActivation } from "../../ipc";
import type { NavigateToEntitySettings } from "../../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable";
import { withInjectables } from "@ogre-tools/injectable-react";
import navigateToEntitySettingsInjectable from "../../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable";
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
import type { RequestClusterActivation } from "../../../features/cluster/activation/common/request-token";
import requestClusterActivationInjectable from "../../../features/cluster/activation/renderer/request-activation.injectable";
export interface ClusterStatusProps {
className?: IClassName;
@ -31,6 +32,7 @@ export interface ClusterStatusProps {
interface Dependencies {
navigateToEntitySettings: NavigateToEntitySettings;
entityRegistry: CatalogEntityRegistry;
requestClusterActivation: RequestClusterActivation;
}
@observer
@ -83,7 +85,10 @@ class NonInjectedClusterStatus extends React.Component<ClusterStatusProps & Depe
this.isReconnecting = true;
try {
await requestClusterActivation(this.cluster.id, true);
await this.props.requestClusterActivation({
clusterId: this.cluster.id,
force: true,
});
} catch (error) {
this.authOutput.push({
message: String(error),
@ -173,5 +178,6 @@ export const ClusterStatus = withInjectables<Dependencies, ClusterStatusProps>(N
...props,
navigateToEntitySettings: di.inject(navigateToEntitySettingsInjectable),
entityRegistry: di.inject(catalogEntityRegistryInjectable),
requestClusterActivation: di.inject(requestClusterActivationInjectable),
}),
});

View File

@ -11,7 +11,6 @@ import { disposeOnUnmount, observer } from "mobx-react";
import { ClusterStatus } from "./cluster-status";
import type { ClusterFrameHandler } from "./cluster-frame-handler";
import type { Cluster } from "../../../common/cluster/cluster";
import { requestClusterActivation } from "../../ipc";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { NavigateToCatalog } from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
@ -21,6 +20,8 @@ import type { CatalogEntityRegistry } from "../../api/catalog/entity/registry";
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
import type { GetClusterById } from "../../../common/cluster-store/get-by-id.injectable";
import getClusterByIdInjectable from "../../../common/cluster-store/get-by-id.injectable";
import type { RequestClusterActivation } from "../../../features/cluster/activation/common/request-token";
import requestClusterActivationInjectable from "../../../features/cluster/activation/renderer/request-activation.injectable";
interface Dependencies {
clusterId: IComputedValue<string>;
@ -28,6 +29,7 @@ interface Dependencies {
navigateToCatalog: NavigateToCatalog;
entityRegistry: CatalogEntityRegistry;
getClusterById: GetClusterById;
requestClusterActivation: RequestClusterActivation;
}
@observer
@ -83,7 +85,7 @@ class NonInjectedClusterView extends React.Component<Dependencies> {
this.props.clusterFrames.setVisibleCluster(clusterId);
this.props.clusterFrames.initView(clusterId);
requestClusterActivation(clusterId, false); // activate and fetch cluster's state from main
this.props.requestClusterActivation({ clusterId });
this.props.entityRegistry.activeEntity = clusterId;
}, {
fireImmediately: true,
@ -117,6 +119,7 @@ export const ClusterView = withInjectables<Dependencies>(NonInjectedClusterView,
clusterFrames: di.inject(clusterFrameHandlerInjectable),
entityRegistry: di.inject(catalogEntityRegistryInjectable),
getClusterById: di.inject(getClusterByIdInjectable),
requestClusterActivation: di.inject(requestClusterActivationInjectable),
}),
});

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { clusterActivateHandler, clusterDisconnectHandler, clusterSetFrameIdHandler, clusterStates } from "../../common/ipc/cluster";
import { clusterDisconnectHandler, 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 requestClusterActivation(clusterId: ClusterId, force?: boolean): Promise<void> {
return requestMain(clusterActivateHandler, clusterId, force);
}
export function requestClusterDisconnection(clusterId: ClusterId, force?: boolean): Promise<void> {
return requestMain(clusterDisconnectHandler, clusterId, force);
}