mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Support filtering catalog entities (#3647)
* Support filtering catalog entities - This allows extensions and OpenLens to restrict which of the entities the sources have provied are "visible" to the rest of Lens Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * switch to filtering only for catalog view Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix test Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Add test to iter reduce Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> Co-authored-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
parent
7981e79cbd
commit
6fdb2f0b58
34
src/common/utils/__tests__/iter.test.ts
Normal file
34
src/common/utils/__tests__/iter.test.ts
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { reduce } from "../iter";
|
||||
|
||||
describe("iter", () => {
|
||||
describe("reduce", () => {
|
||||
it("can reduce a value", () => {
|
||||
expect(reduce([1, 2, 3], (acc: number[], current: number) => [current, ...acc], [0])).toEqual([3, 2, 1, 0]);
|
||||
});
|
||||
|
||||
it("can reduce an empty iterable", () => {
|
||||
expect(reduce([], (acc: number[], current: number) => [acc[0] + current], [])).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -156,3 +156,22 @@ export function find<T>(src: Iterable<T>, match: (i: T) => any): T | undefined {
|
||||
|
||||
return void 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over `src` calling `reducer` with the previous produced value and the current
|
||||
* yielded value until `src` is exausted. Then return the final value.
|
||||
* @param src The value to iterate over
|
||||
* @param reducer A function for producing the next item from an accumilation and the current item
|
||||
* @param initial The initial value for the iteration
|
||||
*/
|
||||
export function reduce<T, R>(src: Iterable<T>, reducer: (acc: Iterable<R>, cur: T) => Iterable<R>, initial: Iterable<R>): Iterable<R>;
|
||||
|
||||
export function reduce<T, R = T>(src: Iterable<T>, reducer: (acc: R, cur: T) => R, initial: R): R {
|
||||
let acc = initial;
|
||||
|
||||
for (const item of src) {
|
||||
acc = reducer(acc, item);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}
|
||||
|
||||
@ -21,9 +21,11 @@
|
||||
|
||||
import type * as registries from "./registries";
|
||||
import type { Cluster } from "../main/cluster";
|
||||
import { LensExtension } from "./lens-extension";
|
||||
import { Disposers, LensExtension } from "./lens-extension";
|
||||
import { getExtensionPageUrl } from "./registries/page-registry";
|
||||
import type { CatalogEntity } from "../common/catalog";
|
||||
import type { Disposer } from "../common/utils";
|
||||
import { catalogEntityRegistry, EntityFilter } from "../renderer/api/catalog-entity-registry";
|
||||
|
||||
export class LensRendererExtension extends LensExtension {
|
||||
globalPages: registries.PageRegistration[] = [];
|
||||
@ -59,4 +61,17 @@ export class LensRendererExtension extends LensExtension {
|
||||
async isEnabledForCluster(cluster: Cluster): Promise<Boolean> {
|
||||
return (void cluster) || true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a filtering function for the catalog. This will be removed if the extension is disabled.
|
||||
* @param fn The function which should return a truthy value for those entities which should be kepted
|
||||
* @returns A function to clean up the filter
|
||||
*/
|
||||
addCatalogFilter(fn: EntityFilter): Disposer {
|
||||
const dispose = catalogEntityRegistry.addCatalogFilter(fn);
|
||||
|
||||
this[Disposers].push(dispose);
|
||||
|
||||
return dispose;
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,24 +43,20 @@ export class CatalogEntityRegistry {
|
||||
}
|
||||
|
||||
@computed get items(): CatalogEntity[] {
|
||||
const allItems = Array.from(iter.flatMap(this.sources.values(), source => source.get()));
|
||||
|
||||
return allItems.filter((entity) => this.categoryRegistry.getCategoryForEntity(entity) !== undefined);
|
||||
return Array.from(
|
||||
iter.filter(
|
||||
iter.flatMap(this.sources.values(), source => source.get()),
|
||||
entity => this.categoryRegistry.getCategoryForEntity(entity)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
getById<T extends CatalogEntity>(id: string): T | undefined {
|
||||
const item = this.items.find((entity) => entity.metadata.uid === id);
|
||||
|
||||
if (item) return item as T;
|
||||
|
||||
|
||||
return undefined;
|
||||
return this.items.find((entity) => entity.metadata.uid === id) as T | undefined;
|
||||
}
|
||||
|
||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
||||
const items = this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
|
||||
|
||||
return items as T[];
|
||||
return this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind) as T[];
|
||||
}
|
||||
|
||||
getItemsByEntityClass<T extends CatalogEntity>({ apiVersion, kind }: CatalogEntityKindData): T[] {
|
||||
|
||||
@ -23,7 +23,8 @@ import { CatalogEntityRegistry } from "../catalog-entity-registry";
|
||||
import "../../../common/catalog-entities";
|
||||
import { catalogCategoryRegistry } from "../../../common/catalog/catalog-category-registry";
|
||||
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "../catalog-entity";
|
||||
import { WebLink } from "../../../common/catalog-entities";
|
||||
import { KubernetesCluster, WebLink } from "../../../common/catalog-entities";
|
||||
import { observable } from "mobx";
|
||||
|
||||
class TestCatalogEntityRegistry extends CatalogEntityRegistry {
|
||||
replaceItems(items: Array<CatalogEntityData & CatalogEntityKindData>) {
|
||||
@ -51,6 +52,49 @@ class FooBarCategory extends CatalogCategory {
|
||||
}
|
||||
};
|
||||
}
|
||||
const entity = new WebLink({
|
||||
metadata: {
|
||||
uid: "test",
|
||||
name: "test-link",
|
||||
source: "test",
|
||||
labels: {}
|
||||
},
|
||||
spec: {
|
||||
url: "https://k8slens.dev"
|
||||
},
|
||||
status: {
|
||||
phase: "available"
|
||||
}
|
||||
});
|
||||
const entity2 = new WebLink({
|
||||
metadata: {
|
||||
uid: "test2",
|
||||
name: "test-link",
|
||||
source: "test",
|
||||
labels: {}
|
||||
},
|
||||
spec: {
|
||||
url: "https://k8slens.dev"
|
||||
},
|
||||
status: {
|
||||
phase: "available"
|
||||
}
|
||||
});
|
||||
const entitykc = new KubernetesCluster({
|
||||
metadata: {
|
||||
uid: "test3",
|
||||
name: "test-link",
|
||||
source: "test",
|
||||
labels: {}
|
||||
},
|
||||
spec: {
|
||||
kubeconfigPath: "",
|
||||
kubeconfigContext: "",
|
||||
},
|
||||
status: {
|
||||
phase: "connected"
|
||||
}
|
||||
});
|
||||
|
||||
describe("CatalogEntityRegistry", () => {
|
||||
describe("updateItems", () => {
|
||||
@ -250,4 +294,25 @@ describe("CatalogEntityRegistry", () => {
|
||||
catalogCategoryRegistry.add(new FooBarCategory());
|
||||
expect(catalog.items.length).toBe(1);
|
||||
});
|
||||
|
||||
it("does not return items that are filtered out", () => {
|
||||
const source = observable.array([entity, entity2, entitykc]);
|
||||
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||
|
||||
catalog.replaceItems(source);
|
||||
|
||||
expect(catalog.items.length).toBe(3);
|
||||
expect(catalog.filteredItems.length).toBe(3);
|
||||
|
||||
const d = catalog.addCatalogFilter(entity => entity.kind === KubernetesCluster.kind);
|
||||
|
||||
expect(catalog.items.length).toBe(3);
|
||||
expect(catalog.filteredItems.length).toBe(1);
|
||||
|
||||
// Remove filter
|
||||
d();
|
||||
|
||||
expect(catalog.items.length).toBe(3);
|
||||
expect(catalog.filteredItems.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
@ -25,10 +25,17 @@ import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegis
|
||||
import "../../common/catalog-entities";
|
||||
import type { Cluster } from "../../main/cluster";
|
||||
import { ClusterStore } from "../../common/cluster-store";
|
||||
import { Disposer, iter } from "../utils";
|
||||
import { once } from "lodash";
|
||||
|
||||
export type EntityFilter = (entity: CatalogEntity) => any;
|
||||
|
||||
export class CatalogEntityRegistry {
|
||||
@observable.ref activeEntity: CatalogEntity;
|
||||
protected _entities = observable.map<string, CatalogEntity>([], { deep: true });
|
||||
protected filters = observable.set<EntityFilter>([], {
|
||||
deep: false,
|
||||
});
|
||||
|
||||
/**
|
||||
* Buffer for keeping entities that don't yet have CatalogCategory synced
|
||||
@ -95,27 +102,56 @@ export class CatalogEntityRegistry {
|
||||
return Array.from(this._entities.values());
|
||||
}
|
||||
|
||||
@computed get entities(): Map<string, CatalogEntity> {
|
||||
this.processRawEntities();
|
||||
@computed get filteredItems() {
|
||||
return Array.from(
|
||||
iter.reduce(
|
||||
this.filters,
|
||||
iter.filter,
|
||||
this.items,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return this._entities;
|
||||
@computed get entities(): Map<string, CatalogEntity> {
|
||||
return new Map(
|
||||
this.items.map(entity => [entity.getId(), entity])
|
||||
);
|
||||
}
|
||||
|
||||
@computed get filteredEntities(): Map<string, CatalogEntity> {
|
||||
return new Map(
|
||||
this.filteredItems.map(entity => [entity.getId(), entity])
|
||||
);
|
||||
}
|
||||
|
||||
getById<T extends CatalogEntity>(id: string) {
|
||||
return this.entities.get(id) as T;
|
||||
}
|
||||
|
||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
||||
const items = this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
|
||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string, { filtered = false } = {}): T[] {
|
||||
const byApiKind = (item: CatalogEntity) => item.apiVersion === apiVersion && item.kind === kind;
|
||||
const entities = filtered ? this.filteredItems : this.items;
|
||||
|
||||
return items as T[];
|
||||
return entities.filter(byApiKind) as T[];
|
||||
}
|
||||
|
||||
getItemsForCategory<T extends CatalogEntity>(category: CatalogCategory): T[] {
|
||||
const supportedVersions = category.spec.versions.map((v) => `${category.spec.group}/${v.name}`);
|
||||
const items = this.items.filter((item) => supportedVersions.includes(item.apiVersion) && item.kind === category.spec.names.kind);
|
||||
getItemsForCategory<T extends CatalogEntity>(category: CatalogCategory, { filtered = false } = {}): T[] {
|
||||
const supportedVersions = new Set(category.spec.versions.map((v) => `${category.spec.group}/${v.name}`));
|
||||
const byApiVersionKind = (item: CatalogEntity) => supportedVersions.has(item.apiVersion) && item.kind === category.spec.names.kind;
|
||||
const entities = filtered ? this.filteredItems : this.items;
|
||||
|
||||
return items as T[];
|
||||
return entities.filter(byApiVersionKind) as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new filter to the set of item filters
|
||||
* @param fn The function that should return a truthy value if that entity should be sent currently "active"
|
||||
* @returns A function to remove that filter
|
||||
*/
|
||||
addCatalogFilter(fn: EntityFilter): Disposer {
|
||||
this.filters.add(fn);
|
||||
|
||||
return once(() => void this.filters.delete(fn));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -129,10 +129,10 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem<CatalogEntit
|
||||
|
||||
@computed get entities() {
|
||||
if (!this.activeCategory) {
|
||||
return catalogEntityRegistry.items.map(entity => new CatalogEntityItem(entity));
|
||||
return catalogEntityRegistry.filteredItems.map(entity => new CatalogEntityItem(entity));
|
||||
}
|
||||
|
||||
return catalogEntityRegistry.getItemsForCategory(this.activeCategory).map(entity => new CatalogEntityItem(entity));
|
||||
return catalogEntityRegistry.getItemsForCategory(this.activeCategory, { filtered: true }).map(entity => new CatalogEntityItem(entity));
|
||||
}
|
||||
|
||||
@computed get selectedItem() {
|
||||
|
||||
@ -52,7 +52,7 @@ export class HotbarMenu extends React.Component<Props> {
|
||||
return null;
|
||||
}
|
||||
|
||||
return item ? catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item.entity.uid) : null;
|
||||
return catalogEntityRegistry.getById(item?.entity.uid) ?? null;
|
||||
}
|
||||
|
||||
onDragEnd(result: DropResult) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user