From aab91863fcd14682944ad53a1873577a5e4a789f Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 12 Jul 2020 13:41:05 +0300 Subject: [PATCH] auto-refresh status only for active (selected) cluster Signed-off-by: Roman --- src/common/cluster-store.ts | 21 +++++++++++++------ src/common/ipc-messages.ts | 2 +- src/main/cluster-manager.ts | 13 ++++++++++-- src/main/cluster.ts | 20 +++++++----------- src/main/context-handler.ts | 1 - .../cluster-manager/cluster-context.tsx | 2 +- .../cluster-manager/clusters-menu.tsx | 4 ++-- 7 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index 93b73d5702..0a416bda4a 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -48,11 +48,15 @@ export class ClusterStore extends BaseStore { }); } - @observable activeCluster: ClusterId; // todo: current-context from kube-config? + @observable activeClusterId: ClusterId; @observable removedClusters = observable.map(); @observable clusters = observable.map(); - @computed get clustersList() { + @computed get activeCluster(): Cluster { + return this.clusters.get(this.activeClusterId); + } + + @computed get clustersList(): Cluster[] { return Array.from(this.clusters.values()); } @@ -74,8 +78,8 @@ export class ClusterStore extends BaseStore { @action removeById(clusterId: ClusterId): void { - if (this.activeCluster === clusterId) { - this.activeCluster = null; + if (this.activeClusterId === clusterId) { + this.activeClusterId = null; } this.clusters.delete(clusterId); } @@ -111,14 +115,19 @@ export class ClusterStore extends BaseStore { } }); - this.activeCluster = newClusters.has(activeCluster) ? activeCluster : null; + this.activeClusterId = newClusters.has(activeCluster) ? activeCluster : null; this.clusters.replace(newClusters); this.removedClusters.replace(removedClusters); + + // "auto-select" first cluster if not available or invalid from config file + if (!this.activeClusterId && newClusters.size) { + this.activeClusterId = Array.from(newClusters.values())[0].id; + } } toJSON(): ClusterStoreModel { return toJS({ - activeCluster: this.activeCluster, + activeCluster: this.activeClusterId, clusters: this.clustersList.map(cluster => cluster.toJSON()), }, { recurseEverything: true diff --git a/src/common/ipc-messages.ts b/src/common/ipc-messages.ts index 46eba200ae..2012abc1a2 100644 --- a/src/common/ipc-messages.ts +++ b/src/common/ipc-messages.ts @@ -5,7 +5,7 @@ export enum ClusterIpcMessage { CLUSTER_STOP = "cluster-stop", CLUSTER_REMOVE = "cluster-remove", CLUSTER_REMOVE_WORKSPACE = "cluster-remove-all-from-workspace", - CLUSTER_REFRESH = "cluster-refresh", // todo: remove, possibly won't needed + CLUSTER_REFRESH = "cluster-refresh", FEATURE_INSTALL = "cluster-feature-install", FEATURE_UPGRADE = "cluster-feature-upgrade", FEATURE_REMOVE = "cluster-feature-remove", diff --git a/src/main/cluster-manager.ts b/src/main/cluster-manager.ts index c38e3f7622..75e74ce454 100644 --- a/src/main/cluster-manager.ts +++ b/src/main/cluster-manager.ts @@ -39,6 +39,13 @@ export class ClusterManager { removedClusters.clear(); } }); + // auto-refresh status for active cluster + autorun(() => { + const { activeCluster } = clusterStore; + if (activeCluster && activeCluster.initialized) { + activeCluster.refreshStatus(); + } + }); // listen ipc-events ClusterManager.ipcListen(this); } @@ -87,7 +94,6 @@ export class ClusterManager { } } - // fixme getClusterForRequest(req: http.IncomingMessage): Cluster { let cluster: Cluster = null @@ -147,7 +153,10 @@ export class ClusterManager { } protected async refreshCluster(clusterId: ClusterId) { - await this.getCluster(clusterId)?.refreshStatus(); + const cluster = this.getCluster(clusterId); + if (cluster) { + await cluster.refreshStatus(); + } } static ipcListen(clusterManager: ClusterManager) { diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 6acb3f5270..9bce895cad 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -42,7 +42,6 @@ export class Cluster implements ClusterModel { @observable kubeConfigPath: string; @observable url: string; // cluster-api url @observable proxyUrl: string; // lens-proxy url - @observable kubeProxyUrl: string; @observable webContentUrl: string; @observable proxyPort: number; @observable online: boolean; @@ -60,32 +59,26 @@ export class Cluster implements ClusterModel { this.updateModel(model); } + @action updateModel(model: ClusterModel) { Object.assign(this, model); - if (!this.contextName) { - this.contextName = this.preferences.clusterName; - } + this.url = this.getKubeconfig().getCurrentCluster().server; + this.contextName = this.preferences.clusterName; } @action async init(proxyPort: number) { try { this.proxyPort = proxyPort; - this.url = this.getKubeconfig().getCurrentCluster().server; - this.proxyUrl = `http://localhost:${proxyPort}`; - this.kubeProxyUrl = this.proxyUrl + apiKubePrefix; - this.webContentUrl = `http://${this.id}.localhost:${proxyPort}`; - this.contextHandler = new ContextHandler(this); this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler); - await this.refreshStatus(); + this.proxyUrl = `http://localhost:${proxyPort}`; + this.webContentUrl = `http://${this.id}.localhost:${proxyPort}`; this.initialized = true; - logger.info(`[CLUSTER]: init success`, { id: this.id, url: this.url, proxyUrl: this.proxyUrl, - kubeProxyUrl: this.kubeProxyUrl, webContentUrl: this.webContentUrl, }); } catch (err) { @@ -150,7 +143,8 @@ export class Cluster implements ClusterModel { } protected k8sRequest(path: string, options: RequestPromiseOptions = {}) { - return request(this.kubeProxyUrl + path, { + const apiUrl = this.proxyUrl + apiKubePrefix + path; + return request(apiUrl, { json: true, timeout: 10000, headers: { diff --git a/src/main/context-handler.ts b/src/main/context-handler.ts index eff8666051..e0a11ca414 100644 --- a/src/main/context-handler.ts +++ b/src/main/context-handler.ts @@ -80,7 +80,6 @@ export class ContextHandler { return apiTarget } - // fixme public async getApiTargetUrl(): Promise { await this.ensurePort(); return `http://127.0.0.1:${this.proxyPort}${this.clusterUrl.path}`; diff --git a/src/renderer/components/cluster-manager/cluster-context.tsx b/src/renderer/components/cluster-manager/cluster-context.tsx index c1feaf091c..9214d7cbe6 100644 --- a/src/renderer/components/cluster-manager/cluster-context.tsx +++ b/src/renderer/components/cluster-manager/cluster-context.tsx @@ -12,7 +12,7 @@ export interface ClusterContextValue { export function getClusterContext(): ClusterContextValue { return { - clusterId: clusterStore.activeCluster, + clusterId: clusterStore.activeClusterId, workspaceId: workspaceStore.currentWorkspace, } } diff --git a/src/renderer/components/cluster-manager/clusters-menu.tsx b/src/renderer/components/cluster-manager/clusters-menu.tsx index 55761f303f..e26d234f01 100644 --- a/src/renderer/components/cluster-manager/clusters-menu.tsx +++ b/src/renderer/components/cluster-manager/clusters-menu.tsx @@ -23,7 +23,7 @@ interface Props { @observer export class ClustersMenu extends React.Component { selectCluster = (cluster: Cluster) => { - clusterStore.activeCluster = cluster.id; + clusterStore.activeClusterId = cluster.id; console.log('load lens for cluster:', cluster) } @@ -59,7 +59,7 @@ export class ClustersMenu extends React.Component { return (
{clusters.map(cluster => { - const isActive = cluster.id === clusterStore.activeCluster; + const isActive = cluster.id === clusterStore.activeClusterId; return (