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:
parent
e7fc6b4a35
commit
aab91863fc
@ -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 removedClusters = observable.map<ClusterId, Cluster>();
|
||||||
@observable clusters = 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());
|
return Array.from(this.clusters.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,8 +78,8 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
removeById(clusterId: ClusterId): void {
|
removeById(clusterId: ClusterId): void {
|
||||||
if (this.activeCluster === clusterId) {
|
if (this.activeClusterId === clusterId) {
|
||||||
this.activeCluster = null;
|
this.activeClusterId = null;
|
||||||
}
|
}
|
||||||
this.clusters.delete(clusterId);
|
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.clusters.replace(newClusters);
|
||||||
this.removedClusters.replace(removedClusters);
|
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 {
|
toJSON(): ClusterStoreModel {
|
||||||
return toJS({
|
return toJS({
|
||||||
activeCluster: this.activeCluster,
|
activeCluster: this.activeClusterId,
|
||||||
clusters: this.clustersList.map(cluster => cluster.toJSON()),
|
clusters: this.clustersList.map(cluster => cluster.toJSON()),
|
||||||
}, {
|
}, {
|
||||||
recurseEverything: true
|
recurseEverything: true
|
||||||
|
|||||||
@ -5,7 +5,7 @@ export enum ClusterIpcMessage {
|
|||||||
CLUSTER_STOP = "cluster-stop",
|
CLUSTER_STOP = "cluster-stop",
|
||||||
CLUSTER_REMOVE = "cluster-remove",
|
CLUSTER_REMOVE = "cluster-remove",
|
||||||
CLUSTER_REMOVE_WORKSPACE = "cluster-remove-all-from-workspace",
|
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_INSTALL = "cluster-feature-install",
|
||||||
FEATURE_UPGRADE = "cluster-feature-upgrade",
|
FEATURE_UPGRADE = "cluster-feature-upgrade",
|
||||||
FEATURE_REMOVE = "cluster-feature-remove",
|
FEATURE_REMOVE = "cluster-feature-remove",
|
||||||
|
|||||||
@ -39,6 +39,13 @@ export class ClusterManager {
|
|||||||
removedClusters.clear();
|
removedClusters.clear();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// auto-refresh status for active cluster
|
||||||
|
autorun(() => {
|
||||||
|
const { activeCluster } = clusterStore;
|
||||||
|
if (activeCluster && activeCluster.initialized) {
|
||||||
|
activeCluster.refreshStatus();
|
||||||
|
}
|
||||||
|
});
|
||||||
// listen ipc-events
|
// listen ipc-events
|
||||||
ClusterManager.ipcListen(this);
|
ClusterManager.ipcListen(this);
|
||||||
}
|
}
|
||||||
@ -87,7 +94,6 @@ export class ClusterManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fixme
|
|
||||||
getClusterForRequest(req: http.IncomingMessage): Cluster {
|
getClusterForRequest(req: http.IncomingMessage): Cluster {
|
||||||
let cluster: Cluster = null
|
let cluster: Cluster = null
|
||||||
|
|
||||||
@ -147,7 +153,10 @@ export class ClusterManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async refreshCluster(clusterId: ClusterId) {
|
protected async refreshCluster(clusterId: ClusterId) {
|
||||||
await this.getCluster(clusterId)?.refreshStatus();
|
const cluster = this.getCluster(clusterId);
|
||||||
|
if (cluster) {
|
||||||
|
await cluster.refreshStatus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ipcListen(clusterManager: ClusterManager) {
|
static ipcListen(clusterManager: ClusterManager) {
|
||||||
|
|||||||
@ -42,7 +42,6 @@ export class Cluster implements ClusterModel {
|
|||||||
@observable kubeConfigPath: string;
|
@observable kubeConfigPath: string;
|
||||||
@observable url: string; // cluster-api url
|
@observable url: string; // cluster-api url
|
||||||
@observable proxyUrl: string; // lens-proxy url
|
@observable proxyUrl: string; // lens-proxy url
|
||||||
@observable kubeProxyUrl: string;
|
|
||||||
@observable webContentUrl: string;
|
@observable webContentUrl: string;
|
||||||
@observable proxyPort: number;
|
@observable proxyPort: number;
|
||||||
@observable online: boolean;
|
@observable online: boolean;
|
||||||
@ -60,32 +59,26 @@ export class Cluster implements ClusterModel {
|
|||||||
this.updateModel(model);
|
this.updateModel(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
updateModel(model: ClusterModel) {
|
updateModel(model: ClusterModel) {
|
||||||
Object.assign(this, model);
|
Object.assign(this, model);
|
||||||
if (!this.contextName) {
|
this.url = this.getKubeconfig().getCurrentCluster().server;
|
||||||
this.contextName = this.preferences.clusterName;
|
this.contextName = this.preferences.clusterName;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async init(proxyPort: number) {
|
async init(proxyPort: number) {
|
||||||
try {
|
try {
|
||||||
this.proxyPort = proxyPort;
|
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.contextHandler = new ContextHandler(this);
|
||||||
this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler);
|
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;
|
this.initialized = true;
|
||||||
|
|
||||||
logger.info(`[CLUSTER]: init success`, {
|
logger.info(`[CLUSTER]: init success`, {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
url: this.url,
|
url: this.url,
|
||||||
proxyUrl: this.proxyUrl,
|
proxyUrl: this.proxyUrl,
|
||||||
kubeProxyUrl: this.kubeProxyUrl,
|
|
||||||
webContentUrl: this.webContentUrl,
|
webContentUrl: this.webContentUrl,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -150,7 +143,8 @@ export class Cluster implements ClusterModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected k8sRequest(path: string, options: RequestPromiseOptions = {}) {
|
protected k8sRequest(path: string, options: RequestPromiseOptions = {}) {
|
||||||
return request(this.kubeProxyUrl + path, {
|
const apiUrl = this.proxyUrl + apiKubePrefix + path;
|
||||||
|
return request(apiUrl, {
|
||||||
json: true,
|
json: true,
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -80,7 +80,6 @@ export class ContextHandler {
|
|||||||
return apiTarget
|
return apiTarget
|
||||||
}
|
}
|
||||||
|
|
||||||
// fixme
|
|
||||||
public async getApiTargetUrl(): Promise<string> {
|
public async getApiTargetUrl(): Promise<string> {
|
||||||
await this.ensurePort();
|
await this.ensurePort();
|
||||||
return `http://127.0.0.1:${this.proxyPort}${this.clusterUrl.path}`;
|
return `http://127.0.0.1:${this.proxyPort}${this.clusterUrl.path}`;
|
||||||
|
|||||||
@ -12,7 +12,7 @@ export interface ClusterContextValue {
|
|||||||
|
|
||||||
export function getClusterContext(): ClusterContextValue {
|
export function getClusterContext(): ClusterContextValue {
|
||||||
return {
|
return {
|
||||||
clusterId: clusterStore.activeCluster,
|
clusterId: clusterStore.activeClusterId,
|
||||||
workspaceId: workspaceStore.currentWorkspace,
|
workspaceId: workspaceStore.currentWorkspace,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ interface Props {
|
|||||||
@observer
|
@observer
|
||||||
export class ClustersMenu extends React.Component<Props> {
|
export class ClustersMenu extends React.Component<Props> {
|
||||||
selectCluster = (cluster: Cluster) => {
|
selectCluster = (cluster: Cluster) => {
|
||||||
clusterStore.activeCluster = cluster.id;
|
clusterStore.activeClusterId = cluster.id;
|
||||||
console.log('load lens for cluster:', cluster)
|
console.log('load lens for cluster:', cluster)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ export class ClustersMenu extends React.Component<Props> {
|
|||||||
return (
|
return (
|
||||||
<div className={cssNames("ClustersMenu flex gaps column", className)}>
|
<div className={cssNames("ClustersMenu flex gaps column", className)}>
|
||||||
{clusters.map(cluster => {
|
{clusters.map(cluster => {
|
||||||
const isActive = cluster.id === clusterStore.activeCluster;
|
const isActive = cluster.id === clusterStore.activeClusterId;
|
||||||
return (
|
return (
|
||||||
<ClusterIcon
|
<ClusterIcon
|
||||||
key={cluster.id}
|
key={cluster.id}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user