Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: frontend: BindingList: Make multi cluster aware #2438

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions frontend/src/components/role/BindingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import React from 'react';
import { useTranslation } from 'react-i18next';
import ClusterRoleBinding from '../../lib/k8s/clusterRoleBinding';
import RoleBinding from '../../lib/k8s/roleBinding';
import { useErrorState } from '../../lib/util';
import { combineClusterListErrors, flattenClusterListItems, getClusterGroup } from '../../lib/util';
import { Link } from '../common';
import LabelListItem from '../common/LabelListItem';
import ResourceListView from '../common/Resource/ResourceListView';

interface RoleBindingDict {
[kind: string]: RoleBinding[] | null;
}

function RoleLink(props: { role: string; namespace?: string }) {
const { role, namespace } = props;

Expand All @@ -30,44 +26,36 @@ function RoleLink(props: { role: string; namespace?: string }) {
}

export default function RoleBindingList() {
const [bindings, setBindings] = React.useState<RoleBindingDict | null>(null);
const [roleBindingError, onRoleBindingError] = useErrorState(setupRoleBindings);
const [clusterRoleBindingError, onClusterRoleBindingError] =
useErrorState(setupClusterRoleBindings);
const { t } = useTranslation(['glossary', 'translation']);
const [roleBindings, roleBindingsError] = RoleBinding.useListPerCluster();
const [clusterRoleBindings, clusterRoleBindingsError] = ClusterRoleBinding.useListPerCluster();
const clusters = getClusterGroup();

function setRoleBindings(newBindings: RoleBinding[] | null, kind: string) {
setBindings(currentBindings => ({ ...currentBindings, [kind]: newBindings }));
}
const bindings = React.useMemo(() => {
return flattenClusterListItems(roleBindings, clusterRoleBindings);
}, [roleBindings, clusterRoleBindings]);

function setupRoleBindings(newBindings: RoleBinding[] | null) {
setRoleBindings(newBindings, 'RoleBinding');
}
const bindingErrors = React.useMemo(() => {
return combineClusterListErrors(roleBindingsError, clusterRoleBindingsError);
}, [roleBindingsError, clusterRoleBindingsError]);

function setupClusterRoleBindings(newBindings: RoleBinding[] | null) {
setRoleBindings(newBindings, 'ClusterRoleBinding');
}
const { t } = useTranslation(['glossary', 'translation']);

function getJointItems() {
if (!bindings) {
return null;
}

let joint: RoleBinding[] = [];
let hasItems = false;
for (const items of Object.values(bindings as object)) {
if (items !== null) {
joint = joint.concat(items);
hasItems = true;
}
for (const items of Object.values(bindings)) {
joint = joint.concat(items);
}

return hasItems ? joint : null;
return joint.length > 0 ? joint : null;
}

function getErrorMessage() {
if (getJointItems() === null) {
return RoleBinding.getErrorMessage(roleBindingError || clusterRoleBindingError);
if (Object.values(bindingErrors || {}).length === clusters.length && clusters.length > 1) {
return RoleBinding.getErrorMessage(Object.values(bindingErrors!)[0]);
}

return null;
Expand All @@ -93,13 +81,13 @@ export default function RoleBindingList() {
};
}

RoleBinding.useApiList(setupRoleBindings, onRoleBindingError);
ClusterRoleBinding.useApiList(setupClusterRoleBindings, onClusterRoleBindingError);
const isMultiCluster = clusters.length > 1;

return (
<ResourceListView
title={t('glossary|Role Bindings')}
errorMessage={getErrorMessage()}
clusterErrors={isMultiCluster ? bindingErrors : null}
columns={[
'type',
'name',
Expand Down Expand Up @@ -179,6 +167,7 @@ export default function RoleBindingList() {
),
sort: sortBindings('Service Accounts'),
},
'cluster',
'age',
]}
data={getJointItems()}
Expand Down
Loading