From fe4a63a9556620be1be0bb72ed437c46169c6fb7 Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Thu, 24 Sep 2020 14:30:44 +0300 Subject: [PATCH] Allow to add the same cluster to multiple workspaces (#961) Signed-off-by: Lauri Nevala --- src/common/cluster-store.ts | 4 ++-- src/common/cluster-store_test.ts | 21 +++++++++++++++++-- .../components/+add-cluster/add-cluster.tsx | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index a7cd5cc3c5..d75de14609 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -122,8 +122,8 @@ export class ClusterStore extends BaseStore { return this.clusters.size > 0; } - hasContext(name: string) { - return this.clustersList.some(cluster => cluster.contextName === name); + hasContextInWorkspace(name: string, workspaceId: string) { + return this.clustersList.some(cluster => cluster.contextName === name && cluster.workspace === workspaceId); } getById(id: ClusterId): Cluster { diff --git a/src/common/cluster-store_test.ts b/src/common/cluster-store_test.ts index d7d6df4d2a..77eb688cfc 100644 --- a/src/common/cluster-store_test.ts +++ b/src/common/cluster-store_test.ts @@ -146,14 +146,22 @@ describe("config with existing clusters", () => { id: 'cluster1', kubeConfig: 'foo', contextName: 'foo', - preferences: { terminalCWD: '/foo' } + preferences: { terminalCWD: '/foo' }, + workspace: 'default' }, { id: 'cluster2', kubeConfig: 'foo2', contextName: 'foo2', preferences: { terminalCWD: '/foo2' } - } + }, + { + id: 'cluster3', + kubeConfig: 'foo', + contextName: 'foo', + preferences: { terminalCWD: '/foo' }, + workspace: 'foo' + }, ] }) } @@ -183,10 +191,19 @@ describe("config with existing clusters", () => { it("allows getting all of the clusters", async () => { const storedClusters = clusterStore.clustersList; + expect(storedClusters.length).toBe(3) expect(storedClusters[0].id).toBe('cluster1') expect(storedClusters[0].preferences.terminalCWD).toBe('/foo') expect(storedClusters[1].id).toBe('cluster2') expect(storedClusters[1].preferences.terminalCWD).toBe('/foo2') + expect(storedClusters[2].id).toBe('cluster3') + }) + + it("allows to test if store already has context in workspace", async () => { + const existingContext = clusterStore.hasContextInWorkspace('foo', 'default'); + expect(existingContext).toBeTruthy + const nonExistingContext = clusterStore.hasContextInWorkspace('foo2', 'foo'); + expect(existingContext).toBeFalsy }) }) diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index 624df4c1bf..4dd5371973 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -99,7 +99,7 @@ export class AddCluster extends React.Component { getContexts(config: KubeConfig): Map { const contexts = new Map(); splitConfig(config).forEach(config => { - const isExists = clusterStore.hasContext(config.currentContext); + const isExists = clusterStore.hasContextInWorkspace(config.currentContext, workspaceStore.currentWorkspaceId); if (!isExists) { contexts.set(config.currentContext, config); }