Skip to content

Commit

Permalink
Ban circular dependencies
Browse files Browse the repository at this point in the history
- ApiManager now creates the instances of all stores

- ReleaseStore is Singleton-like

- Move most types and helper functions into seperate files

Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed May 19, 2021
1 parent 492fa6f commit 33422ce
Show file tree
Hide file tree
Showing 232 changed files with 2,535 additions and 1,631 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Expand Up @@ -33,12 +33,22 @@ module.exports = {
}
},
overrides: [
{
files: [
"extensions/**/*.ts",
"extensions/**/*.tsx",
],
rules: {
"import/no-unresolved": "off", // warns on @k8slens/extensions
}
},
{
files: [
"**/*.js"
],
extends: [
"eslint:recommended",
"plugin:import/recommended",
],
env: {
node: true
Expand All @@ -53,6 +63,7 @@ module.exports = {
],
rules: {
"header/header": [2, "./license-header"],
// "import/no-cycle": 2,
"indent": ["error", 2, {
"SwitchCase": 1,
}],
Expand Down Expand Up @@ -93,6 +104,8 @@ module.exports = {
parser: "@typescript-eslint/parser",
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
],
plugins: [
"header",
Expand All @@ -104,6 +117,7 @@ module.exports = {
},
rules: {
"header/header": [2, "./license-header"],
// "import/no-cycle": 2,
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": ["error"],
"@typescript-eslint/explicit-function-return-type": "off",
Expand Down Expand Up @@ -158,6 +172,8 @@ module.exports = {
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
],
parserOptions: {
ecmaVersion: 2018,
Expand All @@ -166,6 +182,7 @@ module.exports = {
},
rules: {
"header/header": [2, "./license-header"],
// "import/no-cycle": 2,
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": ["error"],
"@typescript-eslint/explicit-function-return-type": "off",
Expand Down
6 changes: 3 additions & 3 deletions extensions/kube-object-event-status/src/resolver.tsx
Expand Up @@ -22,7 +22,7 @@
import { K8sApi } from "@k8slens/extensions";

export function resolveStatus(object: K8sApi.KubeObject): K8sApi.KubeObjectStatus {
const eventStore = K8sApi.apiManager.getStore(K8sApi.eventApi);
const eventStore = K8sApi.ApiManager.getInstance().getStore(K8sApi.eventApi);
const events = (eventStore as K8sApi.EventStore).getEventsByObject(object);
const warnings = events.filter(evt => evt.isWarning());

Expand All @@ -42,7 +42,7 @@ export function resolveStatusForPods(pod: K8sApi.Pod): K8sApi.KubeObjectStatus {
if (!pod.hasIssues()) {
return null;
}
const eventStore = K8sApi.apiManager.getStore(K8sApi.eventApi);
const eventStore = K8sApi.ApiManager.getInstance().getStore(K8sApi.eventApi);
const events = (eventStore as K8sApi.EventStore).getEventsByObject(pod);
const warnings = events.filter(evt => evt.isWarning());

Expand All @@ -59,7 +59,7 @@ export function resolveStatusForPods(pod: K8sApi.Pod): K8sApi.KubeObjectStatus {
}

export function resolveStatusForCronJobs(cronJob: K8sApi.CronJob): K8sApi.KubeObjectStatus {
const eventStore = K8sApi.apiManager.getStore(K8sApi.eventApi);
const eventStore = K8sApi.ApiManager.getInstance().getStore(K8sApi.eventApi);
let events = (eventStore as K8sApi.EventStore).getEventsByObject(cronJob);
const warnings = events.filter(evt => evt.isWarning());

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -315,6 +315,7 @@
"electron-notarize": "^0.3.0",
"eslint": "^7.7.0",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.23.2",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-unused-imports": "^1.0.1",
"file-loader": "^6.0.0",
Expand Down
Empty file removed src/common/.gitkeep
Empty file.
12 changes: 7 additions & 5 deletions src/common/__tests__/cluster-store.test.ts
Expand Up @@ -23,9 +23,11 @@ import fs from "fs";
import mockFs from "mock-fs";
import yaml from "js-yaml";
import { Cluster } from "../../main/cluster";
import { ClusterStore, getClusterIdFromHost } from "../cluster-store";
import { ClusterStore } from "../cluster-store";
import { Console } from "console";
import { stdout, stderr } from "process";
import { embedCustomKubeConfig } from "../utils";
import { getClusterIdFromHost } from "../cluster-types";

console = new Console(stdout, stderr);

Expand Down Expand Up @@ -102,7 +104,7 @@ describe("empty config", () => {
icon: "data:image/jpeg;base64, iVBORw0KGgoAAAANSUhEUgAAA1wAAAKoCAYAAABjkf5",
clusterName: "minikube"
},
kubeConfigPath: ClusterStore.embedCustomKubeConfig("foo", kubeconfig)
kubeConfigPath: embedCustomKubeConfig("foo", kubeconfig)
})
);
});
Expand Down Expand Up @@ -135,15 +137,15 @@ describe("empty config", () => {
preferences: {
clusterName: "prod"
},
kubeConfigPath: ClusterStore.embedCustomKubeConfig("prod", kubeconfig)
kubeConfigPath: embedCustomKubeConfig("prod", kubeconfig)
}),
new Cluster({
id: "dev",
contextName: "foo2",
preferences: {
clusterName: "dev"
},
kubeConfigPath: ClusterStore.embedCustomKubeConfig("dev", kubeconfig)
kubeConfigPath: embedCustomKubeConfig("dev", kubeconfig)
})
);
});
Expand All @@ -154,7 +156,7 @@ describe("empty config", () => {
});

it("check if cluster's kubeconfig file saved", () => {
const file = ClusterStore.embedCustomKubeConfig("boo", "kubeconfig");
const file = embedCustomKubeConfig("boo", "kubeconfig");

expect(fs.readFileSync(file, "utf8")).toBe("kubeconfig");
});
Expand Down
3 changes: 1 addition & 2 deletions src/common/base-store.ts
Expand Up @@ -24,8 +24,7 @@ import Config from "conf";
import type { Options as ConfOptions } from "conf/dist/source/types";
import { app, ipcMain, ipcRenderer, remote } from "electron";
import { IReactionOptions, observable, reaction, runInAction, when } from "mobx";
import Singleton from "./utils/singleton";
import { getAppVersion } from "./utils/app-version";
import { Singleton, getAppVersion } from "./utils";
import logger from "../main/logger";
import { broadcastMessage, ipcMainOn, ipcRendererOn } from "./ipc";
import isEqual from "lodash/isEqual";
Expand Down
6 changes: 3 additions & 3 deletions src/common/catalog-entities/kubernetes-cluster.ts
Expand Up @@ -20,13 +20,13 @@
*/

import { catalogCategoryRegistry } from "../catalog/catalog-category-registry";
import { CatalogEntity, CatalogEntityActionContext, CatalogEntityAddMenuContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog";
import { CatalogEntity, CatalogEntityActionContext, CatalogEntityAddMenuContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus, CatalogCategory, CatalogCategorySpec } from "../catalog";
import { clusterActivateHandler, clusterDisconnectHandler } from "../cluster-ipc";
import { ClusterStore } from "../cluster-store";
import { requestMain } from "../ipc";
import { productName } from "../vars";
import { CatalogCategory, CatalogCategorySpec } from "../catalog";
import { addClusterURL } from "../routes";
import { storedKubeConfigFolder } from "../utils";
import { app } from "electron";

export type KubernetesClusterPrometheusMetrics = {
Expand Down Expand Up @@ -109,7 +109,7 @@ export class KubernetesCluster extends CatalogEntity<CatalogEntityMetadata, Kube
},
];

if (this.metadata.labels["file"]?.startsWith(ClusterStore.storedKubeConfigFolder)) {
if (this.metadata.labels["file"]?.startsWith(storedKubeConfigFolder())) {
context.menuItems.push({
title: "Delete",
onlyVisibleForSource: "local",
Expand Down
77 changes: 0 additions & 77 deletions src/common/cluster-ipc.ts
Expand Up @@ -19,86 +19,9 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { ClusterId, ClusterStore } from "./cluster-store";
import { appEventBus } from "./event-bus";
import { ResourceApplier } from "../main/resource-applier";
import { ipcMain, IpcMainInvokeEvent } from "electron";
import { clusterFrameMap } from "./cluster-frames";

export const clusterActivateHandler = "cluster:activate";
export const clusterSetFrameIdHandler = "cluster:set-frame-id";
export const clusterRefreshHandler = "cluster:refresh";
export const clusterDisconnectHandler = "cluster:disconnect";
export const clusterKubectlApplyAllHandler = "cluster:kubectl-apply-all";
export const clusterKubectlDeleteAllHandler = "cluster:kubectl-delete-all";

if (ipcMain) {
ipcMain.handle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
return ClusterStore.getInstance()
.getById(clusterId)
?.activate(force);
});

ipcMain.handle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
const cluster = ClusterStore.getInstance().getById(clusterId);

if (cluster) {
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
cluster.pushState();
}
});

ipcMain.handle(clusterRefreshHandler, (event, clusterId: ClusterId) => {
return ClusterStore.getInstance()
.getById(clusterId)
?.refresh({ refreshMetadata: true });
});

ipcMain.handle(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
appEventBus.emit({name: "cluster", action: "stop"});
const cluster = ClusterStore.getInstance().getById(clusterId);

if (cluster) {
cluster.disconnect();
clusterFrameMap.delete(cluster.id);
}
});

ipcMain.handle(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-apply-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);

if (cluster) {
const applier = new ResourceApplier(cluster);

try {
const stdout = await applier.kubectlApplyAll(resources, extraArgs);

return { stdout };
} catch (error: any) {
return { stderr: error };
}
} else {
throw `${clusterId} is not a valid cluster id`;
}
});

ipcMain.handle(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-delete-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);

if (cluster) {
const applier = new ResourceApplier(cluster);

try {
const stdout = await applier.kubectlDeleteAll(resources, extraArgs);

return { stdout };
} catch (error: any) {
return { stderr: error };
}
} else {
throw `${clusterId} is not a valid cluster id`;
}
});
}

0 comments on commit 33422ce

Please sign in to comment.