From 299eceaee4621a0bfeb663c0a87a400efee31c6d Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Mon, 21 Sep 2020 13:20:50 +0300 Subject: [PATCH] Optimise cluster activate and refresh (#938) Signed-off-by: Lauri Nevala --- src/common/cluster-ipc.ts | 8 +++++++ src/main/cluster.ts | 15 ++++++++----- src/main/shell-session.ts | 2 +- .../+cluster-settings/cluster-settings.tsx | 22 +++++++++++++++++-- .../cluster-manager/cluster-status.tsx | 6 ++--- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/common/cluster-ipc.ts b/src/common/cluster-ipc.ts index b8f9f5f84b..f48ce0f9c4 100644 --- a/src/common/cluster-ipc.ts +++ b/src/common/cluster-ipc.ts @@ -14,6 +14,14 @@ export const clusterIpc = { }, }), + refresh: createIpcChannel({ + channel: "cluster:refresh", + handle: (clusterId: ClusterId) => { + const cluster = clusterStore.getById(clusterId); + if (cluster) return cluster.refresh(); + }, + }), + disconnect: createIpcChannel({ channel: "cluster:disconnect", handle: (clusterId: ClusterId) => { diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 48bf01ee02..9fc9172296 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -133,7 +133,13 @@ export class Cluster implements ClusterModel { if (this.disconnected || (!init && !this.accessible)) { await this.reconnect(); } - await this.refresh(); + await this.refreshConnectionStatus() + if (this.accessible) { + await this.refreshAllowedResources() + this.ready = true + this.kubeCtl = new Kubectl(this.version) + this.kubeCtl.ensureKubectl() // download kubectl in background, so it's not blocking dashboard + } return this.pushState(); } @@ -159,15 +165,14 @@ export class Cluster implements ClusterModel { @action async refresh() { logger.info(`[CLUSTER]: refresh`, this.getMeta()); - await this.refreshConnectionStatus(); // refresh "version", "online", etc. + await this.whenInitialized; + await this.refreshConnectionStatus(); if (this.accessible) { - this.kubeCtl = new Kubectl(this.version) this.distribution = this.detectKubernetesDistribution(this.version) const [features, isAdmin, nodesCount] = await Promise.all([ getFeatures(this), this.isClusterAdmin(), this.getNodeCount(), - this.kubeCtl.ensureKubectl() ]); this.features = features; this.isAdmin = isAdmin; @@ -176,8 +181,8 @@ export class Cluster implements ClusterModel { this.refreshEvents(), this.refreshAllowedResources(), ]); - this.ready = true } + this.pushState(); } @action diff --git a/src/main/shell-session.ts b/src/main/shell-session.ts index 704d97382a..1d9d722f57 100644 --- a/src/main/shell-session.ts +++ b/src/main/shell-session.ts @@ -39,7 +39,7 @@ export class ShellSession extends EventEmitter { public async open() { this.kubectlBinDir = await this.kubectl.binDir() const pathFromPreferences = userStore.preferences.kubectlBinariesPath || Kubectl.bundledKubectlPath - this.kubectlPathDir = userStore.preferences.downloadKubectlBinaries ? await this.kubectl.binDir() : path.dirname(pathFromPreferences) + this.kubectlPathDir = userStore.preferences.downloadKubectlBinaries ? this.kubectlBinDir : path.dirname(pathFromPreferences) this.helmBinDir = helmCli.getBinaryDir() const env = await this.getCachedShellEnv() const shell = env.PTYSHELL diff --git a/src/renderer/components/+cluster-settings/cluster-settings.tsx b/src/renderer/components/+cluster-settings/cluster-settings.tsx index 0cec665538..b284ed9c88 100644 --- a/src/renderer/components/+cluster-settings/cluster-settings.tsx +++ b/src/renderer/components/+cluster-settings/cluster-settings.tsx @@ -1,11 +1,12 @@ import "./cluster-settings.scss"; import React from "react"; -import { observer } from "mobx-react"; +import { observer, disposeOnUnmount } from "mobx-react"; import { Features } from "./features"; import { Removal } from "./removal"; import { Status } from "./status"; import { General } from "./general"; +import { Cluster } from "../../../main/cluster"; import { WizardLayout } from "../layout/wizard-layout"; import { ClusterIcon } from "../cluster-icon"; import { Icon } from "../icon"; @@ -13,14 +14,25 @@ import { navigate } from "../../navigation"; import { IClusterSettingsRouteParams } from "./cluster-settings.route"; import { clusterStore } from "../../../common/cluster-store"; import { RouteComponentProps } from "react-router"; +import { clusterIpc } from "../../../common/cluster-ipc"; +import { autorun } from "mobx"; interface Props extends RouteComponentProps { } @observer export class ClusterSettings extends React.Component { + get cluster(): Cluster { + return clusterStore.getById(this.props.match.params.clusterId); + } + async componentDidMount() { window.addEventListener('keydown', this.onEscapeKey); + disposeOnUnmount(this, + autorun(() => { + this.refreshCluster(); + }) + ) } componentWillUnmount() { @@ -34,12 +46,18 @@ export class ClusterSettings extends React.Component { } } + refreshCluster = () => { + if(this.cluster) { + clusterIpc.refresh.invokeFromRenderer(this.cluster.id); + } + } + close() { navigate("/"); } render() { - const cluster = clusterStore.getById(this.props.match.params.clusterId); + const cluster = this.cluster if (!cluster) return null; const header = ( <> diff --git a/src/renderer/components/cluster-manager/cluster-status.tsx b/src/renderer/components/cluster-manager/cluster-status.tsx index b0fae210dd..846b26a504 100644 --- a/src/renderer/components/cluster-manager/cluster-status.tsx +++ b/src/renderer/components/cluster-manager/cluster-status.tsx @@ -39,7 +39,7 @@ export class ClusterStatus extends React.Component { }); }) if (this.cluster.disconnected) { - await this.refreshCluster(); + await this.activateCluster(); } } @@ -47,13 +47,13 @@ export class ClusterStatus extends React.Component { ipcRenderer.removeAllListeners(`kube-auth:${this.props.clusterId}`); } - refreshCluster = async () => { + activateCluster = async () => { await clusterIpc.activate.invokeFromRenderer(this.props.clusterId); } reconnect = async () => { this.isReconnecting = true; - await this.refreshCluster(); + await this.activateCluster(); this.isReconnecting = false; }