mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
refactoring / clean up
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
4ddd32b05f
commit
03794e6c38
@ -12,7 +12,7 @@ import { comparer, computed, observable, reaction } from "mobx";
|
|||||||
import { autobind, EventEmitter } from "../utils";
|
import { autobind, EventEmitter } from "../utils";
|
||||||
import { ensureObjectSelfLink, KubeApi, parseKubeApi } from "./kube-api";
|
import { ensureObjectSelfLink, KubeApi, parseKubeApi } from "./kube-api";
|
||||||
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
|
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
|
||||||
import { apiPrefix, isProduction } from "../../common/vars";
|
import { apiPrefix, isDebugging, isProduction } from "../../common/vars";
|
||||||
import { apiManager } from "./api-manager";
|
import { apiManager } from "./api-manager";
|
||||||
|
|
||||||
export { IKubeWatchEvent, IKubeWatchEventStreamEnd };
|
export { IKubeWatchEvent, IKubeWatchEventStreamEnd };
|
||||||
@ -29,6 +29,11 @@ export interface IKubeWatchSubscribeStoreOptions {
|
|||||||
waitUntilLoaded?: boolean; // subscribe only after loading all stores, default: true
|
waitUntilLoaded?: boolean; // subscribe only after loading all stores, default: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IKubeWatchReconnectOptions {
|
||||||
|
reconnectAttempts: number;
|
||||||
|
timeout: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IKubeWatchLog {
|
export interface IKubeWatchLog {
|
||||||
message: string | Error;
|
message: string | Error;
|
||||||
meta?: object;
|
meta?: object;
|
||||||
@ -277,7 +282,10 @@ export class KubeWatchApi {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "STREAM_END": {
|
case "STREAM_END": {
|
||||||
this.onServerStreamEnd(event as IKubeWatchEventStreamEnd);
|
this.onServerStreamEnd(event as IKubeWatchEventStreamEnd, {
|
||||||
|
reconnectAttempts: 5,
|
||||||
|
timeout: 1000,
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -285,35 +293,36 @@ export class KubeWatchApi {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async onServerStreamEnd(event: IKubeWatchEventStreamEnd) {
|
protected async onServerStreamEnd(event: IKubeWatchEventStreamEnd, opts?: IKubeWatchReconnectOptions) {
|
||||||
const { apiBase, namespace } = parseKubeApi(event.url);
|
const { apiBase, namespace } = parseKubeApi(event.url);
|
||||||
const api = apiManager.getApi(apiBase);
|
const api = apiManager.getApi(apiBase);
|
||||||
|
|
||||||
if (api) {
|
if (!api) return;
|
||||||
try {
|
|
||||||
await api.refreshResourceVersion({ namespace });
|
|
||||||
this.connect();
|
|
||||||
} catch (error) {
|
|
||||||
this.log({
|
|
||||||
message: new Error(`Failed to connect on single stream end: ${error}`),
|
|
||||||
meta: { event, error },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (this.isActive) {
|
try {
|
||||||
setTimeout(() => this.onServerStreamEnd(event), 1000);
|
await api.refreshResourceVersion({ namespace });
|
||||||
}
|
this.connect();
|
||||||
|
} catch (error) {
|
||||||
|
this.log({
|
||||||
|
message: new Error(`Failed to connect on single stream end: ${error}`),
|
||||||
|
meta: { event, error },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (this.isActive && opts?.reconnectAttempts > 0) {
|
||||||
|
opts.reconnectAttempts--;
|
||||||
|
setTimeout(() => this.onServerStreamEnd(event, opts), opts.timeout); // repeat event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected log({ message, meta = {} }: IKubeWatchLog) {
|
protected log({ message, meta = {} }: IKubeWatchLog) {
|
||||||
if (isProduction) {
|
if (isProduction && !isDebugging) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const logMessage = `%c[KUBE-WATCH-API]: ${String(message).toUpperCase()}`;
|
const logMessage = `%c[KUBE-WATCH-API]: ${String(message).toUpperCase()}`;
|
||||||
const isError = message instanceof Error;
|
const isError = message instanceof Error;
|
||||||
const textStyle = `font-weight: bold; ${isError ? "color: red;" : ""}`;
|
const textStyle = `font-weight: bold;`;
|
||||||
const time = new Date().toLocaleString();
|
const time = new Date().toLocaleString();
|
||||||
|
|
||||||
if (isError) {
|
if (isError) {
|
||||||
|
|||||||
@ -52,6 +52,10 @@ export class EventStore extends KubeObjectStore<KubeEvent> {
|
|||||||
|
|
||||||
return compact(eventsWithError);
|
return compact(eventsWithError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getWarningsCount() {
|
||||||
|
return this.getWarnings().length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const eventStore = new EventStore();
|
export const eventStore = new EventStore();
|
||||||
|
|||||||
@ -122,7 +122,7 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
|||||||
|
|
||||||
// if user has given static list of namespaces let's not start watches because watch adds stuff that's not wanted
|
// if user has given static list of namespaces let's not start watches because watch adds stuff that's not wanted
|
||||||
if (accessibleNamespaces.length > 0) {
|
if (accessibleNamespaces.length > 0) {
|
||||||
return []; // no-op
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.getSubscribeApis();
|
return super.getSubscribeApis();
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { sum } from "lodash";
|
||||||
import { action, computed, observable } from "mobx";
|
import { action, computed, observable } from "mobx";
|
||||||
import { clusterApi, IClusterMetrics, INodeMetrics, Node, nodesApi } from "../../api/endpoints";
|
import { clusterApi, IClusterMetrics, INodeMetrics, Node, nodesApi } from "../../api/endpoints";
|
||||||
import { autobind } from "../../utils";
|
import { autobind } from "../../utils";
|
||||||
@ -62,6 +63,10 @@ export class NodesStore extends KubeObjectStore<Node> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getWarningsCount(): number {
|
||||||
|
return sum(this.items.map((node: Node) => node.getWarningConditions().length));
|
||||||
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
super.reset();
|
super.reset();
|
||||||
this.metrics = {};
|
this.metrics = {};
|
||||||
|
|||||||
@ -46,7 +46,6 @@ import { computed, reaction } from "mobx";
|
|||||||
import { nodesStore } from "./+nodes/nodes.store";
|
import { nodesStore } from "./+nodes/nodes.store";
|
||||||
import { podsStore } from "./+workloads-pods/pods.store";
|
import { podsStore } from "./+workloads-pods/pods.store";
|
||||||
import { kubeWatchApi } from "../api/kube-watch-api";
|
import { kubeWatchApi } from "../api/kube-watch-api";
|
||||||
import { sum } from "lodash";
|
|
||||||
import { ReplicaSetScaleDialog } from "./+workloads-replicasets/replicaset-scale-dialog";
|
import { ReplicaSetScaleDialog } from "./+workloads-replicasets/replicaset-scale-dialog";
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
@ -82,19 +81,14 @@ export class App extends React.Component {
|
|||||||
preload: true,
|
preload: true,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
reaction(() => this.warningsCount, (count) => {
|
reaction(() => this.warningsTotal, (count: number) => {
|
||||||
broadcastMessage(`cluster-warning-event-count:${getHostedCluster().id}`, count);
|
broadcastMessage(`cluster-warning-event-count:${getHostedCluster().id}`, count);
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: move to nodes-store.ts
|
@computed get warningsTotal(): number {
|
||||||
@computed get warningsCount() {
|
return nodesStore.getWarningsCount() + eventStore.getWarningsCount();
|
||||||
let warnings = sum(nodesStore.items.map(node => node.getWarningConditions().length));
|
|
||||||
|
|
||||||
warnings = warnings + eventStore.getWarnings().length;
|
|
||||||
|
|
||||||
return warnings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get startURL() {
|
get startURL() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user