mirror of https://github.com/portainer/portainer
update roles tables
parent
31b6de9a11
commit
9ddebf91f2
|
@ -14,6 +14,7 @@ export type ClusterRoleSubject = {
|
||||||
export type ClusterRoleBinding = {
|
export type ClusterRoleBinding = {
|
||||||
name: string;
|
name: string;
|
||||||
uid: string;
|
uid: string;
|
||||||
|
namespace: string;
|
||||||
roleRef: ClusterRoleRef;
|
roleRef: ClusterRoleRef;
|
||||||
subjects: ClusterRoleSubject[] | null;
|
subjects: ClusterRoleSubject[] | null;
|
||||||
creationDate: string;
|
creationDate: string;
|
||||||
|
|
|
@ -2,45 +2,56 @@ import { Trash2, Link as LinkIcon } from 'lucide-react';
|
||||||
import { useRouter } from '@uirouter/react';
|
import { useRouter } from '@uirouter/react';
|
||||||
import { Row } from '@tanstack/react-table';
|
import { Row } from '@tanstack/react-table';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||||
import { useAuthorizations, Authorized } from '@/react/hooks/useUser';
|
import { useAuthorizations, 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 { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
import {
|
||||||
import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNamespacesQuery';
|
DefaultDatatableSettings,
|
||||||
|
TableSettings as KubeTableSettings,
|
||||||
|
} from '@/react/kubernetes/datatables/DefaultDatatableSettings';
|
||||||
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
|
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
|
||||||
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
import { useKubeStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
||||||
|
|
||||||
import { confirmDelete } from '@@/modals/confirm';
|
import { confirmDelete } from '@@/modals/confirm';
|
||||||
import { Datatable, Table, TableSettingsMenu } from '@@/datatables';
|
import { Datatable, Table, TableSettingsMenu } from '@@/datatables';
|
||||||
import { LoadingButton } from '@@/buttons';
|
import { LoadingButton } from '@@/buttons';
|
||||||
import { useTableState } from '@@/datatables/useTableState';
|
import {
|
||||||
|
type FilteredColumnsTableSettings,
|
||||||
import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings';
|
filteredColumnsSettings,
|
||||||
|
} from '@@/datatables/types';
|
||||||
|
|
||||||
import { RoleBinding } from './types';
|
import { RoleBinding } from './types';
|
||||||
import { columns } from './columns';
|
import { columns } from './columns';
|
||||||
import { useGetAllRoleBindingsQuery } from './queries/useGetAllRoleBindingsQuery';
|
import { useAllRoleBindings } from './queries/useAllRoleBindings';
|
||||||
import { useDeleteRoleBindingsMutation } from './queries/useDeleteRoleBindingsMutation';
|
import { useDeleteRoleBindings } from './queries/useDeleteRoleBindings';
|
||||||
|
|
||||||
const storageKey = 'roleBindings';
|
const storageKey = 'roleBindings';
|
||||||
const settingsStore = createStore(storageKey);
|
interface TableSettings
|
||||||
|
extends KubeTableSettings,
|
||||||
|
FilteredColumnsTableSettings {}
|
||||||
|
|
||||||
export function RoleBindingsDatatable() {
|
export function RoleBindingsDatatable() {
|
||||||
const environmentId = useEnvironmentId();
|
const environmentId = useEnvironmentId();
|
||||||
const tableState = useTableState(settingsStore, storageKey);
|
const tableState = useKubeStore<TableSettings>(
|
||||||
const namespacesQuery = useNamespacesQuery(environmentId);
|
storageKey,
|
||||||
const roleBindingsQuery = useGetAllRoleBindingsQuery(environmentId, {
|
undefined,
|
||||||
|
(set) => ({
|
||||||
|
...filteredColumnsSettings(set),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const roleBindingsQuery = useAllRoleBindings(environmentId, {
|
||||||
autoRefreshRate: tableState.autoRefreshRate * 1000,
|
autoRefreshRate: tableState.autoRefreshRate * 1000,
|
||||||
enabled: namespacesQuery.isSuccess,
|
|
||||||
});
|
});
|
||||||
|
const filteredRoleBindings = useMemo(
|
||||||
const filteredRoleBindings = tableState.showSystemResources
|
() =>
|
||||||
? roleBindingsQuery.data
|
tableState.showSystemResources
|
||||||
: roleBindingsQuery.data?.filter(
|
? roleBindingsQuery.data
|
||||||
(rb) => !isSystemNamespace(rb.namespace, namespacesQuery.data)
|
: roleBindingsQuery.data?.filter((rb) => !rb.isSystem),
|
||||||
);
|
[roleBindingsQuery.data, tableState.showSystemResources]
|
||||||
|
);
|
||||||
|
|
||||||
const { authorized: isAuthorisedToAddEdit } = useAuthorizations([
|
const { authorized: isAuthorisedToAddEdit } = useAuthorizations([
|
||||||
'K8sRoleBindingsW',
|
'K8sRoleBindingsW',
|
||||||
|
@ -100,8 +111,7 @@ type TableActionsProps = {
|
||||||
|
|
||||||
function TableActions({ selectedItems }: TableActionsProps) {
|
function TableActions({ selectedItems }: TableActionsProps) {
|
||||||
const environmentId = useEnvironmentId();
|
const environmentId = useEnvironmentId();
|
||||||
const deleteRoleBindingsMutation =
|
const deleteRoleBindingsMutation = useDeleteRoleBindings(environmentId);
|
||||||
useDeleteRoleBindingsMutation(environmentId);
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
async function handleRemoveClick(roles: SelectedRole[]) {
|
async function handleRemoveClick(roles: SelectedRole[]) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { RoleBinding } from '../types';
|
||||||
|
|
||||||
import { queryKeys } from './query-keys';
|
import { queryKeys } from './query-keys';
|
||||||
|
|
||||||
export function useGetAllRoleBindingsQuery(
|
export function useAllRoleBindings(
|
||||||
environmentId: EnvironmentId,
|
environmentId: EnvironmentId,
|
||||||
options?: { autoRefreshRate?: number; enabled?: boolean }
|
options?: { autoRefreshRate?: number; enabled?: boolean }
|
||||||
) {
|
) {
|
|
@ -6,7 +6,7 @@ import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||||
|
|
||||||
import { queryKeys } from './query-keys';
|
import { queryKeys } from './query-keys';
|
||||||
|
|
||||||
export function useDeleteRoleBindingsMutation(environmentId: EnvironmentId) {
|
export function useDeleteRoleBindings(environmentId: EnvironmentId) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
return useMutation(deleteRoleBindings, {
|
return useMutation(deleteRoleBindings, {
|
||||||
...withInvalidate(queryClient, [queryKeys.list(environmentId)]),
|
...withInvalidate(queryClient, [queryKeys.list(environmentId)]),
|
|
@ -15,12 +15,8 @@ export type RoleBinding = {
|
||||||
name: string;
|
name: string;
|
||||||
uid: string;
|
uid: string;
|
||||||
namespace: string;
|
namespace: string;
|
||||||
resourceVersion: string;
|
|
||||||
creationDate: string;
|
|
||||||
annotations: Record<string, string> | null;
|
|
||||||
|
|
||||||
roleRef: RoleRef;
|
roleRef: RoleRef;
|
||||||
subjects: RoleSubject[] | null;
|
subjects: RoleSubject[] | null;
|
||||||
|
creationDate: string;
|
||||||
isSystem: boolean;
|
isSystem: boolean;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,56 +1,76 @@
|
||||||
import { Trash2, UserCheck } from 'lucide-react';
|
import { Trash2, UserCheck } from 'lucide-react';
|
||||||
import { useRouter } from '@uirouter/react';
|
import { useRouter } from '@uirouter/react';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||||
import { Authorized } from '@/react/hooks/useUser';
|
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 { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
|
||||||
import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNamespacesQuery';
|
|
||||||
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
|
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
|
||||||
import { useUnauthorizedRedirect } from '@/react/hooks/useUnauthorizedRedirect';
|
import { useUnauthorizedRedirect } from '@/react/hooks/useUnauthorizedRedirect';
|
||||||
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
import {
|
||||||
|
DefaultDatatableSettings,
|
||||||
|
TableSettings as KubeTableSettings,
|
||||||
|
} from '@/react/kubernetes/datatables/DefaultDatatableSettings';
|
||||||
|
import { useKubeStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
||||||
|
|
||||||
import { confirmDelete } from '@@/modals/confirm';
|
import { confirmDelete } from '@@/modals/confirm';
|
||||||
import { Datatable, TableSettingsMenu } from '@@/datatables';
|
import { Datatable, TableSettingsMenu } from '@@/datatables';
|
||||||
import { LoadingButton } from '@@/buttons';
|
import { LoadingButton } from '@@/buttons';
|
||||||
import { useTableState } from '@@/datatables/useTableState';
|
import {
|
||||||
|
type FilteredColumnsTableSettings,
|
||||||
|
filteredColumnsSettings,
|
||||||
|
} from '@@/datatables/types';
|
||||||
|
|
||||||
import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings';
|
import { useAllRoleBindings } from '../RoleBindingsDatatable/queries/useAllRoleBindings';
|
||||||
|
import { RoleBinding } from '../RoleBindingsDatatable/types';
|
||||||
|
|
||||||
import { columns } from './columns';
|
import { columns } from './columns';
|
||||||
import { Role } from './types';
|
import { Role, RoleRowData } from './types';
|
||||||
import { useGetAllRolesQuery } from './queries/useGetAllRolesQuery';
|
import { useAllRoles } from './queries/useAllRoles';
|
||||||
import { useDeleteRolesMutation } from './queries/useDeleteRolesMutation';
|
import { useDeleteRoles } from './queries/useDeleteRoles';
|
||||||
|
|
||||||
const storageKey = 'roles';
|
const storageKey = 'roles';
|
||||||
const settingsStore = createStore(storageKey);
|
interface TableSettings
|
||||||
|
extends KubeTableSettings,
|
||||||
|
FilteredColumnsTableSettings {}
|
||||||
|
|
||||||
export function RolesDatatable() {
|
export function RolesDatatable() {
|
||||||
const environmentId = useEnvironmentId();
|
|
||||||
const tableState = useTableState(settingsStore, storageKey);
|
|
||||||
const namespacesQuery = useNamespacesQuery(environmentId);
|
|
||||||
const rolesQuery = useGetAllRolesQuery(environmentId, {
|
|
||||||
autoRefreshRate: tableState.autoRefreshRate * 1000,
|
|
||||||
enabled: namespacesQuery.isSuccess,
|
|
||||||
});
|
|
||||||
useUnauthorizedRedirect(
|
useUnauthorizedRedirect(
|
||||||
{ authorizations: ['K8sRolesW'] },
|
{ authorizations: ['K8sRolesW'] },
|
||||||
{ to: 'kubernetes.dashboard' }
|
{ to: 'kubernetes.dashboard' }
|
||||||
);
|
);
|
||||||
|
|
||||||
const filteredRoles = tableState.showSystemResources
|
const environmentId = useEnvironmentId();
|
||||||
? rolesQuery.data
|
const tableState = useKubeStore<TableSettings>(
|
||||||
: rolesQuery.data?.filter(
|
storageKey,
|
||||||
(role) => !isSystemNamespace(role.namespace, namespacesQuery.data)
|
undefined,
|
||||||
);
|
(set) => ({
|
||||||
|
...filteredColumnsSettings(set),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const rolesQuery = useAllRoles(environmentId, {
|
||||||
|
autoRefreshRate: tableState.autoRefreshRate * 1000,
|
||||||
|
});
|
||||||
|
const roleBindingsQuery = useAllRoleBindings(environmentId, {
|
||||||
|
autoRefreshRate: tableState.autoRefreshRate * 1000,
|
||||||
|
});
|
||||||
|
const roleRowData = useRoleRowData(rolesQuery.data, roleBindingsQuery.data);
|
||||||
|
|
||||||
|
const filteredRoles = useMemo(
|
||||||
|
() =>
|
||||||
|
tableState.showSystemResources
|
||||||
|
? roleRowData
|
||||||
|
: roleRowData.filter((role) => !role.isSystem),
|
||||||
|
[roleRowData, tableState.showSystemResources]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Datatable
|
<Datatable
|
||||||
dataset={filteredRoles || []}
|
dataset={filteredRoles || []}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
settingsManager={tableState}
|
settingsManager={tableState}
|
||||||
isLoading={rolesQuery.isLoading}
|
isLoading={rolesQuery.isLoading || roleBindingsQuery.isLoading}
|
||||||
emptyContentLabel="No roles found"
|
emptyContentLabel="No roles found"
|
||||||
title="Roles"
|
title="Roles"
|
||||||
titleIcon={UserCheck}
|
titleIcon={UserCheck}
|
||||||
|
@ -85,7 +105,7 @@ type TableActionsProps = {
|
||||||
|
|
||||||
function TableActions({ selectedItems }: TableActionsProps) {
|
function TableActions({ selectedItems }: TableActionsProps) {
|
||||||
const environmentId = useEnvironmentId();
|
const environmentId = useEnvironmentId();
|
||||||
const deleteRolesMutation = useDeleteRolesMutation(environmentId);
|
const deleteRolesMutation = useDeleteRoles(environmentId);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -155,3 +175,26 @@ function TableActions({ selectedItems }: TableActionsProps) {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark roles that are used by a role binding
|
||||||
|
|
||||||
|
// Mark roles that are used by a role binding
|
||||||
|
function useRoleRowData(
|
||||||
|
roles?: Role[],
|
||||||
|
roleBindings?: RoleBinding[]
|
||||||
|
): RoleRowData[] {
|
||||||
|
const roleRowData = useMemo(
|
||||||
|
() =>
|
||||||
|
roles?.map((role) => {
|
||||||
|
const isUsed = roleBindings?.some(
|
||||||
|
(roleBinding) =>
|
||||||
|
roleBinding.roleRef.name === role.name &&
|
||||||
|
roleBinding.namespace === role.namespace
|
||||||
|
);
|
||||||
|
return { ...role, isUnused: !isUsed };
|
||||||
|
}),
|
||||||
|
[roles, roleBindings]
|
||||||
|
);
|
||||||
|
|
||||||
|
return roleRowData ?? [];
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { createColumnHelper } from '@tanstack/react-table';
|
import { createColumnHelper } from '@tanstack/react-table';
|
||||||
|
|
||||||
import { Role } from '../types';
|
import { RoleRowData } from '../types';
|
||||||
|
|
||||||
export const columnHelper = createColumnHelper<Role>();
|
export const columnHelper = createColumnHelper<RoleRowData>();
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Row } from '@tanstack/react-table';
|
||||||
import { filterHOC } from '@@/datatables/Filter';
|
import { filterHOC } from '@@/datatables/Filter';
|
||||||
import { Link } from '@@/Link';
|
import { Link } from '@@/Link';
|
||||||
|
|
||||||
import { Role } from '../types';
|
import { RoleRowData } from '../types';
|
||||||
|
|
||||||
import { columnHelper } from './helper';
|
import { columnHelper } from './helper';
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ export const namespace = columnHelper.accessor((row) => row.namespace, {
|
||||||
filter: filterHOC('Filter by namespace'),
|
filter: filterHOC('Filter by namespace'),
|
||||||
},
|
},
|
||||||
enableColumnFilter: true,
|
enableColumnFilter: true,
|
||||||
filterFn: (row: Row<Role>, _columnId: string, filterValue: string[]) =>
|
filterFn: (row: Row<RoleRowData>, _columnId: string, filterValue: string[]) =>
|
||||||
filterValue.length === 0 ||
|
filterValue.length === 0 ||
|
||||||
filterValue.includes(row.original.namespace ?? ''),
|
filterValue.includes(row.original.namespace ?? ''),
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,7 +11,7 @@ const queryKeys = {
|
||||||
['environments', environmentId, 'kubernetes', 'roles'] as const,
|
['environments', environmentId, 'kubernetes', 'roles'] as const,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useGetAllRolesQuery(
|
export function useAllRoles(
|
||||||
environmentId: EnvironmentId,
|
environmentId: EnvironmentId,
|
||||||
options?: { autoRefreshRate?: number; enabled?: boolean }
|
options?: { autoRefreshRate?: number; enabled?: boolean }
|
||||||
) {
|
) {
|
|
@ -6,7 +6,7 @@ import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||||
|
|
||||||
import { queryKeys } from './query-keys';
|
import { queryKeys } from './query-keys';
|
||||||
|
|
||||||
export function useDeleteRolesMutation(environmentId: EnvironmentId) {
|
export function useDeleteRoles(environmentId: EnvironmentId) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
return useMutation(deleteRole, {
|
return useMutation(deleteRole, {
|
||||||
...withInvalidate(queryClient, [queryKeys.list(environmentId)]),
|
...withInvalidate(queryClient, [queryKeys.list(environmentId)]),
|
|
@ -8,12 +8,10 @@ export type Role = {
|
||||||
name: string;
|
name: string;
|
||||||
uid: string;
|
uid: string;
|
||||||
namespace: string;
|
namespace: string;
|
||||||
resourceVersion: string;
|
|
||||||
creationDate: string;
|
creationDate: string;
|
||||||
annotations?: Record<string, string>;
|
|
||||||
|
|
||||||
rules: Rule[];
|
|
||||||
|
|
||||||
isSystem: boolean;
|
isSystem: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RoleRowData = Role & {
|
||||||
isUnused: boolean;
|
isUnused: boolean;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue