Make request-api-resources flatter in implementation (#6802)

* Make request-api-resources flatter in implementation

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* More improvements to requestApiResources
- Also move files to better places

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Rename iter.pipeline as iter.chain

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-12-21 16:31:31 +02:00
1 parent 4dac9a8b2b
commit a83259b70a
15 files changed
+186 -97

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ import type { Logger } from "../logger";
import type { BroadcastMessage } from "../ipc/broadcast-message.injectable";
import type { LoadConfigfromFile } from "../kube-helpers/load-config-from-file.injectable";
import type { CanListResource, RequestNamespaceListPermissions, RequestNamespaceListPermissionsFor } from "./request-namespace-list-permissions.injectable";
import type { RequestApiResources } from "./request-api-resources.injectable";
import type { RequestApiResources } from "../../main/cluster/request-api-resources.injectable";
export interface ClusterDependencies {
readonly directoryForKubeConfigs: string;
@@ -1,83 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { V1APIGroupList, V1APIResourceList, V1APIVersions } from "@kubernetes/client-node";
import { getInjectable } from "@ogre-tools/injectable";
import k8SRequestInjectable from "../../main/k8s-request.injectable";
import loggerInjectable from "../logger.injectable";
import type { KubeApiResource } from "../rbac";
import type { Cluster } from "./cluster";
import plimit from "p-limit";
export type RequestApiResources = (cluster: Cluster) => Promise<KubeApiResource[]>;
interface KubeResourceListGroup {
group: string;
path: string;
}
const requestApiResourcesInjectable = getInjectable({
id: "request-api-resources",
instantiate: (di): RequestApiResources => {
const k8sRequest = di.inject(k8SRequestInjectable);
const logger = di.inject(loggerInjectable);
return async (cluster) => {
const apiLimit = plimit(5);
const kubeApiResources: KubeApiResource[] = [];
const resourceListGroups: KubeResourceListGroup[] = [];
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;
for (const resource of resources) {
kubeApiResources.push({
apiName: resource.name,
kind: resource.kind,
group,
namespaced: resource.namespaced,
});
}
})),
);
} catch (error) {
logger.error(`[LIST-API-RESOURCES]: failed to list api resources: ${error}`);
}
return kubeApiResources;
};
},
});
export default requestApiResourcesInjectable;
+7 -6
View File
@@ -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;
@@ -15,15 +15,16 @@ interface Iterator<T> {
join(sep?: string): string;
}
export function pipeline<T>(src: IterableIterator<T>): Iterator<T> {
export function chain<T>(src: IterableIterator<T>): Iterator<T> {
return {
filter: (fn) => pipeline(filter(src, fn)),
filterMap: (fn) => pipeline(filterMap(src, fn)),
map: (fn) => pipeline(map(src, fn)),
flatMap: (fn) => pipeline(flatMap(src, fn)),
filter: (fn) => chain(filter(src, fn)),
filterMap: (fn) => chain(filterMap(src, fn)),
map: (fn) => chain(map(src, fn)),
flatMap: (fn) => chain(flatMap(src, fn)),
find: (fn) => find(src, fn),
join: (sep) => join(src, sep),
collect: (fn) => fn(src),
[Symbol.iterator]: () => src,
};
}
@@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import plimit from "p-limit";
export type ConcurrencyLimiter = <Args extends any[], Res>(fn: (...args: Args) => Res) => (...args: Args) => Promise<Res>;
export function withConcurrencyLimit(limit: number): ConcurrencyLimiter {
const limiter = plimit(limit);
return fn => (...args) => limiter(() => fn(...args));
}