mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
1 parent
a0831b3283
commit
33d3181113
11 files changed
+163
-165
No files matched your search
+9
-111
@@ -1,30 +1,12 @@
|
||||
import { app } from "electron"
|
||||
import type http from "http"
|
||||
import { autorun } from "mobx";
|
||||
import path from "path"
|
||||
import http from "http"
|
||||
import { copyFile, ensureDir } from "fs-extra"
|
||||
import filenamify from "filenamify"
|
||||
import { apiKubePrefix, appProto } from "../common/vars";
|
||||
import { ClusterId, ClusterModel, clusterStore } from "../common/cluster-store"
|
||||
import { handleMessages } from "../common/ipc";
|
||||
import { ClusterIpcMessage } from "../common/ipc-messages";
|
||||
import { apiKubePrefix } from "../common/vars";
|
||||
import { ClusterId, clusterStore } from "../common/cluster-store"
|
||||
import { handleMessage } from "../common/ipc";
|
||||
import { tracker } from "../common/tracker";
|
||||
import { validateConfig } from "../common/kube-helpers";
|
||||
import { Cluster } from "./cluster"
|
||||
import { FeatureInstallRequest } from "./feature";
|
||||
import logger from "./logger"
|
||||
|
||||
export interface ClusterIconUpload {
|
||||
clusterId: string;
|
||||
name: string;
|
||||
path: string;
|
||||
}
|
||||
import { Cluster, ClusterIpcEvent } from "./cluster"
|
||||
|
||||
export class ClusterManager {
|
||||
static get clusterIconDir() {
|
||||
return path.join(app.getPath("userData"), "icons");
|
||||
}
|
||||
|
||||
constructor(public readonly port: number) {
|
||||
// auto-init clusters
|
||||
autorun(() => {
|
||||
@@ -32,13 +14,15 @@ export class ClusterManager {
|
||||
.filter(cluster => !cluster.initialized)
|
||||
.forEach(cluster => cluster.init(port));
|
||||
});
|
||||
|
||||
// auto-stop removed clusters
|
||||
autorun(() => {
|
||||
clusterStore.removedClusters.forEach(cluster => cluster.stop());
|
||||
clusterStore.removedClusters.clear();
|
||||
});
|
||||
// listen ipc-events
|
||||
ClusterManager.ipcListen(this);
|
||||
|
||||
// listen ipc-events which could be handled *only* in main-process (nodeIntegration=true)
|
||||
handleMessage(ClusterIpcEvent.STOP, this.stopCluster.bind(this));
|
||||
}
|
||||
|
||||
stop() {
|
||||
@@ -51,40 +35,11 @@ export class ClusterManager {
|
||||
return clusterStore.getById(id);
|
||||
}
|
||||
|
||||
protected async addCluster(clusterModel: ClusterModel): Promise<Cluster> {
|
||||
tracker.event("cluster", "add");
|
||||
try {
|
||||
await validateConfig(clusterModel.kubeConfigPath);
|
||||
return clusterStore.addCluster(clusterModel);
|
||||
} catch (error) {
|
||||
logger.error(`[CLUSTER-MANAGER]: add cluster error ${JSON.stringify(error)}`)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
protected stopCluster(clusterId: ClusterId) {
|
||||
tracker.event("cluster", "stop");
|
||||
this.getCluster(clusterId)?.stop();
|
||||
}
|
||||
|
||||
protected removeAllByWorkspace(workspaceId: string) {
|
||||
tracker.event("cluster", "remove-workspace");
|
||||
const clusters = clusterStore.getByWorkspaceId(workspaceId);
|
||||
clusters.forEach(cluster => {
|
||||
this.removeCluster(cluster.id);
|
||||
});
|
||||
}
|
||||
|
||||
protected removeCluster(clusterId: string): Cluster {
|
||||
tracker.event("cluster", "remove");
|
||||
const cluster = this.getCluster(clusterId);
|
||||
if (cluster) {
|
||||
cluster.stop()
|
||||
clusterStore.removeById(cluster.id);
|
||||
return cluster;
|
||||
}
|
||||
}
|
||||
|
||||
getClusterForRequest(req: http.IncomingMessage): Cluster {
|
||||
let cluster: Cluster = null
|
||||
|
||||
@@ -105,61 +60,4 @@ export class ClusterManager {
|
||||
|
||||
return cluster;
|
||||
}
|
||||
|
||||
protected async uploadClusterIcon({ clusterId, name: fileName, path: src }: ClusterIconUpload): Promise<string> {
|
||||
const cluster = this.getCluster(clusterId);
|
||||
if (cluster) {
|
||||
tracker.event("cluster", "upload-icon");
|
||||
await ensureDir(ClusterManager.clusterIconDir)
|
||||
fileName = filenamify(cluster.contextName + "-" + fileName)
|
||||
const dest = path.join(ClusterManager.clusterIconDir, fileName)
|
||||
await copyFile(src, dest)
|
||||
cluster.preferences.icon = `${appProto}:///icons/${fileName}`
|
||||
return cluster.preferences.icon;
|
||||
}
|
||||
}
|
||||
|
||||
// todo: remove current icon file ?
|
||||
protected resetClusterIcon(clusterId: ClusterId) {
|
||||
const cluster = this.getCluster(clusterId);
|
||||
if (cluster) {
|
||||
tracker.event("cluster", "reset-icon")
|
||||
cluster.preferences.icon = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected async installFeature({ clusterId, name, config }: FeatureInstallRequest) {
|
||||
tracker.event("cluster", "install-feature")
|
||||
return this.getCluster(clusterId)?.installFeature(name, config)
|
||||
}
|
||||
|
||||
protected async upgradeFeature({ clusterId, name, config }: FeatureInstallRequest) {
|
||||
tracker.event("cluster", "upgrade-feature")
|
||||
return this.getCluster(clusterId)?.upgradeFeature(name, config)
|
||||
}
|
||||
|
||||
protected async uninstallFeature({ clusterId, name }: FeatureInstallRequest) {
|
||||
tracker.event("cluster", "uninstall-feature")
|
||||
return this.getCluster(clusterId)?.uninstallFeature(name);
|
||||
}
|
||||
|
||||
static ipcListen(clusterManager: ClusterManager) {
|
||||
const handlers = {
|
||||
[ClusterIpcMessage.ADD]: clusterManager.addCluster,
|
||||
[ClusterIpcMessage.STOP]: clusterManager.stopCluster,
|
||||
[ClusterIpcMessage.REMOVE]: clusterManager.removeCluster,
|
||||
[ClusterIpcMessage.REMOVE_WORKSPACE]: clusterManager.removeAllByWorkspace,
|
||||
[ClusterIpcMessage.FEATURE_INSTALL]: clusterManager.installFeature,
|
||||
[ClusterIpcMessage.FEATURE_UPGRADE]: clusterManager.upgradeFeature,
|
||||
[ClusterIpcMessage.FEATURE_REMOVE]: clusterManager.uninstallFeature,
|
||||
[ClusterIpcMessage.ICON_SAVE]: clusterManager.uploadClusterIcon,
|
||||
[ClusterIpcMessage.ICON_RESET]: clusterManager.removeCluster,
|
||||
};
|
||||
Object.entries(handlers).forEach(([key, handler]) => {
|
||||
handlers[key as keyof typeof handlers] = handler.bind(clusterManager);
|
||||
})
|
||||
handleMessages(handlers, {
|
||||
timeout: 2000
|
||||
})
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -13,7 +13,11 @@ import { getFeatures, installFeature, uninstallFeature, upgradeFeature } from ".
|
||||
import request, { RequestPromiseOptions } from "request-promise-native"
|
||||
import logger from "./logger"
|
||||
|
||||
enum ClusterStatus {
|
||||
export enum ClusterIpcEvent {
|
||||
STOP = "cluster:stop",
|
||||
}
|
||||
|
||||
export enum ClusterStatus {
|
||||
AccessGranted = 2,
|
||||
AccessDenied = 1,
|
||||
Offline = 0
|
||||
@@ -310,7 +314,7 @@ export class Cluster implements ClusterModel {
|
||||
})
|
||||
}
|
||||
|
||||
// serializable full-featured state of the cluster
|
||||
// serializable cluster-info for push-notifications
|
||||
getState(): ClusterState {
|
||||
const state: ClusterState = {
|
||||
...this.toJSON(),
|
||||
|
||||
Reference in new issue
Block a user