mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make KubeconfigSyncManager more injectable
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
6027adaa71
commit
4dd3f9d3fd
@ -7,7 +7,7 @@ import { ObservableMap } from "mobx";
|
|||||||
import type { CatalogEntity } from "../../../common/catalog";
|
import type { CatalogEntity } from "../../../common/catalog";
|
||||||
import { loadFromOptions } from "../../../common/kube-helpers";
|
import { loadFromOptions } from "../../../common/kube-helpers";
|
||||||
import type { Cluster } from "../../../common/cluster/cluster";
|
import type { Cluster } from "../../../common/cluster/cluster";
|
||||||
import { computeDiff as computeDiffFor, configToModels } from "../kubeconfig-sync-manager/kubeconfig-sync-manager";
|
import { computeDiff as computeDiffFor, configToModels } from "../kubeconfig-sync/manager";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { ClusterManager } from "../../cluster-manager";
|
import { ClusterManager } from "../../cluster-manager";
|
||||||
|
|||||||
@ -4,8 +4,9 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import directoryForKubeConfigsInjectable from "../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
import directoryForKubeConfigsInjectable from "../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
||||||
import { KubeconfigSyncManager } from "./kubeconfig-sync-manager";
|
import { KubeconfigSyncManager } from "./manager";
|
||||||
import { createClusterInjectionToken } from "../../../common/cluster/create-cluster-injection-token";
|
import { createClusterInjectionToken } from "../../../common/cluster/create-cluster-injection-token";
|
||||||
|
import catalogEntityRegistryInjectable from "../../catalog/entity-registry.injectable";
|
||||||
|
|
||||||
const kubeconfigSyncManagerInjectable = getInjectable({
|
const kubeconfigSyncManagerInjectable = getInjectable({
|
||||||
id: "kubeconfig-sync-manager",
|
id: "kubeconfig-sync-manager",
|
||||||
@ -13,6 +14,7 @@ const kubeconfigSyncManagerInjectable = getInjectable({
|
|||||||
instantiate: (di) => new KubeconfigSyncManager({
|
instantiate: (di) => new KubeconfigSyncManager({
|
||||||
directoryForKubeConfigs: di.inject(directoryForKubeConfigsInjectable),
|
directoryForKubeConfigs: di.inject(directoryForKubeConfigsInjectable),
|
||||||
createCluster: di.inject(createClusterInjectionToken),
|
createCluster: di.inject(createClusterInjectionToken),
|
||||||
|
entityRegistry: di.inject(catalogEntityRegistryInjectable),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -6,7 +6,6 @@
|
|||||||
import type { IComputedValue, ObservableMap } from "mobx";
|
import type { IComputedValue, ObservableMap } from "mobx";
|
||||||
import { action, observable, computed, runInAction, makeObservable, observe } from "mobx";
|
import { action, observable, computed, runInAction, makeObservable, observe } from "mobx";
|
||||||
import type { CatalogEntity } from "../../../common/catalog";
|
import type { CatalogEntity } from "../../../common/catalog";
|
||||||
import { catalogEntityRegistry } from "../../catalog";
|
|
||||||
import type { FSWatcher } from "chokidar";
|
import type { FSWatcher } from "chokidar";
|
||||||
import { watch } from "chokidar";
|
import { watch } from "chokidar";
|
||||||
import type { Stats } from "fs";
|
import type { Stats } from "fs";
|
||||||
@ -27,6 +26,7 @@ import globToRegExp from "glob-to-regexp";
|
|||||||
import { inspect } from "util";
|
import { inspect } from "util";
|
||||||
import type { ClusterModel, UpdateClusterModel } from "../../../common/cluster-types";
|
import type { ClusterModel, UpdateClusterModel } from "../../../common/cluster-types";
|
||||||
import type { Cluster } from "../../../common/cluster/cluster";
|
import type { Cluster } from "../../../common/cluster/cluster";
|
||||||
|
import type { CatalogEntityRegistry } from "../../catalog/entity-registry";
|
||||||
|
|
||||||
const logPrefix = "[KUBECONFIG-SYNC]:";
|
const logPrefix = "[KUBECONFIG-SYNC]:";
|
||||||
|
|
||||||
@ -52,18 +52,19 @@ const folderSyncMaxAllowedFileReadSize = 2 * 1024 * 1024; // 2 MiB
|
|||||||
const fileSyncMaxAllowedFileReadSize = 16 * folderSyncMaxAllowedFileReadSize; // 32 MiB
|
const fileSyncMaxAllowedFileReadSize = 16 * folderSyncMaxAllowedFileReadSize; // 32 MiB
|
||||||
|
|
||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
directoryForKubeConfigs: string;
|
readonly directoryForKubeConfigs: string;
|
||||||
|
readonly entityRegistry: CatalogEntityRegistry;
|
||||||
createCluster: (model: ClusterModel) => Cluster;
|
createCluster: (model: ClusterModel) => Cluster;
|
||||||
}
|
}
|
||||||
|
|
||||||
const kubeConfigSyncName = "lens:kube-sync";
|
const kubeConfigSyncName = "lens:kube-sync";
|
||||||
|
|
||||||
export class KubeconfigSyncManager {
|
export class KubeconfigSyncManager {
|
||||||
protected sources = observable.map<string, [IComputedValue<CatalogEntity[]>, Disposer]>();
|
protected readonly sources = observable.map<string, [IComputedValue<CatalogEntity[]>, Disposer]>();
|
||||||
protected syncing = false;
|
protected syncing = false;
|
||||||
protected syncListDisposer?: Disposer;
|
protected syncListDisposer?: Disposer;
|
||||||
|
|
||||||
constructor(private dependencies: Dependencies) {
|
constructor(protected readonly dependencies: Dependencies) {
|
||||||
makeObservable(this);
|
makeObservable(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ export class KubeconfigSyncManager {
|
|||||||
|
|
||||||
logger.info(`${logPrefix} starting requested syncs`);
|
logger.info(`${logPrefix} starting requested syncs`);
|
||||||
|
|
||||||
catalogEntityRegistry.addComputedSource(kubeConfigSyncName, computed(() => (
|
this.dependencies.entityRegistry.addComputedSource(kubeConfigSyncName, computed(() => (
|
||||||
Array.from(iter.flatMap(
|
Array.from(iter.flatMap(
|
||||||
this.sources.values(),
|
this.sources.values(),
|
||||||
([entities]) => entities.get(),
|
([entities]) => entities.get(),
|
||||||
@ -111,7 +112,7 @@ export class KubeconfigSyncManager {
|
|||||||
this.stopOldSync(filePath);
|
this.stopOldSync(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
catalogEntityRegistry.removeSource(kubeConfigSyncName);
|
this.dependencies.entityRegistry.removeSource(kubeConfigSyncName);
|
||||||
this.syncing = false;
|
this.syncing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +166,7 @@ type RootSourceValue = [Cluster, CatalogEntity];
|
|||||||
type RootSource = ObservableMap<string, RootSourceValue>;
|
type RootSource = ObservableMap<string, RootSourceValue>;
|
||||||
|
|
||||||
// exported for testing
|
// exported for testing
|
||||||
export const computeDiff = ({ directoryForKubeConfigs, createCluster }: Dependencies) => (contents: string, source: RootSource, filePath: string): void => {
|
export const computeDiff = ({ directoryForKubeConfigs, createCluster }: Pick<Dependencies, "createCluster" | "directoryForKubeConfigs">) => (contents: string, source: RootSource, filePath: string): void => {
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
try {
|
try {
|
||||||
const { config, error } = loadConfigFromString(contents);
|
const { config, error } = loadConfigFromString(contents);
|
||||||
@ -48,7 +48,7 @@ import extensionDiscoveryInjectable from "../extensions/extension-discovery/exte
|
|||||||
import directoryForExesInjectable from "../common/app-paths/directory-for-exes/directory-for-exes.injectable";
|
import directoryForExesInjectable from "../common/app-paths/directory-for-exes/directory-for-exes.injectable";
|
||||||
import initIpcMainHandlersInjectable from "./initializers/init-ipc-main-handlers/init-ipc-main-handlers.injectable";
|
import initIpcMainHandlersInjectable from "./initializers/init-ipc-main-handlers/init-ipc-main-handlers.injectable";
|
||||||
import directoryForKubeConfigsInjectable from "../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
import directoryForKubeConfigsInjectable from "../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
||||||
import kubeconfigSyncManagerInjectable from "./catalog-sources/kubeconfig-sync-manager/kubeconfig-sync-manager.injectable";
|
import kubeconfigSyncManagerInjectable from "./catalog-sources/kubeconfig-sync/manager.injectable";
|
||||||
import clusterStoreInjectable from "../common/cluster-store/cluster-store.injectable";
|
import clusterStoreInjectable from "../common/cluster-store/cluster-store.injectable";
|
||||||
import routerInjectable from "./router/router.injectable";
|
import routerInjectable from "./router/router.injectable";
|
||||||
import shellApiRequestInjectable from "./proxy-functions/shell-api-request/shell-api-request.injectable";
|
import shellApiRequestInjectable from "./proxy-functions/shell-api-request/shell-api-request.injectable";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user