1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

[FIX]: Cluster dashboard not rendered (#848)

* Cluster dashboard not rendered, fix #811

Signed-off-by: Roman <ixrock@gmail.com>

* refactoring

Signed-off-by: Roman <ixrock@gmail.com>

* actual fix

Signed-off-by: Roman <ixrock@gmail.com>

* reverted back reconnect check in cluster.activate()

Signed-off-by: Roman <ixrock@gmail.com>

* fix: cluster.activate() should not start reconnect on init (if not accessible)

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-09-11 12:02:25 +03:00 committed by GitHub
parent e005985621
commit 7722f443eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 66 additions and 61 deletions

View File

@ -3,21 +3,14 @@ import { ClusterId, clusterStore } from "./cluster-store";
import { tracker } from "./tracker"; import { tracker } from "./tracker";
export const clusterIpc = { export const clusterIpc = {
initView: createIpcChannel({
channel: "cluster:init",
handle: async (clusterId: ClusterId, frameId: number) => {
const cluster = clusterStore.getById(clusterId);
if (cluster) {
cluster.frameId = frameId; // save cluster's webFrame.routingId to be able to send push-updates
return cluster.pushState();
}
},
}),
activate: createIpcChannel({ activate: createIpcChannel({
channel: "cluster:activate", channel: "cluster:activate",
handle: (clusterId: ClusterId) => { handle: (clusterId: ClusterId, frameId?: number) => {
return clusterStore.getById(clusterId)?.activate(); const cluster = clusterStore.getById(clusterId);
if (cluster) {
if (frameId) cluster.frameId = frameId; // save cluster's webFrame.routingId to be able to send push-updates
return cluster.activate(true);
}
}, },
}), }),

View File

@ -63,7 +63,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
static embedCustomKubeConfig(clusterId: ClusterId, kubeConfig: KubeConfig | string): string { static embedCustomKubeConfig(clusterId: ClusterId, kubeConfig: KubeConfig | string): string {
const filePath = ClusterStore.getCustomKubeConfigPath(clusterId); const filePath = ClusterStore.getCustomKubeConfigPath(clusterId);
const fileContents = typeof kubeConfig == "string" ? kubeConfig : dumpConfigYaml(kubeConfig); const fileContents = typeof kubeConfig == "string" ? kubeConfig : dumpConfigYaml(kubeConfig);
saveToAppFiles(filePath, fileContents, { mode: 0o600}); saveToAppFiles(filePath, fileContents, { mode: 0o600 });
return filePath; return filePath;
} }
@ -209,11 +209,17 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
export const clusterStore = ClusterStore.getInstance<ClusterStore>(); export const clusterStore = ClusterStore.getInstance<ClusterStore>();
export function getHostedClusterId(): ClusterId { export function getClusterIdFromHost(hostname: string): ClusterId {
const clusterHost = location.hostname.match(/^(.*?)\.localhost/); const subDomains = hostname.split(":")[0].split(".");
if (clusterHost) { return subDomains.slice(-2)[0]; // e.g host == "%clusterId.localhost:45345"
return clusterHost[1] }
}
export function getClusterFrameUrl(clusterId: ClusterId) {
return `//${clusterId}.${location.host}`;
}
export function getHostedClusterId() {
return getClusterIdFromHost(location.hostname);
} }
export function getHostedCluster(): Cluster { export function getHostedCluster(): Cluster {

View File

@ -1,7 +1,7 @@
import "../common/cluster-ipc"; import "../common/cluster-ipc";
import type http from "http" import type http from "http"
import { autorun } from "mobx"; import { autorun } from "mobx";
import { ClusterId, clusterStore } from "../common/cluster-store" import { clusterStore, getClusterIdFromHost } from "../common/cluster-store"
import { Cluster } from "./cluster" import { Cluster } from "./cluster"
import logger from "./logger"; import logger from "./logger";
import { apiKubePrefix } from "../common/vars"; import { apiKubePrefix } from "../common/vars";
@ -38,26 +38,20 @@ export class ClusterManager {
}) })
} }
protected getCluster(id: ClusterId) {
return clusterStore.getById(id);
}
getClusterForRequest(req: http.IncomingMessage): Cluster { getClusterForRequest(req: http.IncomingMessage): Cluster {
let cluster: Cluster = null let cluster: Cluster = null
// lens-server is connecting to 127.0.0.1:<port>/<uid> // lens-server is connecting to 127.0.0.1:<port>/<uid>
if (req.headers.host.startsWith("127.0.0.1")) { if (req.headers.host.startsWith("127.0.0.1")) {
const clusterId = req.url.split("/")[1] const clusterId = req.url.split("/")[1]
if (clusterId) { const cluster = clusterStore.getById(clusterId)
cluster = this.getCluster(clusterId) if (cluster) {
if (cluster) { // we need to swap path prefix so that request is proxied to kube api
// we need to swap path prefix so that request is proxied to kube api req.url = req.url.replace(`/${clusterId}`, apiKubePrefix)
req.url = req.url.replace(`/${clusterId}`, apiKubePrefix)
}
} }
} else { } else {
const id = req.headers.host.split(".")[0] const clusterId = getClusterIdFromHost(req.headers.host);
cluster = this.getCluster(id) cluster = clusterStore.getById(clusterId)
} }
return cluster; return cluster;

View File

@ -2,7 +2,7 @@ import type { ClusterId, ClusterModel, ClusterPreferences } from "../common/clus
import type { IMetricsReqParams } from "../renderer/api/endpoints/metrics.api"; import type { IMetricsReqParams } from "../renderer/api/endpoints/metrics.api";
import type { WorkspaceId } from "../common/workspace-store"; import type { WorkspaceId } from "../common/workspace-store";
import type { FeatureStatusMap } from "./feature" import type { FeatureStatusMap } from "./feature"
import { action, computed, intercept, observable, reaction, toJS, when } from "mobx"; import { action, computed, observable, reaction, toJS, when } from "mobx";
import { apiKubePrefix } from "../common/vars"; import { apiKubePrefix } from "../common/vars";
import { broadcastIpc } from "../common/ipc"; import { broadcastIpc } from "../common/ipc";
import { ContextHandler } from "./context-handler" import { ContextHandler } from "./context-handler"
@ -126,13 +126,13 @@ export class Cluster implements ClusterModel {
this.eventDisposers.length = 0; this.eventDisposers.length = 0;
} }
async activate() { async activate(init = false) {
logger.info(`[CLUSTER]: activate`, this.getMeta()); logger.info(`[CLUSTER]: activate`, this.getMeta());
await this.whenInitialized; await this.whenInitialized;
if (!this.eventDisposers.length) { if (!this.eventDisposers.length) {
this.bindEvents(); this.bindEvents();
} }
if (this.disconnected || !this.accessible) { if (this.disconnected || (!init && !this.accessible)) {
await this.reconnect(); await this.reconnect();
} }
await this.refresh(); await this.refresh();
@ -411,6 +411,7 @@ export class Cluster implements ClusterModel {
id: this.id, id: this.id,
name: this.contextName, name: this.contextName,
initialized: this.initialized, initialized: this.initialized,
ready: this.ready,
online: this.online, online: this.online,
accessible: this.accessible, accessible: this.accessible,
disconnected: this.disconnected, disconnected: this.disconnected,

View File

@ -1,9 +1,9 @@
import type { ClusterId } from "../common/cluster-store";
import { clusterStore } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store";
import { BrowserWindow, dialog, ipcMain, shell, webContents } from "electron" import { BrowserWindow, dialog, ipcMain, shell, webContents } from "electron"
import windowStateKeeper from "electron-window-state" import windowStateKeeper from "electron-window-state"
import { observable } from "mobx"; import { observable } from "mobx";
import { initMenu } from "./menu"; import { initMenu } from "./menu";
import type { ClusterId } from "../common/cluster-store";
export class WindowManager { export class WindowManager {
protected mainView: BrowserWindow; protected mainView: BrowserWindow;
@ -42,7 +42,7 @@ export class WindowManager {
}); });
// track visible cluster from ui // track visible cluster from ui
ipcMain.on("cluster-view:change", (event, clusterId: ClusterId) => { ipcMain.on("cluster-view:current-id", (event, clusterId: ClusterId) => {
this.activeClusterId = clusterId; this.activeClusterId = clusterId;
}); });

View File

@ -4,7 +4,7 @@ import { render } from "react-dom";
import { isMac } from "../common/vars"; import { isMac } from "../common/vars";
import { userStore } from "../common/user-store"; import { userStore } from "../common/user-store";
import { workspaceStore } from "../common/workspace-store"; import { workspaceStore } from "../common/workspace-store";
import { clusterStore, getHostedClusterId } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store";
import { i18nStore } from "./i18n"; import { i18nStore } from "./i18n";
import { themeStore } from "./theme.store"; import { themeStore } from "./theme.store";
import { App } from "./components/app"; import { App } from "./components/app";
@ -35,4 +35,4 @@ export async function bootstrap(App: AppComponent) {
} }
// run // run
bootstrap(getHostedClusterId() ? App : LensApp); bootstrap(process.isMainFrame ? LensApp : App);

View File

@ -43,8 +43,8 @@ export class App extends React.Component {
const clusterId = getHostedClusterId(); const clusterId = getHostedClusterId();
logger.info(`[APP]: Init dashboard, clusterId=${clusterId}, frameId=${frameId}`) logger.info(`[APP]: Init dashboard, clusterId=${clusterId}, frameId=${frameId}`)
await Terminal.preloadFonts() await Terminal.preloadFonts()
await clusterIpc.initView.invokeFromRenderer(clusterId, frameId); await clusterIpc.activate.invokeFromRenderer(clusterId, frameId);
await getHostedCluster().whenInitialized; await getHostedCluster().whenReady; // cluster.refresh() is done at this point
} }
get startURL() { get startURL() {

View File

@ -1,7 +1,7 @@
import "./cluster-manager.scss" import "./cluster-manager.scss"
import React from "react"; import React from "react";
import { Redirect, Route, Switch } from "react-router"; import { Redirect, Route, Switch } from "react-router";
import { reaction } from "mobx"; import { comparer, reaction } from "mobx";
import { disposeOnUnmount, observer } from "mobx-react"; import { disposeOnUnmount, observer } from "mobx-react";
import { ClustersMenu } from "./clusters-menu"; import { ClustersMenu } from "./clusters-menu";
import { BottomBar } from "./bottom-bar"; import { BottomBar } from "./bottom-bar";
@ -23,11 +23,14 @@ export class ClusterManager extends React.Component {
fireImmediately: true fireImmediately: true
}), }),
reaction(() => [ reaction(() => [
getMatchedClusterId(), // refresh when active cluster-view changed
hasLoadedView(getMatchedClusterId()), // refresh when cluster's webview loaded hasLoadedView(getMatchedClusterId()), // refresh when cluster's webview loaded
getMatchedCluster()?.available, // refresh on disconnect active-cluster getMatchedCluster()?.available, // refresh on disconnect active-cluster
getMatchedCluster()?.ready, // refresh when cluster ready-state change
], refreshViews, { ], refreshViews, {
fireImmediately: true fireImmediately: true,
}) equals: comparer.shallow,
}),
]) ])
} }

