From b14e77a616d188df5c18d534bb9c0ea9c7ce5cc9 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 17 Jun 2021 14:50:49 -0400 Subject: [PATCH] Fix non-legacy APIs not being retrieved correctly Signed-off-by: Sebastian Malton --- src/common/utils/extended-map.ts | 4 ++-- src/main/cluster.ts | 11 ++++++----- src/main/utils/api-resources.ts | 4 +++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/common/utils/extended-map.ts b/src/common/utils/extended-map.ts index 3014a4333a..fe92a8b502 100644 --- a/src/common/utils/extended-map.ts +++ b/src/common/utils/extended-map.ts @@ -47,7 +47,7 @@ export class ExtendedMap extends Map { */ strictSet(key: K, val: V): this { if (this.has(key)) { - throw new TypeError("Duplicate key in map"); + throw new TypeError(`Duplicate key in map: ${key}`); } return this.set(key, val); @@ -59,7 +59,7 @@ export class ExtendedMap extends Map { */ strictGet(key: K): V { if (!this.has(key)) { - throw new TypeError("key not in map"); + throw new TypeError(`Key not in map: ${key}`); } return this.get(key); diff --git a/src/main/cluster.ts b/src/main/cluster.ts index a1b4762b24..a68d92e222 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -522,19 +522,20 @@ export class Cluster implements ClusterModel, ClusterState { private isAllowedCheckers = new ExtendedObservableMap Promise>>(); private async getIsAllowedResourcesInNamespace(namespace: string): Promise> { - console.log("getIsAllowedResourcesInNamespace", Date.now()); const groups = await this.getApiResourceMap(); const isAllowed = new Set(); - for (const group of groups.values()) { - for (const versions of group.values()) { - for (const resource of versions.keys()) { + for (const [group, versions] of groups) { + for (const [version, resources] of versions) { + for (const resource of resources.keys()) { const canList = await this.canI({ resource, + version, + group, namespace, verb: "list", }); - + if (canList) { isAllowed.add(resource); } diff --git a/src/main/utils/api-resources.ts b/src/main/utils/api-resources.ts index b910bafd22..70ba831f13 100644 --- a/src/main/utils/api-resources.ts +++ b/src/main/utils/api-resources.ts @@ -45,6 +45,8 @@ type ResourceName = string; */ export type ApiResourceMap = Map>>; +const groupVersionRegex = /^((?.*)\/)?(?.*)$/; + /** * Get the list of all resources kubernetes knows about from the current cluster of `kc`. * @param kc The config of the cluster to get all resources of @@ -72,7 +74,7 @@ export async function getClusterResources(kc: KubeConfig, throttle = 10): Promis const res = new ExtendedMap>>(); for (const apiResourceList of apiResourceLists) { - const [group, version] = apiResourceList.groupVersion.split("/"); + const { groups: { group, version } } = apiResourceList.groupVersion.match(groupVersionRegex); const versions = res.getOrInsert(group, ExtendedMap.new); const resources = versions.getOrInsert(version, ExtendedMap.new);