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

Switch to deterministic cluster IDs (#3201)

This commit is contained in:
Sebastian Malton 2021-06-28 11:14:55 -04:00 committed by GitHub
parent 7e8cc2122c
commit 2788025fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 152 additions and 4 deletions

View File

@ -3,7 +3,7 @@
"productName": "OpenLens", "productName": "OpenLens",
"description": "OpenLens - Open Source IDE for Kubernetes", "description": "OpenLens - Open Source IDE for Kubernetes",
"homepage": "https://github.com/lensapp/lens", "homepage": "https://github.com/lensapp/lens",
"version": "5.0.0-beta.12", "version": "5.0.0-beta.13",
"main": "static/build/main.js", "main": "static/build/main.js",
"copyright": "© 2021 OpenLens Authors", "copyright": "© 2021 OpenLens Authors",
"license": "MIT", "license": "MIT",

View File

@ -72,7 +72,7 @@ export interface ClusterModel {
workspace?: string; workspace?: string;
/** User context in kubeconfig */ /** User context in kubeconfig */
contextName?: string; contextName: string;
/** Preferences */ /** Preferences */
preferences?: ClusterPreferences; preferences?: ClusterPreferences;

View File

@ -0,0 +1,120 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import type { ClusterModel, ClusterPreferences, ClusterPrometheusPreferences } from "../../common/cluster-store";
import { MigrationDeclaration, migrationLog } from "../helpers";
import { generateNewIdFor } from "../utils";
import path from "path";
import { app } from "electron";
import { moveSync, removeSync } from "fs-extra";
function mergePrometheusPreferences(left: ClusterPrometheusPreferences, right: ClusterPrometheusPreferences): ClusterPrometheusPreferences {
if (left.prometheus && left.prometheusProvider) {
return {
prometheus: left.prometheus,
prometheusProvider: left.prometheusProvider,
};
}
if (right.prometheus && right.prometheusProvider) {
return {
prometheus: right.prometheus,
prometheusProvider: right.prometheusProvider,
};
}
return {};
}
function mergePreferences(left: ClusterPreferences, right: ClusterPreferences): ClusterPreferences {
return {
terminalCWD: left.terminalCWD || right.terminalCWD || undefined,
clusterName: left.clusterName || right.clusterName || undefined,
iconOrder: left.iconOrder || right.iconOrder || undefined,
icon: left.icon || right.icon || undefined,
httpsProxy: left.httpsProxy || right.httpsProxy || undefined,
hiddenMetrics: mergeSet(left.hiddenMetrics ?? [], right.hiddenMetrics ?? []),
...mergePrometheusPreferences(left, right),
};
}
function mergeLabels(left: Record<string, string>, right: Record<string, string>): Record<string, string> {
return {
...right,
...left
};
}
function mergeSet(left: Iterable<string>, right: Iterable<string>): string[] {
return [...new Set([...left, ...right])];
}
function mergeClusterModel(prev: ClusterModel, right: Omit<ClusterModel, "id">): ClusterModel {
return {
id: prev.id,
kubeConfigPath: prev.id,
contextName: prev.contextName,
preferences: mergePreferences(prev.preferences ?? {}, right.preferences ?? {}),
metadata: prev.metadata,
labels: mergeLabels(prev.labels ?? {}, right.labels ?? {}),
accessibleNamespaces: mergeSet(prev.accessibleNamespaces ?? [], right.accessibleNamespaces ?? []),
};
}
function moveStorageFolder({ folder, newId, oldId }: { folder: string, newId: string, oldId: string }): void {
const oldPath = path.resolve(folder, `${oldId}.json`);
const newPath = path.resolve(folder, `${newId}.json`);
try {
moveSync(oldPath, newPath);
} catch (error) {
if (String(error).includes("dest already exists")) {
migrationLog(`Multiple old lens-local-storage files for newId=${newId}. Removing ${oldId}.json`);
removeSync(oldPath);
}
}
}
export default {
version: "5.0.0-beta.13",
run(store) {
const folder = path.resolve(app.getPath("userData"), "lens-local-storage");
const oldClusters: ClusterModel[] = store.get("clusters");
const clusters = new Map<string, ClusterModel>();
for (const { id: oldId, ...cluster } of oldClusters) {
const newId = generateNewIdFor(cluster);
if (clusters.has(newId)) {
clusters.set(newId, mergeClusterModel(clusters.get(newId), cluster));
} else {
clusters.set(newId, {
...cluster,
id: newId,
});
moveStorageFolder({ folder, newId, oldId });
}
}
store.set("clusters", [...clusters.values()]);
}
} as MigrationDeclaration;

View File

@ -31,6 +31,7 @@ import version270Beta0 from "./2.7.0-beta.0";
import version270Beta1 from "./2.7.0-beta.1"; import version270Beta1 from "./2.7.0-beta.1";
import version360Beta1 from "./3.6.0-beta.1"; import version360Beta1 from "./3.6.0-beta.1";
import version500Beta10 from "./5.0.0-beta.10"; import version500Beta10 from "./5.0.0-beta.10";
import version500Beta13 from "./5.0.0-beta.13";
import snap from "./snap"; import snap from "./snap";
export default joinMigrations( export default joinMigrations(
@ -42,5 +43,6 @@ export default joinMigrations(
version270Beta1, version270Beta1,
version360Beta1, version360Beta1,
version500Beta10, version500Beta10,
version500Beta13,
snap, snap,
); );

View File

@ -19,7 +19,6 @@
* 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 { createHash } from "crypto";
import { app } from "electron"; import { app } from "electron";
import fse from "fs-extra"; import fse from "fs-extra";
import { isNull } from "lodash"; import { isNull } from "lodash";
@ -29,6 +28,7 @@ import type { ClusterStoreModel } from "../../common/cluster-store";
import { defaultHotbarCells, Hotbar, HotbarStore } from "../../common/hotbar-store"; import { defaultHotbarCells, Hotbar, HotbarStore } from "../../common/hotbar-store";
import { catalogEntity } from "../../main/catalog-sources/general"; import { catalogEntity } from "../../main/catalog-sources/general";
import type { MigrationDeclaration } from "../helpers"; import type { MigrationDeclaration } from "../helpers";
import { generateNewIdFor } from "../utils";
interface Pre500WorkspaceStoreModel { interface Pre500WorkspaceStoreModel {
workspaces: { workspaces: {
@ -74,7 +74,7 @@ export default {
if (workspaceHotbar?.items.length < defaultHotbarCells) { if (workspaceHotbar?.items.length < defaultHotbarCells) {
workspaceHotbar.items.push({ workspaceHotbar.items.push({
entity: { entity: {
uid: createHash("md5").update(`${cluster.kubeConfigPath}:${cluster.contextName}`).digest("hex"), uid: generateNewIdFor(cluster),
name: cluster.preferences.clusterName || cluster.contextName, name: cluster.preferences.clusterName || cluster.contextName,
} }
}); });

26
src/migrations/utils.ts Normal file
View File

@ -0,0 +1,26 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { createHash } from "crypto";
export function generateNewIdFor(cluster: { kubeConfigPath: string, contextName: string }): string {
return createHash("md5").update(`${cluster.kubeConfigPath}:${cluster.contextName}`).digest("hex");
}