add cluster icon migration code (#673)

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2020-08-25 16:42:30 +03:00
1 parent b75cac4546
commit a12aa8a04f
8 files changed
+143 -34

No files matched your search

+1 -7
View File
@@ -1,6 +1,5 @@
import type { WorkspaceId } from "./workspace-store";
import path from "path";
import { app, ipcRenderer, remote } from "electron";
import { ipcRenderer } from "electron";
import { unlink } from "fs-extra";
import { action, computed, observable, toJS } from "mobx";
import { BaseStore } from "./base-store";
@@ -50,11 +49,6 @@ export interface ClusterPreferences {
}
export class ClusterStore extends BaseStore<ClusterStoreModel> {
static get iconsDir() {
// TODO: remove remote cheat
return path.join((app || remote.app).getPath("userData"), "icons");
}
private constructor() {
super({
configName: "lens-cluster-store",
+20 -7
View File
@@ -6,6 +6,10 @@ import { ClusterStore } from "./cluster-store";
import { workspaceStore } from "./workspace-store";
import { saveConfigToAppFiles } from "./kube-helpers";
const testDataIcon = fs.readFileSync("test-data/cluster-store-migration-icon.png")
console.log("") // fix bug
let clusterStore: ClusterStore;
describe("empty config", () => {
@@ -236,12 +240,13 @@ describe("pre 2.6.0 config with a cluster icon", () => {
},
cluster1: {
kubeConfig: "foo",
icon: "icon path",
icon: "icon_path",
preferences: {
terminalCWD: "/tmp"
}
},
})
}),
"icon_path": testDataIcon,
}
}
mockFs(mockOpts);
@@ -257,7 +262,7 @@ describe("pre 2.6.0 config with a cluster icon", () => {
const storedClusterData = clusterStore.clustersList[0];
expect(storedClusterData.hasOwnProperty('icon')).toBe(false);
expect(storedClusterData.preferences.hasOwnProperty('icon')).toBe(true);
expect(storedClusterData.preferences.icon).toBe("icon path");
expect(storedClusterData.preferences.icon.startsWith("data:image/jpeg;base64,")).toBe(true);
})
})
@@ -274,7 +279,6 @@ describe("for a pre 2.7.0-beta.0 config without a workspace", () => {
},
cluster1: {
kubeConfig: "foo",
icon: "icon path",
preferences: {
terminalCWD: "/tmp"
}
@@ -305,16 +309,20 @@ describe("pre 3.6.0-beta.1 config with an existing cluster", () => {
'lens-cluster-store.json': JSON.stringify({
__internal__: {
migrations: {
version: "2.7.0"
version: "3.5.0"
}
},
clusters: [
{
id: 'cluster1',
kubeConfig: 'kubeconfig content'
kubeConfig: 'kubeconfig content',
preferences: {
icon: "store://icon_path",
}
}
]
})
}),
"icon_path": testDataIcon,
}
};
mockFs(mockOpts);
@@ -330,4 +338,9 @@ describe("pre 3.6.0-beta.1 config with an existing cluster", () => {
const config = clusterStore.clustersList[0].kubeConfigPath;
expect(fs.readFileSync(config, "utf8")).toBe("kubeconfig content");
})
it("migrates to modern format with icon not in file", async () => {
const { icon } = clusterStore.clustersList[0].preferences;
expect(icon.startsWith("data:image/jpeg;base64, ")).toBe(true);
})
})
+1 -1
View File
@@ -6,7 +6,7 @@ export default migration({
run(store, log) {
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
if (clusterKey === "__internal__") continue
const cluster = value[1];
cluster.workspace = "default"
store.set(clusterKey, cluster)
+54 -17
View File
@@ -1,34 +1,71 @@
// Move embedded kubeconfig into separate file and add reference to it to cluster settings
// convert file path cluster icons to their base64 encoded versions
import path from "path"
import { app, remote } from "electron"
import { migration } from "../migration-wrapper";
import { ensureDirSync } from "fs-extra"
import fse from "fs-extra"
import { ClusterModel } from "../../common/cluster-store";
import { loadConfig, saveConfigToAppFiles } from "../../common/kube-helpers";
import makeSynchronous from "make-synchronous"
const AsyncFunction = Object.getPrototypeOf(async function () { return }).constructor;
const getFileTypeFnString = `return require("file-type").fromBuffer(fileData)`;
const getFileType = new AsyncFunction("fileData", getFileTypeFnString);
export default migration({
version: "3.6.0-beta.1",
run(store, printLog) {
const migratedClusters: ClusterModel[] = []
const storedClusters: ClusterModel[] = store.get("clusters");
const kubeConfigBase = path.join((app || remote.app).getPath("userData"), "kubeconfigs")
const userDataPath = (app || remote.app).getPath("userData")
const kubeConfigBase = path.join(userDataPath, "kubeconfigs")
const storedClusters: ClusterModel[] = store.get("clusters") || [];
if (!storedClusters) return;
ensureDirSync(kubeConfigBase);
if (!storedClusters.length) return;
fse.ensureDirSync(kubeConfigBase);
printLog("Number of clusters to migrate: ", storedClusters.length)
for (const cluster of storedClusters) {
try {
// take the embedded kubeconfig and dump it into a file
cluster.kubeConfigPath = saveConfigToAppFiles(cluster.id, cluster.kubeConfig)
cluster.contextName = loadConfig(cluster.kubeConfigPath).getCurrentContext();
delete cluster.kubeConfig;
migratedClusters.push(cluster)
} catch (error) {
printLog(`Failed to migrate Kubeconfig for cluster "${cluster.id}"`, error)
}
}
const migratedClusters = storedClusters
.map(cluster => {
/**
* migrate kubeconfig
*/
try {
// take the embedded kubeconfig and dump it into a file
cluster.kubeConfigPath = saveConfigToAppFiles(cluster.id, cluster.kubeConfig)
cluster.contextName = loadConfig(cluster.kubeConfigPath).getCurrentContext();
delete cluster.kubeConfig;
} catch (error) {
printLog(`Failed to migrate Kubeconfig for cluster "${cluster.id}", removing cluster...`, error)
return undefined;
}
/**
* migrate cluster icon
*/
try {
if (cluster.preferences?.icon) {
printLog(`migrating ${cluster.preferences.icon} for ${cluster.preferences.clusterName}`)
const iconPath = cluster.preferences.icon.replace("store://", "")
const fileData = fse.readFileSync(path.join(userDataPath, iconPath));
const { mime = "" } = makeSynchronous(getFileType)(fileData);
if (!mime) {
printLog(`mime type not detected for ${cluster.preferences.clusterName}'s icon: ${iconPath}`)
}
cluster.preferences.icon = `data:${mime};base64, ${fileData.toString('base64')}`;
} else {
delete cluster.preferences?.icon;
}
} catch (error) {
printLog(`Failed to migrate cluster icon for cluster "${cluster.id}"`, error)
delete cluster.preferences.icon;
}
return cluster;
})
.filter(c => c);
// "overwrite" the cluster configs
if (migratedClusters.length > 0) {
@@ -28,7 +28,7 @@ export class ClusterIconSetting extends React.Component<Props> {
try {
if (file) {
const buf = Buffer.from(await file.arrayBuffer());
cluster.preferences.icon = `data:image/jpeg;base64, ${buf.toString('base64')}`;
cluster.preferences.icon = `data:${file.type};base64, ${buf.toString('base64')}`;
} else {
// this has to be done as a seperate branch (and not always) because `cluster`
// is observable and triggers an update loop.