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

Make more Cluster data computed instead of stateful

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-03-14 10:00:47 -04:00
parent 9cbe7d5078
commit 1c26fc4af9
3 changed files with 21 additions and 24 deletions

View File

@ -184,9 +184,7 @@ export const initialNodeShellImage = "docker.io/alpine:3.13";
*/ */
export interface ClusterState { export interface ClusterState {
apiUrl: string; apiUrl: string;
online: boolean; connectionStatus: ClusterStatus | undefined;
disconnected: boolean;
accessible: boolean;
ready: boolean; ready: boolean;
isAdmin: boolean; isAdmin: boolean;
allowedNamespaces: string[]; allowedNamespaces: string[];

View File

@ -6,7 +6,7 @@
import { computed, observable, toJS, runInAction } from "mobx"; import { computed, observable, toJS, runInAction } from "mobx";
import type { KubeApiResource } from "../rbac"; import type { KubeApiResource } from "../rbac";
import type { ClusterState, ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences, UpdateClusterModel, ClusterConfigData } from "../cluster-types"; import type { ClusterState, ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences, UpdateClusterModel, ClusterConfigData } from "../cluster-types";
import { ClusterMetadataKey, clusterModelIdChecker, updateClusterModelChecker } from "../cluster-types"; import { ClusterStatus, ClusterMetadataKey, clusterModelIdChecker, updateClusterModelChecker } from "../cluster-types";
import type { IObservableValue } from "mobx"; import type { IObservableValue } from "mobx";
import { replaceObservableObject } from "../utils/replace-observable-object"; import { replaceObservableObject } from "../utils/replace-observable-object";
import { pick } from "lodash"; import { pick } from "lodash";
@ -32,15 +32,23 @@ export class Cluster {
*/ */
readonly apiUrl: IObservableValue<string>; readonly apiUrl: IObservableValue<string>;
readonly connectionStatus = observable.box<ClusterStatus>();
/** /**
* Describes if we can detect that cluster is online * Describes if we can detect that cluster is online
*/ */
readonly online = observable.box(false); readonly online = computed(() => {
const status = this.connectionStatus.get() ?? ClusterStatus.Offline;
return status > ClusterStatus.Offline;
});
/** /**
* Describes if user is able to access cluster resources * Describes if user is able to access cluster resources
*/ */
readonly accessible = observable.box(false); readonly accessible = computed(() => (
this.connectionStatus.get() === ClusterStatus.AccessGranted
));
/** /**
* Is cluster instance in usable state * Is cluster instance in usable state
@ -50,7 +58,9 @@ export class Cluster {
/** /**
* Is cluster disconnected. False if user has selected to connect. * Is cluster disconnected. False if user has selected to connect.
*/ */
readonly disconnected = observable.box(true); readonly disconnected = computed(() => (
this.connectionStatus.get() === undefined
));
/** /**
* Does user have admin like access * Does user have admin like access
@ -188,14 +198,12 @@ export class Cluster {
getState(): ClusterState { getState(): ClusterState {
return { return {
apiUrl: this.apiUrl.get(), apiUrl: this.apiUrl.get(),
online: this.online.get(),
ready: this.ready.get(), ready: this.ready.get(),
disconnected: this.disconnected.get(),
accessible: this.accessible.get(),
isAdmin: this.isAdmin.get(), isAdmin: this.isAdmin.get(),
allowedNamespaces: this.allowedNamespaces.toJSON(), allowedNamespaces: this.allowedNamespaces.toJSON(),
resourcesToShow: this.resourcesToShow.toJSON(), resourcesToShow: this.resourcesToShow.toJSON(),
isGlobalWatchEnabled: this.isGlobalWatchEnabled.get(), isGlobalWatchEnabled: this.isGlobalWatchEnabled.get(),
connectionStatus: this.connectionStatus.get(),
}; };
} }
@ -204,15 +212,13 @@ export class Cluster {
*/ */
setState(state: ClusterState) { setState(state: ClusterState) {
runInAction(() => { runInAction(() => {
this.accessible.set(state.accessible);
this.allowedNamespaces.replace(state.allowedNamespaces); this.allowedNamespaces.replace(state.allowedNamespaces);
this.resourcesToShow.replace(state.resourcesToShow); this.resourcesToShow.replace(state.resourcesToShow);
this.apiUrl.set(state.apiUrl); this.apiUrl.set(state.apiUrl);
this.disconnected.set(state.disconnected);
this.isAdmin.set(state.isAdmin); this.isAdmin.set(state.isAdmin);
this.isGlobalWatchEnabled.set(state.isGlobalWatchEnabled); this.isGlobalWatchEnabled.set(state.isGlobalWatchEnabled);
this.online.set(state.online);
this.ready.set(state.ready); this.ready.set(state.ready);
this.connectionStatus.set(state.connectionStatus);
}); });
} }

View File

@ -124,10 +124,6 @@ class ClusterConnection {
this.dependencies.logger.info("[CLUSTER]: starting connection ...", this.cluster.getMeta()); this.dependencies.logger.info("[CLUSTER]: starting connection ...", this.cluster.getMeta());
await this.dependencies.kubeAuthProxyServer.ensureRunning(); await this.dependencies.kubeAuthProxyServer.ensureRunning();
runInAction(() => {
this.cluster.disconnected.set(false);
});
} catch (error) { } catch (error) {
this.dependencies.broadcastConnectionUpdate({ this.dependencies.broadcastConnectionUpdate({
level: "error", level: "error",
@ -185,10 +181,8 @@ class ClusterConnection {
runInAction(() => { runInAction(() => {
this.dependencies.logger.info(`[CLUSTER]: disconnecting`, { id: this.cluster.id }); this.dependencies.logger.info(`[CLUSTER]: disconnecting`, { id: this.cluster.id });
this.eventsDisposer(); this.eventsDisposer();
this.dependencies.kubeAuthProxyServer?.stop(); this.dependencies.kubeAuthProxyServer.stop();
this.cluster.disconnected.set(true); this.cluster.connectionStatus.set(undefined);
this.cluster.online.set(false);
this.cluster.accessible.set(false);
this.cluster.ready.set(false); this.cluster.ready.set(false);
this.activated = false; this.activated = false;
this.cluster.allowedNamespaces.clear(); this.cluster.allowedNamespaces.clear();
@ -267,11 +261,10 @@ class ClusterConnection {
} }
async refreshConnectionStatus() { async refreshConnectionStatus() {
const connectionStatus = await this.getConnectionStatus(); const status = await this.getConnectionStatus();
runInAction(() => { runInAction(() => {
this.cluster.online.set(connectionStatus > ClusterStatus.Offline); this.cluster.connectionStatus.set(status);
this.cluster.accessible.set(connectionStatus == ClusterStatus.AccessGranted);
}); });
} }