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

Refactoring ListApiResources

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-25 12:26:35 -05:00
parent c1c44471a3
commit 40a7bf8830

View File

@ -9,11 +9,9 @@ import type {
V1APIVersions, V1APIVersions,
} from "@kubernetes/client-node"; } from "@kubernetes/client-node";
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { K8sRequest } from "../../main/k8s-request.injectable";
import k8SRequestInjectable from "../../main/k8s-request.injectable"; import k8SRequestInjectable from "../../main/k8s-request.injectable";
import type { Logger } from "../logger";
import loggerInjectable from "../logger.injectable"; import loggerInjectable from "../logger.injectable";
import type { KubeApiResource, KubeResource } from "../rbac"; import type { KubeApiResource } from "../rbac";
import type { Cluster } from "./cluster"; import type { Cluster } from "./cluster";
import plimit from "p-limit"; import plimit from "p-limit";
@ -24,67 +22,77 @@ export type RequestListApiResources = () => Promise<KubeApiResource[]>;
*/ */
export type ListApiResources = (cluster: Cluster) => RequestListApiResources; export type ListApiResources = (cluster: Cluster) => RequestListApiResources;
interface Dependencies { interface KubeResourceListGroup {
logger: Logger; group: string;
k8sRequest: K8sRequest; path: string;
} }
const listApiResources = ({ k8sRequest, logger }: Dependencies): ListApiResources => { const listApiResourcesInjectable = getInjectable({
id: "list-api-resources",
instantiate: (di): ListApiResources => {
const k8sRequest = di.inject(k8SRequestInjectable);
const logger = di.inject(loggerInjectable);
return (cluster) => { return (cluster) => {
const clusterRequest = (path: string) => k8sRequest(cluster, path);
const apiLimit = plimit(5); const apiLimit = plimit(5);
return async () => { return async () => {
const resources: KubeApiResource[] = []; const kubeApiResources: KubeApiResource[] = [];
const resourceListGroups: KubeResourceListGroup[] = [];
try { try {
const resourceListGroups:{ group:string;path:string }[] = []; await Promise.all([
(async () => {
const { versions } = await k8sRequest(cluster, "/api") as V1APIVersions;
await Promise.all( for (const version of versions) {
[ resourceListGroups.push({
clusterRequest("/api").then((response:V1APIVersions)=>response.versions.forEach(version => resourceListGroups.push({ group:version, path:`/api/${version}` }))), group: version,
clusterRequest("/apis").then((response:V1APIGroupList) => response.groups.forEach(group => { path: `/api/${version}`,
const preferredVersion = group.preferredVersion?.groupVersion; });
if (preferredVersion) {
resourceListGroups.push({ group:group.name, path:`/apis/${preferredVersion}` });
} }
})), })(),
], (async () => {
); const { groups } = await k8sRequest(cluster, "/apis") as V1APIGroupList;
for (const { preferredVersion, name } of groups) {
const { groupVersion } = preferredVersion ?? {};
if (groupVersion) {
resourceListGroups.push({
group: name,
path: `/apis/${groupVersion}`,
});
}
}
})(),
]);
await Promise.all( await Promise.all(
resourceListGroups.map(({ group, path }) => apiLimit(async () => { resourceListGroups.map(({ group, path }) => apiLimit(async () => {
const apiResources:V1APIResourceList = await clusterRequest(path); const { resources } = await k8sRequest(cluster, path) as V1APIResourceList;
if (apiResources.resources) { for (const resource of resources) {
resources.push( if (!resource.verbs.includes("list")) {
...apiResources.resources.filter(resource => resource.verbs.includes("list")).map((resource) => ({ continue;
apiName: resource.name as KubeResource, }
kubeApiResources.push({
apiName: resource.name,
kind: resource.kind, kind: resource.kind,
group, group,
})), namespaced: resource.namespaced,
); });
} }
}), })),
),
); );
} catch (error) { } catch (error) {
logger.error(`[LIST-API-RESOURCES]: failed to list api resources: ${error}`); logger.error(`[LIST-API-RESOURCES]: failed to list api resources: ${error}`);
} }
return resources; return kubeApiResources;
}; };
}; };
};
const listApiResourcesInjectable = getInjectable({
id: "list-api-resources",
instantiate: (di) => {
const k8sRequest = di.inject(k8SRequestInjectable);
const logger = di.inject(loggerInjectable);
return listApiResources({ k8sRequest, logger });
}, },
}); });