From a33327d0bb071ce4e565e583338a6317f761a040 Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Thu, 14 Jan 2021 12:59:49 +0200 Subject: [PATCH] Ignore clusters with invalid kubeconfig Signed-off-by: Lauri Nevala --- src/common/__tests__/cluster-store.test.ts | 70 ++++++++++++++++++++++ src/common/cluster-store.ts | 11 +++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/common/__tests__/cluster-store.test.ts b/src/common/__tests__/cluster-store.test.ts index fcbd5ebd6b..147500871b 100644 --- a/src/common/__tests__/cluster-store.test.ts +++ b/src/common/__tests__/cluster-store.test.ts @@ -247,6 +247,76 @@ describe("config with existing clusters", () => { }); }); +describe("config with invalid cluster kubeconfig", () => { + beforeEach(() => { + const invalidKubeconfig = ` +apiVersion: v1 +clusters: +- cluster: + server: https://localhost + name: test2 +contexts: +- context: + cluster: test + user: test + name: test +current-context: test +kind: Config +preferences: {} +users: +- name: test + user: + token: kubeconfig-user-q4lm4:xxxyyyy +`; + + ClusterStore.resetInstance(); + const mockOpts = { + "tmp": { + "lens-cluster-store.json": JSON.stringify({ + __internal__: { + migrations: { + version: "99.99.99" + } + }, + clusters: [ + { + id: "cluster1", + kubeConfigPath: invalidKubeconfig, + contextName: "test", + preferences: { terminalCWD: "/foo" }, + workspace: "foo", + }, + { + id: "cluster2", + kubeConfig: "foo", + contextName: "foo", + preferences: { terminalCWD: "/foo" }, + workspace: "default" + }, + + ] + }) + } + }; + + mockFs(mockOpts); + clusterStore = ClusterStore.getInstance(); + + return clusterStore.load(); + }); + + afterEach(() => { + mockFs.restore(); + }); + + it("ignores clusters with invalid kubeconfig", () => { + const storedClusters = clusterStore.clustersList; + + expect(storedClusters.length).toBe(1); + expect(storedClusters[0].id).toBe("cluster2"); + }); +}); + describe("pre 2.0 config with an existing cluster", () => { beforeEach(() => { ClusterStore.resetInstance(); diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index d8bd28f1e8..2792239a84 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -321,10 +321,15 @@ export class ClusterStore extends BaseStore { if (cluster) { cluster.updateModel(clusterModel); } else { - cluster = new Cluster(clusterModel); + try { + cluster = new Cluster(clusterModel); - if (!cluster.isManaged) { - cluster.enabled = true; + if (!cluster.isManaged) { + cluster.enabled = true; + } + } catch (err) { + logger.error(`[CLUSTER-STORE] Failed to construct a cluster (context: ${clusterModel.contextName}, kubeconfig: ${clusterModel.kubeConfigPath})... Removing it from the app.`); + continue; } } newClusters.set(clusterModel.id, cluster);