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 { KubeApiResource } from "../rbac";
|
||||||
import type { Cluster } from "./cluster";
|
import type { Cluster } from "./cluster";
|
||||||
import plimit from "p-limit";
|
import plimit from "p-limit";
|
||||||
|
import { pipeline } from "../utils/iter";
|
||||||
|
|
||||||
export type RequestApiResources = (cluster: Cluster) => Promise<KubeApiResource[]>;
|
export type RequestApiResources = (cluster: Cluster) => Promise<KubeApiResource[]>;
|
||||||
|
|
||||||
@ -24,58 +25,49 @@ const requestApiResourcesInjectable = getInjectable({
|
|||||||
const k8sRequest = di.inject(k8SRequestInjectable);
|
const k8sRequest = di.inject(k8SRequestInjectable);
|
||||||
const logger = di.inject(loggerInjectable);
|
const logger = di.inject(loggerInjectable);
|
||||||
|
|
||||||
return async (cluster) => {
|
const requestApiVersions = async (cluster: Cluster) => (await k8sRequest(cluster, "/api") as V1APIVersions).versions;
|
||||||
const apiLimit = plimit(5);
|
const requestApisVersions = async (cluster: Cluster) => (await k8sRequest(cluster, "/apis") as V1APIGroupList).groups;
|
||||||
const kubeApiResources: KubeApiResource[] = [];
|
const limitingFor = (limit: plimit.Limit) => <Args extends any[], Res>(fn: (...args: Args) => Res) => (...args: Args) => limit(() => fn(...args));
|
||||||
const resourceListGroups: KubeResourceListGroup[] = [];
|
const requestKubeApiResourcesFor = (cluster: Cluster) => async ({ group, path }: KubeResourceListGroup): Promise<KubeApiResource[]> => {
|
||||||
|
|
||||||
try {
|
|
||||||
await Promise.all([
|
|
||||||
(async () => {
|
|
||||||
const { versions } = await k8sRequest(cluster, "/api") as V1APIVersions;
|
|
||||||
|
|
||||||
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;
|
const { resources } = await k8sRequest(cluster, path) as V1APIResourceList;
|
||||||
|
|
||||||
for (const resource of resources) {
|
return resources.map(resource => ({
|
||||||
kubeApiResources.push({
|
|
||||||
apiName: resource.name,
|
apiName: resource.name,
|
||||||
kind: resource.kind,
|
kind: resource.kind,
|
||||||
group,
|
group,
|
||||||
namespaced: resource.namespaced,
|
namespaced: resource.namespaced,
|
||||||
});
|
}));
|
||||||
}
|
};
|
||||||
|
|
||||||
|
return async (cluster) => {
|
||||||
|
const requestKubeApiResources = requestKubeApiResourcesFor(cluster);
|
||||||
|
const withApiLimit = limitingFor(plimit(5));
|
||||||
|
|
||||||
|
try {
|
||||||
|
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}`,
|
||||||
|
})),
|
||||||
|
];
|
||||||
|
|
||||||
|
const resources = await Promise.all(
|
||||||
|
resourceListGroups
|
||||||
|
.map(withApiLimit(requestKubeApiResources)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return resources.flat();
|
||||||
} 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 kubeApiResources;
|
return [];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
export type Falsey = false | 0 | "" | null | undefined;
|
export type Falsey = false | 0 | "" | null | undefined;
|
||||||
|
|
||||||
interface Iterator<T> {
|
interface Iterator<T> extends Iterable<T> {
|
||||||
filter(fn: (val: T) => unknown): Iterator<T>;
|
filter(fn: (val: T) => unknown): Iterator<T>;
|
||||||
filterMap<U>(fn: (val: T) => Falsey | U): Iterator<U>;
|
filterMap<U>(fn: (val: T) => Falsey | U): Iterator<U>;
|
||||||
find(fn: (val: T) => unknown): T | undefined;
|
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),
|
find: (fn) => find(src, fn),
|
||||||
join: (sep) => join(src, sep),
|
join: (sep) => join(src, sep),
|
||||||
collect: (fn) => fn(src),
|
collect: (fn) => fn(src),
|
||||||
|
[Symbol.iterator]: () => src,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user