View File

@ -38,7 +38,7 @@ export class ClusterStatus extends React.Component<Props> {
error: res.error, error: res.error,
}); });
}) })
if (!this.cluster.initialized || this.cluster.disconnected) { if (this.cluster.disconnected) {
await this.refreshCluster(); await this.refreshCluster();
} }
} }
@ -63,7 +63,7 @@ export class ClusterStatus extends React.Component<Props> {
if (!hasErrors || this.isReconnecting) { if (!hasErrors || this.isReconnecting) {
return ( return (
<> <>
<CubeSpinner /> <CubeSpinner/>
<pre className="kube-auth-out"> <pre className="kube-auth-out">
<p>{this.isReconnecting ? "Reconnecting..." : "Connecting..."}</p> <p>{this.isReconnecting ? "Reconnecting..." : "Connecting..."}</p>
{authOutput.map(({ data, error }, index) => { {authOutput.map(({ data, error }, index) => {
@ -75,7 +75,7 @@ export class ClusterStatus extends React.Component<Props> {
} }
return ( return (
<> <>
<Icon material="cloud_off" className="error" /> <Icon material="cloud_off" className="error"/>
<h2> <h2>
{cluster.preferences.clusterName} {cluster.preferences.clusterName}
</h2> </h2>

View File

@ -2,7 +2,7 @@ import { reaction } from "mobx";
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { matchPath, RouteProps } from "react-router"; import { matchPath, RouteProps } from "react-router";
import { buildURL, navigation } from "../../navigation"; import { buildURL, navigation } from "../../navigation";
import { clusterStore, getHostedClusterId } from "../../../common/cluster-store"; import { clusterStore } from "../../../common/cluster-store";
import { clusterSettingsRoute } from "../+cluster-settings/cluster-settings.route"; import { clusterSettingsRoute } from "../+cluster-settings/cluster-settings.route";
export interface IClusterViewRouteParams { export interface IClusterViewRouteParams {
@ -34,11 +34,10 @@ export function getMatchedCluster() {
} }
if (ipcRenderer) { if (ipcRenderer) {
// Refresh global menu depending on active route's type (common/cluster view) if (process.isMainFrame) {
const isMainView = !getHostedClusterId(); // Keep track of active cluster-id for handling IPC/menus/etc.
if (isMainView) {
reaction(() => getMatchedClusterId(), clusterId => { reaction(() => getMatchedClusterId(), clusterId => {
ipcRenderer.send("cluster-view:change", clusterId); ipcRenderer.send("cluster-view:current-id", clusterId);
}, { }, {
fireImmediately: true fireImmediately: true
}) })

View File

@ -1,5 +1,5 @@
import { observable, when } from "mobx"; import { observable, when } from "mobx";
import { ClusterId, clusterStore } from "../../../common/cluster-store"; import { ClusterId, clusterStore, getClusterFrameUrl } from "../../../common/cluster-store";
import { getMatchedCluster } from "./cluster-view.route" import { getMatchedCluster } from "./cluster-view.route"
import logger from "../../../main/logger"; import logger from "../../../main/logger";
@ -21,29 +21,38 @@ export async function initView(clusterId: ClusterId) {
} }
logger.info(`[LENS-VIEW]: init dashboard, clusterId=${clusterId}`) logger.info(`[LENS-VIEW]: init dashboard, clusterId=${clusterId}`)
const cluster = clusterStore.getById(clusterId); const cluster = clusterStore.getById(clusterId);
await cluster.whenReady;
const parentElem = document.getElementById("lens-views"); const parentElem = document.getElementById("lens-views");
const iframe = document.createElement("iframe"); const iframe = document.createElement("iframe");
iframe.name = cluster.contextName; iframe.name = cluster.contextName;
iframe.setAttribute("src", `//${clusterId}.${location.host}`) iframe.setAttribute("src", getClusterFrameUrl(clusterId))
iframe.addEventListener("load", async () => { iframe.addEventListener("load", () => {
logger.info(`[LENS-VIEW]: loaded from ${iframe.src}`) logger.info(`[LENS-VIEW]: loaded from ${iframe.src}`)
lensViews.get(clusterId).isLoaded = true; lensViews.get(clusterId).isLoaded = true;
}) }, { once: true });
lensViews.set(clusterId, { clusterId, view: iframe }); lensViews.set(clusterId, { clusterId, view: iframe });
parentElem.appendChild(iframe); parentElem.appendChild(iframe);
// auto-clean when cluster removed await autoCleanOnRemove(clusterId, iframe);
}
export async function autoCleanOnRemove(clusterId: ClusterId, iframe: HTMLIFrameElement) {
await when(() => !clusterStore.getById(clusterId)); await when(() => !clusterStore.getById(clusterId));
logger.info(`[LENS-VIEW]: remove dashboard, clusterId=${clusterId}`) logger.info(`[LENS-VIEW]: remove dashboard, clusterId=${clusterId}`)
parentElem.removeChild(iframe)
lensViews.delete(clusterId) lensViews.delete(clusterId)
// Keep frame in DOM to avoid possible bugs when same cluster re-created after being removed.
// In that case for some reasons `webFrame.routingId` returns some previous frameId (usage in app.tsx)
// Issue: https://github.com/lensapp/lens/issues/811
iframe.dataset.meta = `${iframe.name} was removed at ${new Date().toLocaleString()}`;
iframe.removeAttribute("src")
iframe.removeAttribute("name")
} }
export function refreshViews() { export function refreshViews() {
const cluster = getMatchedCluster(); const cluster = getMatchedCluster();
lensViews.forEach(({ clusterId, view, isLoaded }) => { lensViews.forEach(({ clusterId, view, isLoaded }) => {
const isVisible = cluster && cluster.available && cluster.id === clusterId; const isCurrent = clusterId === cluster?.id;
view.style.display = isLoaded && isVisible ? "flex" : "none" const isReady = cluster?.available && cluster?.ready;
const isVisible = isCurrent && isLoaded && isReady;
view.style.display = isVisible ? "flex" : "none"
}) })
} }