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