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

auto-refresh status only for active (selected) cluster

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-07-12 13:41:05 +03:00
parent e7fc6b4a35
commit aab91863fc
7 changed files with 37 additions and 26 deletions

View File

@ -48,11 +48,15 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
});
}
@observable activeCluster: ClusterId; // todo: current-context from kube-config?
@observable activeClusterId: ClusterId;
@observable removedClusters = observable.map<ClusterId, Cluster>();
@observable clusters = observable.map<ClusterId, Cluster>();
@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<ClusterStoreModel> {
@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<ClusterStoreModel> {
}
});
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

View File

@ -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",

View File

@ -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) {

View File

@ -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: {

View File

@ -80,7 +80,6 @@ export class ContextHandler {
return apiTarget
}
// fixme
public async getApiTargetUrl(): Promise<string> {
await this.ensurePort();
return `http://127.0.0.1:${this.proxyPort}${this.clusterUrl.path}`;

View File

@ -12,7 +12,7 @@ export interface ClusterContextValue {
export function getClusterContext(): ClusterContextValue {
return {
clusterId: clusterStore.activeCluster,
clusterId: clusterStore.activeClusterId,
workspaceId: workspaceStore.currentWorkspace,
}
}

View File

@ -23,7 +23,7 @@ interface Props {
@observer
export class ClustersMenu extends React.Component<Props> {
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<Props> {
return (
<div className={cssNames("ClustersMenu flex gaps column", className)}>
{clusters.map(cluster => {
const isActive = cluster.id === clusterStore.activeCluster;
const isActive = cluster.id === clusterStore.activeClusterId;
return (
<ClusterIcon
key={cluster.id}