diff --git a/src/main/cluster-detectors/detector-registry.ts b/src/main/cluster-detectors/detector-registry.ts index 40b4fa6949..5d3461b0bb 100644 --- a/src/main/cluster-detectors/detector-registry.ts +++ b/src/main/cluster-detectors/detector-registry.ts @@ -21,19 +21,17 @@ import { observable } from "mobx"; import type { ClusterMetadata } from "../../common/cluster-types"; +import { Singleton } from "../../common/utils"; import type { Cluster } from "../cluster"; import type { BaseClusterDetector, ClusterDetectionResult } from "./base-cluster-detector"; -import { ClusterIdDetector } from "./cluster-id-detector"; -import { DistributionDetector } from "./distribution-detector"; -import { LastSeenDetector } from "./last-seen-detector"; -import { NodesCountDetector } from "./nodes-count-detector"; -import { VersionDetector } from "./version-detector"; -export class DetectorRegistry { +export class DetectorRegistry extends Singleton { registry = observable.array([], { deep: false }); - add(detectorClass: typeof BaseClusterDetector) { + add(detectorClass: typeof BaseClusterDetector): this { this.registry.push(detectorClass); + + return this; } async detectForCluster(cluster: Cluster): Promise { @@ -63,10 +61,3 @@ export class DetectorRegistry { return metadata; } } - -export const detectorRegistry = new DetectorRegistry(); -detectorRegistry.add(ClusterIdDetector); -detectorRegistry.add(LastSeenDetector); -detectorRegistry.add(VersionDetector); -detectorRegistry.add(DistributionDetector); -detectorRegistry.add(NodesCountDetector); diff --git a/src/main/cluster.ts b/src/main/cluster.ts index bd09c1fad4..b3eef9613f 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -30,7 +30,7 @@ import { loadConfigFromFile, loadConfigFromFileSync, validateKubeConfig } from " import { apiResourceRecord, apiResources, KubeApiResource, KubeResource } from "../common/rbac"; import logger from "./logger"; import { VersionDetector } from "./cluster-detectors/version-detector"; -import { detectorRegistry } from "./cluster-detectors/detector-registry"; +import { DetectorRegistry } from "./cluster-detectors/detector-registry"; import plimit from "p-limit"; import { toJS } from "../common/utils"; import { initialNodeShellImage, ClusterState, ClusterMetadataKey, ClusterRefreshOptions, ClusterStatus, ClusterMetricsResourceType, ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences, UpdateClusterModel } from "../common/cluster-types"; @@ -404,7 +404,7 @@ export class Cluster implements ClusterModel, ClusterState { @action async refreshMetadata() { logger.info(`[CLUSTER]: refreshMetadata`, this.getMeta()); - const metadata = await detectorRegistry.detectForCluster(this); + const metadata = await DetectorRegistry.getInstance().detectForCluster(this); const existingMetadata = this.metadata; this.metadata = Object.assign(existingMetadata, metadata); diff --git a/src/main/index.ts b/src/main/index.ts index 66e336dedc..2e7ae172fd 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -62,8 +62,6 @@ import { FilesystemProvisionerStore } from "./extension-filesystem"; import { SentryInit } from "../common/sentry"; import { ensureDir } from "fs-extra"; -// This has to be called before start using winton-based logger -// For example, before any logger.log SentryInit(); const workingDir = path.join(app.getPath("appData"), appName); @@ -168,6 +166,8 @@ app.on("ready", async () => { ClusterManager.createInstance().init(); KubeconfigSyncManager.createInstance(); + initializers.initClusterMetadataDetectors(); + try { logger.info("🔌 Starting LensProxy"); await lensProxy.listen(); diff --git a/src/main/initializers/cluster-metadata-detectors.ts b/src/main/initializers/cluster-metadata-detectors.ts new file mode 100644 index 0000000000..47942cd28e --- /dev/null +++ b/src/main/initializers/cluster-metadata-detectors.ts @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import { ClusterIdDetector } from "../cluster-detectors/cluster-id-detector"; +import { DetectorRegistry } from "../cluster-detectors/detector-registry"; +import { DistributionDetector } from "../cluster-detectors/distribution-detector"; +import { LastSeenDetector } from "../cluster-detectors/last-seen-detector"; +import { NodesCountDetector } from "../cluster-detectors/nodes-count-detector"; +import { VersionDetector } from "../cluster-detectors/version-detector"; + +export function initClusterMetadataDetectors() { + DetectorRegistry.createInstance() + .add(ClusterIdDetector) + .add(LastSeenDetector) + .add(VersionDetector) + .add(DistributionDetector) + .add(NodesCountDetector); +} diff --git a/src/main/initializers/index.ts b/src/main/initializers/index.ts index 526b61d4ed..34a5c32e34 100644 --- a/src/main/initializers/index.ts +++ b/src/main/initializers/index.ts @@ -22,3 +22,4 @@ export * from "./registries"; export * from "./metrics-providers"; export * from "./ipc"; +export * from "./cluster-metadata-detectors";