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

optimise Cluster.getAllowedResources()

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-12-21 20:04:15 +02:00
parent e8f36e97a3
commit e3156992cb

View File

@ -11,7 +11,7 @@ import { Kubectl } from "./kubectl";
import { KubeconfigManager } from "./kubeconfig-manager"; import { KubeconfigManager } from "./kubeconfig-manager";
import { loadConfig } from "../common/kube-helpers"; import { loadConfig } from "../common/kube-helpers";
import request, { RequestPromiseOptions } from "request-promise-native"; import request, { RequestPromiseOptions } from "request-promise-native";
import { apiResources } from "../common/rbac"; import { apiResources, KubeApiResource } from "../common/rbac";
import logger from "./logger"; import logger from "./logger";
import { VersionDetector } from "./cluster-detectors/version-detector"; import { VersionDetector } from "./cluster-detectors/version-detector";
import { detectorRegistry } from "./cluster-detectors/detector-registry"; import { detectorRegistry } from "./cluster-detectors/detector-registry";
@ -78,6 +78,7 @@ export class Cluster implements ClusterModel, ClusterState {
protected kubeconfigManager: KubeconfigManager; protected kubeconfigManager: KubeconfigManager;
protected eventDisposers: Function[] = []; protected eventDisposers: Function[] = [];
protected activated = false; protected activated = false;
private resourceAccessStatuses: Map<KubeApiResource, boolean> = new Map();
whenInitialized = when(() => this.initialized); whenInitialized = when(() => this.initialized);
whenReady = when(() => this.ready); whenReady = when(() => this.ready);
@ -379,6 +380,7 @@ export class Cluster implements ClusterModel, ClusterState {
this.accessible = false; this.accessible = false;
this.ready = false; this.ready = false;
this.activated = false; this.activated = false;
this.resourceAccessStatuses.clear();
this.pushState(); this.pushState();
} }
@ -484,6 +486,8 @@ export class Cluster implements ClusterModel, ClusterState {
this.metadata.version = versionData.value; this.metadata.version = versionData.value;
this.failureReason = null;
return ClusterStatus.AccessGranted; return ClusterStatus.AccessGranted;
} catch (error) { } catch (error) {
logger.error(`Failed to connect cluster "${this.contextName}": ${error}`); logger.error(`Failed to connect cluster "${this.contextName}": ${error}`);
@ -643,17 +647,26 @@ export class Cluster implements ClusterModel, ClusterState {
if (!this.allowedNamespaces.length) { if (!this.allowedNamespaces.length) {
return []; return [];
} }
const resourceAccessStatuses = await Promise.all(
apiResources.map(apiResource => this.canI({ const resources = apiResources.filter((resource) => this.resourceAccessStatuses.get(resource) === undefined);
resource: apiResource.resource,
group: apiResource.group, for (const apiResource of resources) {
verb: "list", for (const namespace of this.allowedNamespaces.slice(0, 10)) {
namespace: this.allowedNamespaces[0] if (!this.resourceAccessStatuses.get(apiResource)) {
})) const result = await this.canI({
); resource: apiResource.resource,
group: apiResource.group,
verb: "list",
namespace
});
this.resourceAccessStatuses.set(apiResource, result);
}
}
}
return apiResources return apiResources
.filter((resource, i) => resourceAccessStatuses[i]) .filter((resource) => this.resourceAccessStatuses.get(resource))
.map(apiResource => apiResource.resource); .map(apiResource => apiResource.resource);
} catch (error) { } catch (error) {
return []; return [];