diff --git a/src/common/cluster-meta-manager.ts b/src/common/cluster-meta-manager.ts index 3796edd62f..137e7add89 100644 --- a/src/common/cluster-meta-manager.ts +++ b/src/common/cluster-meta-manager.ts @@ -3,6 +3,8 @@ import { clusterMetaStore } from "./cluster-meta-store"; import { ClusterId } from "./cluster-store"; import Singleton from "./utils/singleton"; +export class StopError extends Error { } + export abstract class ClusterMetaCollector { /** * start tells the collector to start collecting its metadata once. diff --git a/src/common/meta-collectors/__tests__/distribution.test.ts b/src/common/meta-collectors/__tests__/distribution.test.ts new file mode 100644 index 0000000000..a537ee5251 --- /dev/null +++ b/src/common/meta-collectors/__tests__/distribution.test.ts @@ -0,0 +1 @@ +import { Distribution } from "../distribution" diff --git a/src/common/meta-collectors/distribution.ts b/src/common/meta-collectors/distribution.ts index fbfb488fb7..e09c8eb81e 100644 --- a/src/common/meta-collectors/distribution.ts +++ b/src/common/meta-collectors/distribution.ts @@ -1,16 +1,76 @@ -import { ClusterMetaCollector } from "../cluster-meta-manager"; -import { ClusterId } from "../cluster-store"; +import { observable, when } from "mobx"; +import requestPromise from "request-promise-native"; +import { ClusterMetaCollector, StopError } from "../cluster-meta-manager"; +import { ClusterId, clusterStore } from "../cluster-store"; export class Distribution extends ClusterMetaCollector { - constructor(protected clusterId: ClusterId, protected onSuccess: (result: any) => void, protected onError: (err: string) => void) { + private isCollecting = false + + shouldStop = when(() => this.isStopping); + @observable isStopping = false + + + constructor( + protected clusterId: ClusterId, + protected onSuccess: (result: any) => void, + protected onError: (err: string) => void, + ) { super() } - start(): void { - // TODO + promStopEarly(prom: Promise): Promise { + return Promise.race([ + prom, + this.shouldStop.then(() => { throw new StopError() }), + ]) + } + + // private detectDistribution() { + // if (kubernetesVersion.includes("gke")) return "gke" + // if (kubernetesVersion.includes("eks")) return "eks" + // if (kubernetesVersion.includes("IKS")) return "iks" + // if (this.apiUrl.endsWith("azmk8s.io")) return "aks" + // if (this.apiUrl.endsWith("k8s.ondigitalocean.com")) return "digitalocean" + // if (this.contextName.startsWith("minikube")) return "minikube" + // if (kubernetesVersion.includes("+")) return "custom" + // return "vanilla" + // } + + async start(): Promise { + if (this.isCollecting) { + return + } + + try { + this.isCollecting = true + const cluster = clusterStore.getById(this.clusterId) + const url = cluster.kubeProxyUrl + "/version" + + const res = await this.promStopEarly(requestPromise(url, { + json: true, + timeout: 30000, + headers: { + Host: `${this.clusterId}.${(new URL(cluster.kubeProxyUrl)).host}` + } + })) + + } catch (err) { + if (err instanceof StopError) { + return + } + + this.onError(String(err)) + } finally { + this.isStopping = false + this.isCollecting = false + } } stop(): void { - // TODO + if (!this.isCollecting) { + return + } + + this.isStopping = true } } diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 71c55395b1..ffb8c904ef 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -51,12 +51,12 @@ export class Cluster implements ClusterModel { whenInitialized = when(() => this.initialized); whenReady = when(() => this.ready); + public port: number; @observable initialized = false; @observable contextName: string; @observable workspace: WorkspaceId; @observable kubeConfigPath: string; @observable apiUrl: string; // cluster server url - @observable kubeProxyUrl: string; // lens-proxy to kube-api url @observable online = false; @observable accessible = false; @observable ready = false; @@ -90,12 +90,16 @@ export class Cluster implements ClusterModel { Object.assign(this, model); } + get kubeProxyUrl() { + return `http://localhost:${this.port}${apiKubePrefix}` + } + @action async init(port: number) { try { this.contextHandler = new ContextHandler(this); this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler, port); - this.kubeProxyUrl = `http://localhost:${port}${apiKubePrefix}`; + this.port = port this.initialized = true; logger.info(`[CLUSTER]: "${this.contextName}" init success`, { id: this.id, @@ -262,6 +266,7 @@ export class Cluster implements ClusterModel { protected async getConnectionStatus(): Promise { try { const response = await this.k8sRequest("/version") + console.log(response) this.version = response.gitVersion this.failureReason = null return ClusterStatus.AccessGranted;