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

Ignore extensions' clusters when adding to kubeConfigSync

- To accomplish this we now ignore all configs that are logiaclly in a
  child directory of $userData/extension_data. This is done at the path
  level so as to minimize the number of FS interactions.

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-07-08 09:51:05 -04:00
parent 497e47b277
commit 6b742901f0
2 changed files with 41 additions and 13 deletions

View File

@ -19,10 +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 { Console } from "console";
console = new Console(process.stdout, process.stderr);
import mockFs from "mock-fs"; import mockFs from "mock-fs";
jest.mock("electron", () => { jest.mock("electron", () => {
@ -37,27 +33,27 @@ jest.mock("electron", () => {
}); });
import { UserStore } from "../user-store"; import { UserStore } from "../user-store";
import { Console } from "console";
import { SemVer } from "semver"; import { SemVer } from "semver";
import electron from "electron"; import electron from "electron";
import { stdout, stderr } from "process"; import { stdout, stderr } from "process";
import { beforeEachWrapped } from "../../../integration/helpers/utils"; import { beforeEachWrapped } from "../../../integration/helpers/utils";
import { ThemeStore } from "../../renderer/theme.store"; import { ThemeStore } from "../../renderer/theme.store";
import type { ClusterStoreModel } from "../cluster-store";
console = new Console(stdout, stderr); console = new Console(stdout, stderr);
describe("user store tests", () => { describe("user store tests", () => {
describe("for an empty config", () => { describe("for an empty config", () => {
beforeEachWrapped(() => { beforeEachWrapped(() => {
UserStore.resetInstance();
mockFs({ tmp: { "config.json": "{}", "kube_config": "{}" } }); mockFs({ tmp: { "config.json": "{}", "kube_config": "{}" } });
(UserStore.createInstance() as any).refreshNewContexts = jest.fn(() => Promise.resolve()); (UserStore.createInstance() as any).refreshNewContexts = jest.fn(() => Promise.resolve());
UserStore.getInstance();
}); });
afterEach(() => { afterEach(() => {
mockFs.restore(); mockFs.restore();
UserStore.resetInstance();
}); });
it("allows setting and retrieving lastSeenAppVersion", () => { it("allows setting and retrieving lastSeenAppVersion", () => {
@ -99,14 +95,31 @@ describe("user store tests", () => {
describe("migrations", () => { describe("migrations", () => {
beforeEachWrapped(() => { beforeEachWrapped(() => {
UserStore.resetInstance();
mockFs({ mockFs({
"tmp": { "tmp": {
"config.json": JSON.stringify({ "config.json": JSON.stringify({
user: { username: "foobar" }, user: { username: "foobar" },
preferences: { colorTheme: "light" }, preferences: { colorTheme: "light" },
lastSeenAppVersion: "1.2.3" lastSeenAppVersion: "1.2.3"
}) }),
"lens-cluster-store.json": JSON.stringify({
clusters: [
{
id: "foobar",
kubeConfigPath: "tmp/extension_data/foo/bar",
},
{
id: "barfoo",
kubeConfigPath: "some/other/path",
},
]
} as ClusterStoreModel),
"extension_data": {},
},
"some": {
"other": {
"path": "is file",
}
} }
}); });
@ -114,6 +127,7 @@ describe("user store tests", () => {
}); });
afterEach(() => { afterEach(() => {
UserStore.resetInstance();
mockFs.restore(); mockFs.restore();
}); });
@ -122,5 +136,12 @@ describe("user store tests", () => {
expect(us.lastSeenAppVersion).toBe("0.0.0"); expect(us.lastSeenAppVersion).toBe("0.0.0");
}); });
it.only("skips clusters for adding to kube-sync with files under extension_data/", () => {
const us = UserStore.getInstance();
expect(us.syncKubeconfigEntries.has("tmp/extension_data/foo/bar")).toBe(false);
expect(us.syncKubeconfigEntries.has("some/other/path")).toBe(true);
});
}); });
}); });

View File

@ -20,7 +20,7 @@
*/ */
import { app } from "electron"; import { app } from "electron";
import { existsSync, readJsonSync } from "fs-extra"; import { existsSync, readFileSync } from "fs";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
import { ClusterStore, ClusterStoreModel } from "../../common/cluster-store"; import { ClusterStore, ClusterStoreModel } from "../../common/cluster-store";
@ -32,9 +32,11 @@ export default {
run(store) { run(store) {
try { try {
const { syncKubeconfigEntries = [], ...preferences }: UserPreferencesModel = store.get("preferences") ?? {}; const { syncKubeconfigEntries = [], ...preferences }: UserPreferencesModel = store.get("preferences") ?? {};
const { clusters = [] }: ClusterStoreModel = readJsonSync(path.resolve(app.getPath("userData"), "lens-cluster-store.json")) ?? {}; const { clusters = [] }: ClusterStoreModel = JSON.parse(readFileSync(path.resolve(app.getPath("userData"), "lens-cluster-store.json"), "utf-8")) ?? {};
const extensionDataDir = path.resolve(app.getPath("userData"), "extension_data");
const syncPaths = new Set(syncKubeconfigEntries.map(s => s.filePath)); const syncPaths = new Set(syncKubeconfigEntries.map(s => s.filePath));
console.log(extensionDataDir);
syncPaths.add(path.join(os.homedir(), ".kube")); syncPaths.add(path.join(os.homedir(), ".kube"));
for (const cluster of clusters) { for (const cluster of clusters) {
@ -50,6 +52,13 @@ export default {
continue; continue;
} }
const relativeToExtensionData = path.relative(cluster.kubeConfigPath, extensionDataDir);
if (relativeToExtensionData === "" || relativeToExtensionData.match(/^(\.\.)([\\\/]\.\.)*$/)) {
migrationLog(`Skipping ${cluster.id} because kubeConfigPath is placed under an extension_data folder`);
continue;
}
if (!existsSync(cluster.kubeConfigPath)) { if (!existsSync(cluster.kubeConfigPath)) {
migrationLog(`Skipping ${cluster.id} because kubeConfigPath no longer exists`); migrationLog(`Skipping ${cluster.id} because kubeConfigPath no longer exists`);
continue; continue;
@ -64,8 +73,6 @@ export default {
migrationLog("Final list of synced paths", updatedSyncEntries); migrationLog("Final list of synced paths", updatedSyncEntries);
store.set("preferences", { ...preferences, syncKubeconfigEntries: updatedSyncEntries }); store.set("preferences", { ...preferences, syncKubeconfigEntries: updatedSyncEntries });
} catch (error) { } catch (error) {
console.log(error);
if (error.code !== "ENOENT") { if (error.code !== "ENOENT") {
// ignore files being missing // ignore files being missing
throw error; throw error;