Skip to content

Commit

Permalink
Merge pull request #2438 from headlamp-k8s/binding-list-multi
Browse files Browse the repository at this point in the history
frontend: BindingList: Make multi cluster aware
  • Loading branch information
illume authored Nov 18, 2024
2 parents 483c2f5 + bb429eb commit 316c1df
Showing 1 changed file with 17 additions and 38 deletions.
55 changes: 17 additions & 38 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, 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,28 @@ 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 { items: roles, clusterErrors: rolesErrors } = RoleBinding.useList();
const { items: clusterRoles, clusterErrors: clusterRolesErrors } = ClusterRoleBinding.useList();
const clusters = getClusterGroup();

function setRoleBindings(newBindings: RoleBinding[] | null, kind: string) {
setBindings(currentBindings => ({ ...currentBindings, [kind]: newBindings }));
}

function setupRoleBindings(newBindings: RoleBinding[] | null) {
setRoleBindings(newBindings, 'RoleBinding');
}
const isMultiCluster = clusters.length > 1;

function setupClusterRoleBindings(newBindings: RoleBinding[] | null) {
setRoleBindings(newBindings, 'ClusterRoleBinding');
}

function getJointItems() {
if (!bindings) {
const allRoles = React.useMemo(() => {
if (roles === null && clusterRoles === null) {
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;
}
}
return roles ? roles.concat(clusterRoles || []) : clusterRoles;
}, [roles, clusterRoles]);

return hasItems ? joint : null;
}
const allErrors = React.useMemo(() => {
return combineClusterListErrors(rolesErrors || null, clusterRolesErrors || null);
}, [rolesErrors, clusterRolesErrors]);

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

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

RoleBinding.useApiList(setupRoleBindings, onRoleBindingError);
ClusterRoleBinding.useApiList(setupClusterRoleBindings, onClusterRoleBindingError);

return (
<ResourceListView
title={t('glossary|Role Bindings')}
errorMessage={getErrorMessage()}
clusterErrors={isMultiCluster ? allErrors : null}
columns={[
'type',
'name',
Expand All @@ -116,6 +94,7 @@ export default function RoleBindingList() {
t('translation|All namespaces')
),
},
'cluster',
{
id: 'role',
label: t('glossary|Role'),
Expand Down Expand Up @@ -181,7 +160,7 @@ export default function RoleBindingList() {
},
'age',
]}
data={getJointItems()}
data={allRoles}
id="headlamp-rolebindings"
/>
);
Expand Down

0 comments on commit 316c1df

Please sign in to comment.