mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Remove ClusterStore.removedClusters and correctly delete lens storage file (#4077)
This commit is contained in:
parent
b4a4cc6be4
commit
32562b11a4
@ -40,7 +40,6 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
|||||||
private static StateChannel = "cluster:state";
|
private static StateChannel = "cluster:state";
|
||||||
|
|
||||||
clusters = observable.map<ClusterId, Cluster>();
|
clusters = observable.map<ClusterId, Cluster>();
|
||||||
removedClusters = observable.map<ClusterId, Cluster>();
|
|
||||||
|
|
||||||
protected disposer = disposer();
|
protected disposer = disposer();
|
||||||
|
|
||||||
@ -144,7 +143,6 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
|||||||
protected fromStore({ clusters = [] }: ClusterStoreModel = {}) {
|
protected fromStore({ clusters = [] }: ClusterStoreModel = {}) {
|
||||||
const currentClusters = new Map(this.clusters);
|
const currentClusters = new Map(this.clusters);
|
||||||
const newClusters = new Map<ClusterId, Cluster>();
|
const newClusters = new Map<ClusterId, Cluster>();
|
||||||
const removedClusters = new Map<ClusterId, Cluster>();
|
|
||||||
|
|
||||||
// update new clusters
|
// update new clusters
|
||||||
for (const clusterModel of clusters) {
|
for (const clusterModel of clusters) {
|
||||||
@ -162,15 +160,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update removed clusters
|
|
||||||
currentClusters.forEach(cluster => {
|
|
||||||
if (!newClusters.has(cluster.id)) {
|
|
||||||
removedClusters.set(cluster.id, cluster);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.clusters.replace(newClusters);
|
this.clusters.replace(newClusters);
|
||||||
this.removedClusters.replace(removedClusters);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON(): ClusterStoreModel {
|
toJSON(): ClusterStoreModel {
|
||||||
|
|||||||
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
import "../common/cluster-ipc";
|
import "../common/cluster-ipc";
|
||||||
import type http from "http";
|
import type http from "http";
|
||||||
import { action, autorun, makeObservable, observable, observe, reaction, toJS } from "mobx";
|
import { action, makeObservable, observable, observe, reaction, toJS } from "mobx";
|
||||||
import { Cluster } from "./cluster";
|
import { Cluster } from "./cluster";
|
||||||
import logger from "./logger";
|
import logger from "./logger";
|
||||||
import { apiKubePrefix } from "../common/vars";
|
import { apiKubePrefix } from "../common/vars";
|
||||||
@ -71,21 +71,6 @@ export class ClusterManager extends Singleton {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// auto-stop removed clusters
|
|
||||||
autorun(() => {
|
|
||||||
const removedClusters = Array.from(this.store.removedClusters.values());
|
|
||||||
|
|
||||||
if (removedClusters.length > 0) {
|
|
||||||
const meta = removedClusters.map(cluster => cluster.getMeta());
|
|
||||||
|
|
||||||
logger.info(`${logPrefix} removing clusters`, meta);
|
|
||||||
removedClusters.forEach(cluster => cluster.disconnect());
|
|
||||||
this.store.removedClusters.clear();
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
delay: 250
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcMainOn("network:offline", this.onNetworkOffline);
|
ipcMainOn("network:offline", this.onNetworkOffline);
|
||||||
ipcMainOn("network:online", this.onNetworkOnline);
|
ipcMainOn("network:online", this.onNetworkOnline);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { BrowserWindow, dialog, IpcMainInvokeEvent } from "electron";
|
import { app, BrowserWindow, dialog, IpcMainInvokeEvent } from "electron";
|
||||||
import { KubernetesCluster } from "../../common/catalog-entities";
|
import { KubernetesCluster } from "../../common/catalog-entities";
|
||||||
import { clusterFrameMap } from "../../common/cluster-frames";
|
import { clusterFrameMap } from "../../common/cluster-frames";
|
||||||
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../common/cluster-ipc";
|
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../common/cluster-ipc";
|
||||||
@ -32,6 +32,8 @@ import { pushCatalogToRenderer } from "../catalog-pusher";
|
|||||||
import { ClusterManager } from "../cluster-manager";
|
import { ClusterManager } from "../cluster-manager";
|
||||||
import { ResourceApplier } from "../resource-applier";
|
import { ResourceApplier } from "../resource-applier";
|
||||||
import { WindowManager } from "../window-manager";
|
import { WindowManager } from "../window-manager";
|
||||||
|
import path from "path";
|
||||||
|
import { remove } from "fs-extra";
|
||||||
|
|
||||||
export function initIpcMainHandlers() {
|
export function initIpcMainHandlers() {
|
||||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||||
@ -79,9 +81,11 @@ export function initIpcMainHandlers() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMainHandle(clusterDeleteHandler, (event, clusterId: ClusterId) => {
|
ipcMainHandle(clusterDeleteHandler, async (event, clusterId: ClusterId) => {
|
||||||
appEventBus.emit({ name: "cluster", action: "remove" });
|
appEventBus.emit({ name: "cluster", action: "remove" });
|
||||||
const cluster = ClusterStore.getInstance().getById(clusterId);
|
|
||||||
|
const clusterStore = ClusterStore.getInstance();
|
||||||
|
const cluster = clusterStore.getById(clusterId);
|
||||||
|
|
||||||
if (!cluster) {
|
if (!cluster) {
|
||||||
return;
|
return;
|
||||||
@ -89,6 +93,16 @@ export function initIpcMainHandlers() {
|
|||||||
|
|
||||||
cluster.disconnect();
|
cluster.disconnect();
|
||||||
clusterFrameMap.delete(cluster.id);
|
clusterFrameMap.delete(cluster.id);
|
||||||
|
|
||||||
|
// Remove from the cluster store as well, this should clear any old settings
|
||||||
|
clusterStore.clusters.delete(cluster.id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// remove the local storage file
|
||||||
|
const localStorageFilePath = path.resolve(app.getPath("userData"), "lens-local-storage", `${cluster.id}.json`);
|
||||||
|
|
||||||
|
await remove(localStorageFilePath);
|
||||||
|
} catch {}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMainHandle(clusterSetDeletingHandler, (event, clusterId: string) => {
|
ipcMainHandle(clusterSetDeletingHandler, (event, clusterId: string) => {
|
||||||
|
|||||||
@ -116,6 +116,7 @@ export class DeleteClusterDialog extends React.Component {
|
|||||||
await requestMain(clusterDeleteHandler, cluster.id);
|
await requestMain(clusterDeleteHandler, cluster.id);
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`);
|
Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`);
|
||||||
|
} finally {
|
||||||
await requestMain(clusterClearDeletingHandler, cluster.id);
|
await requestMain(clusterClearDeletingHandler, cluster.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,6 @@ import path from "path";
|
|||||||
import { comparer, observable, reaction, toJS, when } from "mobx";
|
import { comparer, observable, reaction, toJS, when } from "mobx";
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { StorageHelper } from "./storageHelper";
|
import { StorageHelper } from "./storageHelper";
|
||||||
import { ClusterStore } from "../../common/cluster-store";
|
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
import { getHostedClusterId, getPath } from "../../common/utils";
|
import { getHostedClusterId, getPath } from "../../common/utils";
|
||||||
import { isTestEnv } from "../../common/vars";
|
import { isTestEnv } from "../../common/vars";
|
||||||
@ -75,11 +74,6 @@ export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: st
|
|||||||
equals: comparer.structural, // save only when something really changed
|
equals: comparer.structural, // save only when something really changed
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove json-file when cluster deleted
|
|
||||||
if (clusterId !== undefined) {
|
|
||||||
when(() => ClusterStore.getInstance(false)?.removedClusters.has(clusterId)).then(removeFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function saveFile(state: Record<string, any> = {}) {
|
async function saveFile(state: Record<string, any> = {}) {
|
||||||
logger.info(`${logPrefix} saving ${filePath}`);
|
logger.info(`${logPrefix} saving ${filePath}`);
|
||||||
|
|
||||||
@ -92,11 +86,6 @@ export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: st
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeFile() {
|
|
||||||
logger.debug(`${logPrefix} removing ${filePath}`);
|
|
||||||
fse.unlink(filePath).catch(Function);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StorageHelper<T>(key, {
|
return new StorageHelper<T>(key, {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user