mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make request-api-resources flatter in implementation
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
4dac9a8b2b
commit
9ebec670e8
@ -10,6 +10,7 @@ import loggerInjectable from "../logger.injectable";
|
||||
import type { KubeApiResource } from "../rbac";
|
||||
import type { Cluster } from "./cluster";
|
||||
import plimit from "p-limit";
|
||||
import { pipeline } from "../utils/iter";
|
||||
|
||||
export type RequestApiResources = (cluster: Cluster) => Promise<KubeApiResource[]>;
|
||||
|
||||
@ -24,58 +25,49 @@ const requestApiResourcesInjectable = getInjectable({
|
||||
const k8sRequest = di.inject(k8SRequestInjectable);
|
||||
const logger = di.inject(loggerInjectable);
|
||||
|
||||
const requestApiVersions = async (cluster: Cluster) => (await k8sRequest(cluster, "/api") as V1APIVersions).versions;
|
||||
const requestApisVersions = async (cluster: Cluster) => (await k8sRequest(cluster, "/apis") as V1APIGroupList).groups;
|
||||
const limitingFor = (limit: plimit.Limit) => <Args extends any[], Res>(fn: (...args: Args) => Res) => (...args: Args) => limit(() => fn(...args));
|
||||
const requestKubeApiResourcesFor = (cluster: Cluster) => async ({ group, path }: KubeResourceListGroup): Promise<KubeApiResource[]> => {
|
||||
const { resources } = await k8sRequest(cluster, path) as V1APIResourceList;
|
||||
|
||||
return resources.map(resource => ({
|
||||
apiName: resource.name,
|
||||
kind: resource.kind,
|
||||
group,
|
||||
namespaced: resource.namespaced,
|
||||
}));
|
||||
};
|
||||
|
||||
return async (cluster) => {
|
||||
const apiLimit = plimit(5);
|
||||
const kubeApiResources: KubeApiResource[] = [];
|
||||
const resourceListGroups: KubeResourceListGroup[] = [];
|
||||
const requestKubeApiResources = requestKubeApiResourcesFor(cluster);
|
||||
const withApiLimit = limitingFor(plimit(5));
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
(async () => {
|
||||
const { versions } = await k8sRequest(cluster, "/api") as V1APIVersions;
|
||||
const resourceListGroups: KubeResourceListGroup[] = [
|
||||
...(await requestApiVersions(cluster))
|
||||
.map(version => ({
|
||||
group: version,
|
||||
path: `/api/${version}`,
|
||||
})),
|
||||
...pipeline((await requestApisVersions(cluster)).values())
|
||||
.filterMap(group => group.preferredVersion?.groupVersion && ({
|
||||
group: group.name,
|
||||
path: `/apis/${group.preferredVersion.groupVersion}`,
|
||||
})),
|
||||
];
|
||||
|
||||
for (const version of versions) {
|
||||
resourceListGroups.push({
|
||||
group: version,
|
||||
path: `/api/${version}`,
|
||||
});
|
||||
}
|
||||
})(),
|
||||
(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(
|
||||
resourceListGroups.map(({ group, path }) => apiLimit(async () => {
|
||||
const { resources } = await k8sRequest(cluster, path) as V1APIResourceList;
|
||||
|
||||
for (const resource of resources) {
|
||||
kubeApiResources.push({
|
||||
apiName: resource.name,
|
||||
kind: resource.kind,
|
||||
group,
|
||||
namespaced: resource.namespaced,
|
||||
});
|
||||
}
|
||||
})),
|
||||
const resources = await Promise.all(
|
||||
resourceListGroups
|
||||
.map(withApiLimit(requestKubeApiResources)),
|
||||
);
|
||||
|
||||
return resources.flat();
|
||||
} catch (error) {
|
||||
logger.error(`[LIST-API-RESOURCES]: failed to list api resources: ${error}`);
|
||||
}
|
||||
|
||||
return kubeApiResources;
|
||||
return [];
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
export type Falsey = false | 0 | "" | null | undefined;
|
||||
|
||||
interface Iterator<T> {
|
||||
interface Iterator<T> extends Iterable<T> {
|
||||
filter(fn: (val: T) => unknown): Iterator<T>;
|
||||
filterMap<U>(fn: (val: T) => Falsey | U): Iterator<U>;
|
||||
find(fn: (val: T) => unknown): T | undefined;
|
||||
@ -24,6 +24,7 @@ export function pipeline<T>(src: IterableIterator<T>): Iterator<T> {
|
||||
find: (fn) => find(src, fn),
|
||||
join: (sep) => join(src, sep),
|
||||
collect: (fn) => fn(src),
|
||||
[Symbol.iterator]: () => src,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user