refactor and get unused cluster roles

pull/12297/head
testA113 2024-10-05 22:24:43 +13:00
parent 61bea1b06d
commit 2d57a01245
12 changed files with 87 additions and 44 deletions

View File

@ -20,8 +20,8 @@ import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSe
import { ClusterRoleBinding } from './types'; import { ClusterRoleBinding } from './types';
import { columns } from './columns'; import { columns } from './columns';
import { useGetClusterRoleBindingsQuery } from './queries/useGetClusterRoleBindingsQuery'; import { useClusterRoleBindings } from './queries/useClusterRoleBindings';
import { useDeleteClusterRoleBindingsMutation } from './queries/useDeleteClusterRoleBindingsMutation'; import { useDeleteClusterRoleBindings } from './queries/useDeleteClusterRoleBindings';
const storageKey = 'clusterRoleBindings'; const storageKey = 'clusterRoleBindings';
const settingsStore = createStore(storageKey); const settingsStore = createStore(storageKey);
@ -29,12 +29,9 @@ const settingsStore = createStore(storageKey);
export function ClusterRoleBindingsDatatable() { export function ClusterRoleBindingsDatatable() {
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const tableState = useTableState(settingsStore, storageKey); const tableState = useTableState(settingsStore, storageKey);
const clusterRoleBindingsQuery = useGetClusterRoleBindingsQuery( const clusterRoleBindingsQuery = useClusterRoleBindings(environmentId, {
environmentId, autoRefreshRate: tableState.autoRefreshRate * 1000,
{ });
autoRefreshRate: tableState.autoRefreshRate * 1000,
}
);
const filteredClusterRoleBindings = useMemo( const filteredClusterRoleBindings = useMemo(
() => () =>
@ -102,7 +99,7 @@ type TableActionsProps = {
function TableActions({ selectedItems }: TableActionsProps) { function TableActions({ selectedItems }: TableActionsProps) {
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const deleteClusterRoleBindingsMutation = const deleteClusterRoleBindingsMutation =
useDeleteClusterRoleBindingsMutation(environmentId); useDeleteClusterRoleBindings(environmentId);
const router = useRouter(); const router = useRouter();
async function handleRemoveClick(roles: SelectedRole[]) { async function handleRemoveClick(roles: SelectedRole[]) {

View File

@ -9,7 +9,7 @@ import { ClusterRoleBinding } from '../types';
import { queryKeys } from './query-keys'; import { queryKeys } from './query-keys';
export function useGetClusterRoleBindingsQuery( export function useClusterRoleBindings(
environmentId: EnvironmentId, environmentId: EnvironmentId,
options?: { autoRefreshRate?: number } options?: { autoRefreshRate?: number }
) { ) {

View File

@ -6,9 +6,7 @@ import { EnvironmentId } from '@/react/portainer/environments/types';
import { queryKeys } from './query-keys'; import { queryKeys } from './query-keys';
export function useDeleteClusterRoleBindingsMutation( export function useDeleteClusterRoleBindings(environmentId: EnvironmentId) {
environmentId: EnvironmentId
) {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation(deleteClusterRoleBindings, { return useMutation(deleteClusterRoleBindings, {
...withInvalidate(queryClient, [queryKeys.list(environmentId)]), ...withInvalidate(queryClient, [queryKeys.list(environmentId)]),

View File

@ -15,8 +15,12 @@ import { LoadingButton } from '@@/buttons';
import { useTableState } from '@@/datatables/useTableState'; import { useTableState } from '@@/datatables/useTableState';
import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings'; import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings';
import { useClusterRoleBindings } from '../ClusterRoleBindingsDatatable/queries/useClusterRoleBindings';
import { useRoleBindings } from '../../RolesView/RoleBindingsDatatable/queries/useRoleBindings';
import { ClusterRoleBinding } from '../ClusterRoleBindingsDatatable/types';
import { RoleBinding } from '../../RolesView/RoleBindingsDatatable/types';
import { ClusterRole } from './types'; import { ClusterRole, ClusterRoleRowData } from './types';
import { columns } from './columns'; import { columns } from './columns';
import { useClusterRoles } from './queries/useClusterRoles'; import { useClusterRoles } from './queries/useClusterRoles';
import { useDeleteClusterRoles } from './queries/useDeleteClusterRoles'; import { useDeleteClusterRoles } from './queries/useDeleteClusterRoles';
@ -27,26 +31,41 @@ const settingsStore = createStore(storageKey);
export function ClusterRolesDatatable() { export function ClusterRolesDatatable() {
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const tableState = useTableState(settingsStore, storageKey); const tableState = useTableState(settingsStore, storageKey);
const clusterRolesQuery = useClusterRoles(environmentId, { const clusterRolesQuery = useClusterRoles(environmentId, {
autoRefreshRate: tableState.autoRefreshRate * 1000, autoRefreshRate: tableState.autoRefreshRate * 1000,
}); });
const clusterRoleBindingsQuery = useClusterRoleBindings(environmentId);
const roleBindingsQuery = useRoleBindings(environmentId);
const clusterRolesWithUnusedFlag = useClusterRolesWithUnusedFlag(
clusterRolesQuery.data,
clusterRoleBindingsQuery.data,
roleBindingsQuery.data
);
const filteredClusterRoles = useMemo(
() =>
clusterRolesWithUnusedFlag.filter(
(cr) => tableState.showSystemResources || !cr.isSystem
),
[clusterRolesWithUnusedFlag, tableState.showSystemResources]
);
const isLoading =
clusterRolesQuery.isLoading ||
clusterRoleBindingsQuery.isLoading ||
roleBindingsQuery.isLoading;
const { authorized: isAuthorizedToAddEdit } = useAuthorizations([ const { authorized: isAuthorizedToAddEdit } = useAuthorizations([
'K8sClusterRolesW', 'K8sClusterRolesW',
]); ]);
const filteredClusterRoles = useMemo(
() =>
clusterRolesQuery.data?.filter(
(cr) => tableState.showSystemResources || !cr.isSystem
),
[clusterRolesQuery.data, tableState.showSystemResources]
);
return ( return (
<Datatable <Datatable
dataset={filteredClusterRoles || []} dataset={filteredClusterRoles || []}
columns={columns} columns={columns}
isLoading={clusterRolesQuery.isLoading} isLoading={isLoading}
settingsManager={tableState} settingsManager={tableState}
emptyContentLabel="No supported cluster roles found" emptyContentLabel="No supported cluster roles found"
title="Cluster Roles" title="Cluster Roles"
@ -149,3 +168,38 @@ function TableActions({ selectedItems }: TableActionsProps) {
</Authorized> </Authorized>
); );
} }
// Updated custom hook
function useClusterRolesWithUnusedFlag(
clusterRoles?: ClusterRole[],
clusterRoleBindings?: ClusterRoleBinding[],
roleBindings?: RoleBinding[]
): ClusterRoleRowData[] {
return useMemo(() => {
if (!clusterRoles || !clusterRoleBindings || !roleBindings) {
return [];
}
const usedRoleNames = new Set<string>();
// Check ClusterRoleBindings
clusterRoleBindings.forEach((binding) => {
if (binding.roleRef.kind === 'ClusterRole') {
usedRoleNames.add(binding.roleRef.name);
}
});
// Check RoleBindings
roleBindings.forEach((binding) => {
if (binding.roleRef.kind === 'ClusterRole') {
usedRoleNames.add(binding.roleRef.name);
}
});
// Mark cluster roles as unused if they're not in the usedRoleNames set
return clusterRoles.map((clusterRole) => ({
...clusterRole,
isUnused: !usedRoleNames.has(clusterRole.name) && !clusterRole.isSystem,
}));
}, [clusterRoles, clusterRoleBindings, roleBindings]);
}

View File

@ -1,5 +1,5 @@
import { createColumnHelper } from '@tanstack/react-table'; import { createColumnHelper } from '@tanstack/react-table';
import { ClusterRole } from '../types'; import { ClusterRoleRowData } from '../types';
export const columnHelper = createColumnHelper<ClusterRole>(); export const columnHelper = createColumnHelper<ClusterRoleRowData>();

View File

@ -1,4 +1,5 @@
import { SystemBadge } from '@@/Badge/SystemBadge'; import { SystemBadge } from '@@/Badge/SystemBadge';
import { UnusedBadge } from '@@/Badge/UnusedBadge';
import { columnHelper } from './helper'; import { columnHelper } from './helper';
@ -17,6 +18,7 @@ export const name = columnHelper.accessor(
<div className="flex gap-2"> <div className="flex gap-2">
{row.original.name} {row.original.name}
{row.original.isSystem && <SystemBadge />} {row.original.isSystem && <SystemBadge />}
{row.original.isUnused && <UnusedBadge />}
</div> </div>
), ),
} }

View File

@ -5,6 +5,10 @@ export type ClusterRole = {
isSystem: boolean; isSystem: boolean;
}; };
export type ClusterRoleRowData = ClusterRole & {
isUnused: boolean;
};
export type DeleteRequestPayload = { export type DeleteRequestPayload = {
clusterRoles: string[]; clusterRoles: string[];
}; };

View File

@ -25,7 +25,7 @@ import {
import { RoleBinding } from './types'; import { RoleBinding } from './types';
import { columns } from './columns'; import { columns } from './columns';
import { useAllRoleBindings } from './queries/useAllRoleBindings'; import { useRoleBindings } from './queries/useRoleBindings';
import { useDeleteRoleBindings } from './queries/useDeleteRoleBindings'; import { useDeleteRoleBindings } from './queries/useDeleteRoleBindings';
const storageKey = 'roleBindings'; const storageKey = 'roleBindings';
@ -42,7 +42,7 @@ export function RoleBindingsDatatable() {
...filteredColumnsSettings(set), ...filteredColumnsSettings(set),
}) })
); );
const roleBindingsQuery = useAllRoleBindings(environmentId, { const roleBindingsQuery = useRoleBindings(environmentId, {
autoRefreshRate: tableState.autoRefreshRate * 1000, autoRefreshRate: tableState.autoRefreshRate * 1000,
}); });
const filteredRoleBindings = useMemo( const filteredRoleBindings = useMemo(

View File

@ -8,7 +8,7 @@ import { RoleBinding } from '../types';
import { queryKeys } from './query-keys'; import { queryKeys } from './query-keys';
export function useAllRoleBindings( export function useRoleBindings(
environmentId: EnvironmentId, environmentId: EnvironmentId,
options?: { autoRefreshRate?: number; enabled?: boolean } options?: { autoRefreshRate?: number; enabled?: boolean }
) { ) {

View File

@ -7,7 +7,6 @@ import { Authorized } from '@/react/hooks/useUser';
import { notifyError, notifySuccess } from '@/portainer/services/notifications'; import { notifyError, notifySuccess } from '@/portainer/services/notifications';
import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription'; import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription';
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton'; import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
import { useUnauthorizedRedirect } from '@/react/hooks/useUnauthorizedRedirect';
import { import {
DefaultDatatableSettings, DefaultDatatableSettings,
TableSettings as KubeTableSettings, TableSettings as KubeTableSettings,
@ -22,12 +21,12 @@ import {
filteredColumnsSettings, filteredColumnsSettings,
} from '@@/datatables/types'; } from '@@/datatables/types';
import { useAllRoleBindings } from '../RoleBindingsDatatable/queries/useAllRoleBindings'; import { useRoleBindings } from '../RoleBindingsDatatable/queries/useRoleBindings';
import { RoleBinding } from '../RoleBindingsDatatable/types'; import { RoleBinding } from '../RoleBindingsDatatable/types';
import { columns } from './columns'; import { columns } from './columns';
import { Role, RoleRowData } from './types'; import { Role, RoleRowData } from './types';
import { useAllRoles } from './queries/useAllRoles'; import { useRoles } from './queries/useRoles';
import { useDeleteRoles } from './queries/useDeleteRoles'; import { useDeleteRoles } from './queries/useDeleteRoles';
const storageKey = 'roles'; const storageKey = 'roles';
@ -36,11 +35,6 @@ interface TableSettings
FilteredColumnsTableSettings {} FilteredColumnsTableSettings {}
export function RolesDatatable() { export function RolesDatatable() {
useUnauthorizedRedirect(
{ authorizations: ['K8sRolesW'] },
{ to: 'kubernetes.dashboard' }
);
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const tableState = useKubeStore<TableSettings>( const tableState = useKubeStore<TableSettings>(
storageKey, storageKey,
@ -49,10 +43,10 @@ export function RolesDatatable() {
...filteredColumnsSettings(set), ...filteredColumnsSettings(set),
}) })
); );
const rolesQuery = useAllRoles(environmentId, { const rolesQuery = useRoles(environmentId, {
autoRefreshRate: tableState.autoRefreshRate * 1000, autoRefreshRate: tableState.autoRefreshRate * 1000,
}); });
const roleBindingsQuery = useAllRoleBindings(environmentId, { const roleBindingsQuery = useRoleBindings(environmentId, {
autoRefreshRate: tableState.autoRefreshRate * 1000, autoRefreshRate: tableState.autoRefreshRate * 1000,
}); });
const roleRowData = useRoleRowData(rolesQuery.data, roleBindingsQuery.data); const roleRowData = useRoleRowData(rolesQuery.data, roleBindingsQuery.data);

View File

@ -11,7 +11,7 @@ const queryKeys = {
['environments', environmentId, 'kubernetes', 'roles'] as const, ['environments', environmentId, 'kubernetes', 'roles'] as const,
}; };
export function useAllRoles( export function useRoles(
environmentId: EnvironmentId, environmentId: EnvironmentId,
options?: { autoRefreshRate?: number; enabled?: boolean } options?: { autoRefreshRate?: number; enabled?: boolean }
) { ) {

View File

@ -12,7 +12,6 @@ import {
TableSettings as KubeTableSettings, TableSettings as KubeTableSettings,
} from '@/react/kubernetes/datatables/DefaultDatatableSettings'; } from '@/react/kubernetes/datatables/DefaultDatatableSettings';
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton'; import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
import { useUnauthorizedRedirect } from '@/react/hooks/useUnauthorizedRedirect';
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace'; import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
import { useKubeStore } from '@/react/kubernetes/datatables/default-kube-datatable-store'; import { useKubeStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
@ -35,11 +34,6 @@ interface TableSettings
FilteredColumnsTableSettings {} FilteredColumnsTableSettings {}
export function ServiceAccountsDatatable() { export function ServiceAccountsDatatable() {
useUnauthorizedRedirect(
{ authorizations: ['K8sServiceAccountsW'] },
{ to: 'kubernetes.dashboard' }
);
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const tableState = useKubeStore<TableSettings>( const tableState = useKubeStore<TableSettings>(
storageKey, storageKey,