1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/+user-management-roles-bindings/role-bindings.store.ts
Roman f8c111ddd8
Load k8s resources only for selected namespaces (#1918)
* loading k8s resources into stores per selected namespaces -- part 1

Signed-off-by: Roman <ixrock@gmail.com>

* loading k8s resources into stores per selected namespaces -- part 2
- fix: generating helm chart id

Signed-off-by: Roman <ixrock@gmail.com>

* loading k8s resources into stores per selected namespaces -- part 3

Signed-off-by: Roman <ixrock@gmail.com>

* fixes

Signed-off-by: Roman <ixrock@gmail.com>

* fixes / responding to comments

Signed-off-by: Roman <ixrock@gmail.com>

* chore / small fixes

Signed-off-by: Roman <ixrock@gmail.com>

* fixes & refactoring

Signed-off-by: Roman <ixrock@gmail.com>

* make lint happy

Signed-off-by: Roman <ixrock@gmail.com>

* reset store on loading error

Signed-off-by: Roman <ixrock@gmail.com>

* added new cluster method: cluster.isAllowedResource

Signed-off-by: Roman <ixrock@gmail.com>

* fix: loading namespaces optimizations

Signed-off-by: Roman <ixrock@gmail.com>

* fixes & refactoring

Signed-off-by: Roman <ixrock@gmail.com>
2021-01-22 13:18:46 +02:00

76 lines
2.3 KiB
TypeScript

import difference from "lodash/difference";
import uniqBy from "lodash/uniqBy";
import { clusterRoleBindingApi, IRoleBindingSubject, RoleBinding, roleBindingApi } from "../../api/endpoints";
import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
import { autobind } from "../../utils";
import { apiManager } from "../../api/api-manager";
@autobind()
export class RoleBindingsStore extends KubeObjectStore<RoleBinding> {
api = clusterRoleBindingApi;
subscribe() {
return super.subscribe([clusterRoleBindingApi, roleBindingApi]);
}
protected sortItems(items: RoleBinding[]) {
return super.sortItems(items, [
roleBinding => roleBinding.kind,
roleBinding => roleBinding.getName()
]);
}
protected loadItem(params: { name: string; namespace?: string }) {
if (params.namespace) return roleBindingApi.get(params);
return clusterRoleBindingApi.get(params);
}
protected async loadItems(params: KubeObjectStoreLoadingParams): Promise<RoleBinding[]> {
const items = await Promise.all([
super.loadItems({ ...params, api: clusterRoleBindingApi }),
super.loadItems({ ...params, api: roleBindingApi }),
]);
return items.flat();
}
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<RoleBinding>) {
if (params.namespace) {
return roleBindingApi.create(params, data);
} else {
return clusterRoleBindingApi.create(params, data);
}
}
async updateSubjects(params: {
roleBinding: RoleBinding;
addSubjects?: IRoleBindingSubject[];
removeSubjects?: IRoleBindingSubject[];
}) {
const { roleBinding, addSubjects, removeSubjects } = params;
const currentSubjects = roleBinding.getSubjects();
let newSubjects = currentSubjects;
if (addSubjects) {
newSubjects = uniqBy(currentSubjects.concat(addSubjects), ({ kind, name, namespace }) => {
return [kind, name, namespace].join("-");
});
} else if (removeSubjects) {
newSubjects = difference(currentSubjects, removeSubjects);
}
return this.update(roleBinding, {
roleRef: roleBinding.roleRef,
subjects: newSubjects
});
}
}
export const roleBindingsStore = new RoleBindingsStore();
apiManager.registerStore(roleBindingsStore, [
roleBindingApi,
clusterRoleBindingApi,
]);