diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index af2eb826d9..b8c371fb71 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -1,32 +1,16 @@ import ElectronStore from "electron-store" +import migrations from "../migrations/cluster-store" import { Cluster, ClusterBaseInfo } from "../main/cluster"; -import * as version200Beta2 from "../migrations/cluster-store/2.0.0-beta.2" -import * as version241 from "../migrations/cluster-store/2.4.1" -import * as version260Beta2 from "../migrations/cluster-store/2.6.0-beta.2" -import * as version260Beta3 from "../migrations/cluster-store/2.6.0-beta.3" -import * as version270Beta0 from "../migrations/cluster-store/2.7.0-beta.0" -import * as version270Beta1 from "../migrations/cluster-store/2.7.0-beta.1" -import { getAppVersion } from "./utils/app-version"; export class ClusterStore { private static instance: ClusterStore; - public store: ElectronStore; + private store: ElectronStore; private constructor() { this.store = new ElectronStore({ - // @ts-ignore - // fixme: tests are failed without "projectVersion" - projectVersion: getAppVersion(), name: "lens-cluster-store", accessPropertiesByDotNotation: false, // To make dots safe in cluster context names - migrations: { - "2.0.0-beta.2": version200Beta2.migration, - "2.4.1": version241.migration, - "2.6.0-beta.2": version260Beta2.migration, - "2.6.0-beta.3": version260Beta3.migration, - "2.7.0-beta.0": version270Beta0.migration, - "2.7.0-beta.1": version270Beta1.migration - } + migrations: migrations, }) } @@ -78,8 +62,7 @@ export class ClusterStore { } if (index === -1) { clusters.push(storable) - } - else { + } else { clusters[index] = storable } this.store.set("clusters", clusters) diff --git a/src/common/user-store.ts b/src/common/user-store.ts index fd3ea296fc..0183dc5d67 100644 --- a/src/common/user-store.ts +++ b/src/common/user-store.ts @@ -1,10 +1,5 @@ import ElectronStore from "electron-store" -import * as version210Beta4 from "../migrations/user-store/2.1.0-beta.4" -import { getAppVersion } from "./utils/app-version"; - -export interface User { - id?: string; -} +import migrations from "../migrations/user-store" export interface UserPreferences { httpsProxy?: string; @@ -20,12 +15,7 @@ export class UserStore { private constructor() { this.store = new ElectronStore({ - // @ts-ignore - // fixme: tests are failed without "projectVersion" - projectVersion: getAppVersion(), - migrations: { - "2.1.0-beta.4": version210Beta4.migration, - } + migrations: migrations, }); } @@ -81,6 +71,4 @@ export class UserStore { } } -const userStore: UserStore = UserStore.getInstance(); - -export { userStore }; +export const userStore: UserStore = UserStore.getInstance(); diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index daf0016b36..06abe913c8 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -20,7 +20,7 @@ export class Workspace implements WorkspaceData { export class WorkspaceStore { public static defaultId = "default" private static instance: WorkspaceStore; - public store: ElectronStore; + private store: ElectronStore; private constructor() { this.store = new ElectronStore({ diff --git a/src/migrations/cluster-store/2.0.0-beta.2.ts b/src/migrations/cluster-store/2.0.0-beta.2.ts index 3ec4f948f5..8a01af5407 100644 --- a/src/migrations/cluster-store/2.0.0-beta.2.ts +++ b/src/migrations/cluster-store/2.0.0-beta.2.ts @@ -1,17 +1,16 @@ /* Early store format had the kubeconfig directly under context name, this moves it under the kubeConfig key */ -import { isTestEnv } from "../../common/vars"; +import { migration } from "../migration-wrapper"; -export function migration(store: any) { - if(!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.0.0-beta.2"); +export default migration({ + version: "2.0.0-beta.2", + run(store, log) { + for (const value of store) { + const contextName = value[0]; + // Looping all the keys gives out the store internal stuff too... + if (contextName === "__internal__" || value[1].hasOwnProperty('kubeConfig')) continue; + store.set(contextName, { kubeConfig: value[1] }); + } } - for (const value of store) { - const contextName = value[0]; - // Looping all the keys gives out the store internal stuff too... - if(contextName === "__internal__" || value[1].hasOwnProperty('kubeConfig')) continue; - - store.set(contextName, { kubeConfig: value[1] }); - } -} +}) \ No newline at end of file diff --git a/src/migrations/cluster-store/2.4.1.ts b/src/migrations/cluster-store/2.4.1.ts index 7652ebedf6..5789f6cc36 100644 --- a/src/migrations/cluster-store/2.4.1.ts +++ b/src/migrations/cluster-store/2.4.1.ts @@ -1,15 +1,14 @@ // Cleans up a store that had the state related data stored -import { isTestEnv } from "../../common/vars"; +import { migration } from "../migration-wrapper"; -export function migration(store: any) { - if (!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.4.1"); +export default migration({ + version: "2.4.1", + run(store, log) { + for (const value of store) { + const contextName = value[0]; + if (contextName === "__internal__") continue; + const cluster = value[1]; + store.set(contextName, { kubeConfig: cluster.kubeConfig, icon: cluster.icon || null, preferences: cluster.preferences || {} }); + } } - for (const value of store) { - const contextName = value[0]; - if (contextName === "__internal__") continue; - const cluster = value[1]; - - store.set(contextName, { kubeConfig: cluster.kubeConfig, icon: cluster.icon || null, preferences: cluster.preferences || {} }); - } -} +}) diff --git a/src/migrations/cluster-store/2.6.0-beta.2.ts b/src/migrations/cluster-store/2.6.0-beta.2.ts index 48d5b48734..0e13afe7a9 100644 --- a/src/migrations/cluster-store/2.6.0-beta.2.ts +++ b/src/migrations/cluster-store/2.6.0-beta.2.ts @@ -1,19 +1,19 @@ // Move cluster icon from root to preferences -import { isTestEnv } from "../../common/vars"; +import { migration } from "../migration-wrapper"; -export function migration(store: any) { - if(!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.6.0-beta.2"); - } - for (const value of store) { - const clusterKey = value[0]; - if(clusterKey === "__internal__") continue - const cluster = value[1]; - if(!cluster.preferences) cluster.preferences = {}; - if(cluster.icon) { - cluster.preferences.icon = cluster.icon; - delete(cluster["icon"]); +export default migration({ + version: "2.6.0-beta.2", + run(store, log) { + for (const value of store) { + const clusterKey = value[0]; + if (clusterKey === "__internal__") continue + const cluster = value[1]; + if (!cluster.preferences) cluster.preferences = {}; + if (cluster.icon) { + cluster.preferences.icon = cluster.icon; + delete (cluster["icon"]); + } + store.set(clusterKey, { contextName: clusterKey, kubeConfig: value[1].kubeConfig, preferences: value[1].preferences }); } - store.set(clusterKey, { contextName: clusterKey, kubeConfig: value[1].kubeConfig, preferences: value[1].preferences }); } -} +}) diff --git a/src/migrations/cluster-store/2.6.0-beta.3.ts b/src/migrations/cluster-store/2.6.0-beta.3.ts index d8063764c0..11f1a3bce9 100644 --- a/src/migrations/cluster-store/2.6.0-beta.3.ts +++ b/src/migrations/cluster-store/2.6.0-beta.3.ts @@ -1,38 +1,38 @@ -import * as yaml from "js-yaml" -import { isTestEnv } from "../../common/vars"; +import { migration } from "../migration-wrapper"; +import yaml from "js-yaml" -// Convert access token and expiry from arrays into strings -export function migration(store: any) { - if(!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.6.0-beta.3"); - } - for (const value of store) { - const clusterKey = value[0]; - if(clusterKey === "__internal__") continue - const cluster = value[1]; - if(!cluster.kubeConfig) continue - const kubeConfig = yaml.safeLoad(cluster.kubeConfig) - if(!kubeConfig.hasOwnProperty('users')) continue - const userObj = kubeConfig.users[0] - if (userObj) { - const user = userObj.user - if (user["auth-provider"] && user["auth-provider"].config) { - const authConfig = user["auth-provider"].config - if (authConfig["access-token"]) { - authConfig["access-token"] = `${authConfig["access-token"]}` +export default migration({ + version: "2.6.0-beta.3", + run(store, log) { + for (const value of store) { + const clusterKey = value[0]; + if (clusterKey === "__internal__") continue + const cluster = value[1]; + if (!cluster.kubeConfig) continue + const kubeConfig = yaml.safeLoad(cluster.kubeConfig) + if (!kubeConfig.hasOwnProperty('users')) continue + const userObj = kubeConfig.users[0] + if (userObj) { + const user = userObj.user + if (user["auth-provider"] && user["auth-provider"].config) { + const authConfig = user["auth-provider"].config + if (authConfig["access-token"]) { + authConfig["access-token"] = `${authConfig["access-token"]}` + } + if (authConfig.expiry) { + authConfig.expiry = `${authConfig.expiry}` + } + log(authConfig) + user["auth-provider"].config = authConfig + kubeConfig.users = [{ + name: userObj.name, + user: user + }] + cluster.kubeConfig = yaml.safeDump(kubeConfig) + store.set(clusterKey, cluster) } - if (authConfig.expiry) { - authConfig.expiry = `${authConfig.expiry}` - } - console.log(authConfig) - user["auth-provider"].config = authConfig - kubeConfig.users = [{ - name: userObj.name, - user: user - }] - cluster.kubeConfig = yaml.safeDump(kubeConfig) - store.set(clusterKey, cluster) } } } -} +}) + diff --git a/src/migrations/cluster-store/2.7.0-beta.0.ts b/src/migrations/cluster-store/2.7.0-beta.0.ts index 1b567fe933..22c4e6bba9 100644 --- a/src/migrations/cluster-store/2.7.0-beta.0.ts +++ b/src/migrations/cluster-store/2.7.0-beta.0.ts @@ -1,15 +1,15 @@ // Add existing clusters to "default" workspace -import { isTestEnv } from "../../common/vars"; +import { migration } from "../migration-wrapper"; -export function migration(store: any) { - if(!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.7.0-beta.0"); +export default migration({ + version: "2.7.0-beta.0", + run(store, log) { + for (const value of store) { + const clusterKey = value[0]; + if(clusterKey === "__internal__") continue + const cluster = value[1]; + cluster.workspace = "default" + store.set(clusterKey, cluster) + } } - for (const value of store) { - const clusterKey = value[0]; - if(clusterKey === "__internal__") continue - const cluster = value[1]; - cluster.workspace = "default" - store.set(clusterKey, cluster) - } -} +}) diff --git a/src/migrations/cluster-store/2.7.0-beta.1.ts b/src/migrations/cluster-store/2.7.0-beta.1.ts index 4918041245..de9e4506d1 100644 --- a/src/migrations/cluster-store/2.7.0-beta.1.ts +++ b/src/migrations/cluster-store/2.7.0-beta.1.ts @@ -1,25 +1,25 @@ -// add id for clusters and store them to array +// Add id for clusters and store them to array +import { migration } from "../migration-wrapper"; import { v4 as uuid } from "uuid" -import { isTestEnv } from "../../common/vars"; -export function migration(store: any) { - if(!isTestEnv) { - console.log("CLUSTER STORE, MIGRATION: 2.7.0-beta.1"); - } - const clusters: any[] = [] - for (const value of store) { - const clusterKey = value[0]; - if(clusterKey === "__internal__") continue - if(clusterKey === "clusters") continue - const cluster = value[1]; - cluster.id = uuid() - if (!cluster.preferences.clusterName) { - cluster.preferences.clusterName = clusterKey +export default migration({ + version: "2.7.0-beta.1", + run(store, log) { + const clusters: any[] = [] + for (const value of store) { + const clusterKey = value[0]; + if (clusterKey === "__internal__") continue + if (clusterKey === "clusters") continue + const cluster = value[1]; + cluster.id = uuid() + if (!cluster.preferences.clusterName) { + cluster.preferences.clusterName = clusterKey + } + clusters.push(cluster) + store.delete(clusterKey) + } + if (clusters.length > 0) { + store.set("clusters", clusters) } - clusters.push(cluster) - store.delete(clusterKey) } - if (clusters.length > 0) { - store.set("clusters", clusters) - } -} +}) diff --git a/src/migrations/cluster-store/index.ts b/src/migrations/cluster-store/index.ts new file mode 100644 index 0000000000..b531d606c4 --- /dev/null +++ b/src/migrations/cluster-store/index.ts @@ -0,0 +1,17 @@ +// Cluster store migrations + +import version200Beta2 from "./2.0.0-beta.2" +import version241 from "./2.4.1" +import version260Beta2 from "./2.6.0-beta.2" +import version260Beta3 from "./2.6.0-beta.3" +import version270Beta0 from "./2.7.0-beta.0" +import version270Beta1 from "./2.7.0-beta.1" + +export default { + ...version200Beta2, + ...version241, + ...version260Beta2, + ...version260Beta3, + ...version270Beta0, + ...version270Beta1, +} \ No newline at end of file diff --git a/src/migrations/migration-wrapper.ts b/src/migrations/migration-wrapper.ts new file mode 100644 index 0000000000..c4d7d8694d --- /dev/null +++ b/src/migrations/migration-wrapper.ts @@ -0,0 +1,21 @@ +import ElectronStore from "electron-store"; +import { isTestEnv } from "../common/vars"; + +export interface MigrationOpts { + version: string; + run(store: ElectronStore, log: (...args: any[]) => void): void; +} + +function infoLog(...args: any[]) { + if (isTestEnv) return; + console.log(...args); +} + +export function migration({ version, run }: MigrationOpts) { + return { + [version]: (store: ElectronStore) => { + infoLog(`CLUSTER STORE, MIGRATION: ${version}`); + run(store, infoLog); + } + }; +} diff --git a/src/migrations/user-store/2.1.0-beta.4.ts b/src/migrations/user-store/2.1.0-beta.4.ts index 987e9a07d1..24c4cde5e3 100644 --- a/src/migrations/user-store/2.1.0-beta.4.ts +++ b/src/migrations/user-store/2.1.0-beta.4.ts @@ -1,4 +1,9 @@ // Add / reset "lastSeenAppVersion" -export function migration(store: any) { - store.set("lastSeenAppVersion", "0.0.0"); -} +import { migration } from "../migration-wrapper"; + +export default migration({ + version: "2.1.0-beta.4", + run(store) { + store.set("lastSeenAppVersion", "0.0.0"); + } +}) diff --git a/src/migrations/user-store/index.ts b/src/migrations/user-store/index.ts new file mode 100644 index 0000000000..895bc5ee18 --- /dev/null +++ b/src/migrations/user-store/index.ts @@ -0,0 +1,7 @@ +// User store migrations + +import version210Beta4 from "./2.1.0-beta.4" + +export default { + ...version210Beta4, +}