Add the ability to sync kube config files (#2567)

* Add the ability to sync kube config files

- Will update when the files changes

- add KUBECONFIG_SYNC label

- fix rebase and change to addObservableSource

- move UI to user settings

- support shallow folder watching

- add some unit tests for the diff-er

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* responding to review comments

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix tests and add try/catch

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* always sync c&p folder, remove bad rebase

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix preferences

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix settings saving and catalog view

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix synced clusters not connectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* change to non-complete shallow watching

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix sizing

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Catch readStream errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* don't clear UserStore on non-existant preference field, fix unlinking not removing items from source

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* change label to file

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-04-30 16:48:20 +03:00
1 parent f06d330835
commit 998f7aa934
32 files changed
+1215 -236

No files matched your search

@@ -0,0 +1,252 @@
import { ObservableMap } from "mobx";
import { CatalogEntity } from "../../../common/catalog";
import { loadFromOptions } from "../../../common/kube-helpers";
import { Cluster } from "../../cluster";
import { computeDiff, configToModels } from "../kubeconfig-sync";
import mockFs from "mock-fs";
import fs from "fs";
describe("kubeconfig-sync.source tests", () => {
beforeEach(() => {
mockFs();
});
afterEach(() => {
mockFs.restore();
});
describe("configsToModels", () => {
it("should filter out invalid split configs", () => {
const config = loadFromOptions({
clusters: [],
users: [],
contexts: [],
currentContext: "foobar"
});
expect(configToModels(config, "").length).toBe(0);
});
it("should keep a single valid split config", () => {
const config = loadFromOptions({
clusters: [{
name: "cluster-name",
server: "1.2.3.4",
skipTLSVerify: false,
}],
users: [{
name: "user-name",
}],
contexts: [{
cluster: "cluster-name",
name: "context-name",
user: "user-name",
}],
currentContext: "foobar"
});
const models = configToModels(config, "/bar");
expect(models.length).toBe(1);
expect(models[0].contextName).toBe("context-name");
expect(models[0].kubeConfigPath).toBe("/bar");
});
});
describe("computeDiff", () => {
it("should leave an empty source empty if there are no entries", () => {
const contents = "";
const rootSource = new ObservableMap<string, [Cluster, CatalogEntity]>();
const port = 0;
const filePath = "/bar";
computeDiff(contents, rootSource, port, filePath);
expect(rootSource.size).toBe(0);
});
it("should add only the valid clusters to the source", () => {
const contents = JSON.stringify({
clusters: [{
name: "cluster-name",
cluster: {
server: "1.2.3.4",
},
skipTLSVerify: false,
}],
users: [{
name: "user-name",
}],
contexts: [{
name: "context-name",
context: {
cluster: "cluster-name",
user: "user-name",
},
}, {
name: "context-the-second",
context: {
cluster: "missing-cluster",
user: "user-name",
},
}],
currentContext: "foobar"
});
const rootSource = new ObservableMap<string, [Cluster, CatalogEntity]>();
const port = 0;
const filePath = "/bar";
fs.writeFileSync(filePath, contents);
computeDiff(contents, rootSource, port, filePath);
expect(rootSource.size).toBe(1);
const c = rootSource.values().next().value[0] as Cluster;
expect(c.kubeConfigPath).toBe("/bar");
expect(c.contextName).toBe("context-name");
});
it("should remove a cluster when it is removed from the contents", () => {
const contents = JSON.stringify({
clusters: [{
name: "cluster-name",
cluster: {
server: "1.2.3.4",
},
skipTLSVerify: false,
}],
users: [{
name: "user-name",
}],
contexts: [{
name: "context-name",
context: {
cluster: "cluster-name",
user: "user-name",
},
}, {
name: "context-the-second",
context: {
cluster: "missing-cluster",
user: "user-name",
},
}],
currentContext: "foobar"
});
const rootSource = new ObservableMap<string, [Cluster, CatalogEntity]>();
const port = 0;
const filePath = "/bar";
fs.writeFileSync(filePath, contents);
computeDiff(contents, rootSource, port, filePath);
expect(rootSource.size).toBe(1);
const c = rootSource.values().next().value[0] as Cluster;
expect(c.kubeConfigPath).toBe("/bar");
expect(c.contextName).toBe("context-name");
computeDiff("{}", rootSource, port, filePath);
expect(rootSource.size).toBe(0);
});
it("should remove only the cluster that it is removed from the contents", () => {
const contents = JSON.stringify({
clusters: [{
name: "cluster-name",
cluster: {
server: "1.2.3.4",
},
skipTLSVerify: false,
}],
users: [{
name: "user-name",
}, {
name: "user-name-2",
}],
contexts: [{
name: "context-name",
context: {
cluster: "cluster-name",
user: "user-name",
},
}, {
name: "context-name-2",
context: {
cluster: "cluster-name",
user: "user-name-2",
},
}, {
name: "context-the-second",
context: {
cluster: "missing-cluster",
user: "user-name",
},
}],
currentContext: "foobar"
});
const rootSource = new ObservableMap<string, [Cluster, CatalogEntity]>();
const port = 0;
const filePath = "/bar";
fs.writeFileSync(filePath, contents);
computeDiff(contents, rootSource, port, filePath);
expect(rootSource.size).toBe(2);
{
const c = rootSource.values().next().value[0] as Cluster;
expect(c.kubeConfigPath).toBe("/bar");
expect(["context-name", "context-name-2"].includes(c.contextName)).toBe(true);
}
const newContents = JSON.stringify({
clusters: [{
name: "cluster-name",
cluster: {
server: "1.2.3.4",
},
skipTLSVerify: false,
}],
users: [{
name: "user-name",
}, {
name: "user-name-2",
}],
contexts: [{
name: "context-name",
context: {
cluster: "cluster-name",
user: "user-name",
},
}, {
name: "context-the-second",
context: {
cluster: "missing-cluster",
user: "user-name",
},
}],
currentContext: "foobar"
});
computeDiff(newContents, rootSource, port, filePath);
expect(rootSource.size).toBe(1);
{
const c = rootSource.values().next().value[0] as Cluster;
expect(c.kubeConfigPath).toBe("/bar");
expect(c.contextName).toBe("context-name");
}
});
});
});