mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
This commit is contained in:
parent
da010f3d08
commit
ea228c3d6d
276 changed files with 9241 additions and 3361 deletions
|
@ -0,0 +1,147 @@
|
|||
import { User } from 'lucide-react';
|
||||
import { useRouter } from '@uirouter/react';
|
||||
|
||||
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||
import { Authorized } from '@/react/hooks/useUser';
|
||||
import { notifyError, notifySuccess } from '@/portainer/services/notifications';
|
||||
import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription';
|
||||
import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNamespacesQuery';
|
||||
import { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
||||
import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton';
|
||||
import { useUnauthorizedRedirect } from '@/react/hooks/useUnauthorizedRedirect';
|
||||
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
||||
|
||||
import { Datatable, TableSettingsMenu } from '@@/datatables';
|
||||
import { useTableState } from '@@/datatables/useTableState';
|
||||
import { DeleteButton } from '@@/buttons/DeleteButton';
|
||||
|
||||
import { ServiceAccount } from '../types';
|
||||
import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings';
|
||||
|
||||
import { useColumns } from './columns';
|
||||
import { useDeleteServiceAccountsMutation } from './queries/useDeleteServiceAccountsMutation';
|
||||
import { useGetAllServiceAccountsQuery } from './queries/useGetAllServiceAccountsQuery';
|
||||
|
||||
const storageKey = 'serviceAccounts';
|
||||
const settingsStore = createStore(storageKey);
|
||||
|
||||
export function ServiceAccountsDatatable() {
|
||||
useUnauthorizedRedirect(
|
||||
{ authorizations: ['K8sServiceAccountsW'] },
|
||||
{ to: 'kubernetes.dashboard' }
|
||||
);
|
||||
|
||||
const environmentId = useEnvironmentId();
|
||||
const tableState = useTableState(settingsStore, storageKey);
|
||||
const namespacesQuery = useNamespacesQuery(environmentId);
|
||||
const serviceAccountsQuery = useGetAllServiceAccountsQuery(environmentId, {
|
||||
refetchInterval: tableState.autoRefreshRate * 1000,
|
||||
enabled: namespacesQuery.isSuccess,
|
||||
});
|
||||
|
||||
const columns = useColumns();
|
||||
|
||||
const filteredServiceAccounts = tableState.showSystemResources
|
||||
? serviceAccountsQuery.data
|
||||
: serviceAccountsQuery.data?.filter(
|
||||
(sa) => !isSystemNamespace(sa.namespace, namespacesQuery.data)
|
||||
);
|
||||
|
||||
return (
|
||||
<Datatable
|
||||
dataset={filteredServiceAccounts || []}
|
||||
columns={columns}
|
||||
settingsManager={tableState}
|
||||
isLoading={serviceAccountsQuery.isLoading}
|
||||
emptyContentLabel="No service accounts found"
|
||||
title="Service Accounts"
|
||||
titleIcon={User}
|
||||
getRowId={(row) => row.uid}
|
||||
isRowSelectable={(row) => !row.original.isSystem}
|
||||
renderTableActions={(selectedRows) => (
|
||||
<TableActions selectedItems={selectedRows} />
|
||||
)}
|
||||
renderTableSettings={() => (
|
||||
<TableSettingsMenu>
|
||||
<DefaultDatatableSettings settings={tableState} />
|
||||
</TableSettingsMenu>
|
||||
)}
|
||||
description={
|
||||
<SystemResourceDescription
|
||||
showSystemResources={tableState.showSystemResources}
|
||||
/>
|
||||
}
|
||||
data-cy="k8s-service-accounts-datatable"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface SelectedServiceAccount {
|
||||
namespace: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
type TableActionsProps = {
|
||||
selectedItems: ServiceAccount[];
|
||||
};
|
||||
|
||||
function TableActions({ selectedItems }: TableActionsProps) {
|
||||
const environmentId = useEnvironmentId();
|
||||
const deleteServiceAccountsMutation =
|
||||
useDeleteServiceAccountsMutation(environmentId);
|
||||
const router = useRouter();
|
||||
|
||||
return (
|
||||
<Authorized authorizations="K8sServiceAccountsW">
|
||||
<DeleteButton
|
||||
disabled={selectedItems.length === 0}
|
||||
onConfirmed={() => handleRemoveClick(selectedItems)}
|
||||
confirmMessage={
|
||||
<>
|
||||
<p>
|
||||
Are you sure you want to delete the selected service account(s)?
|
||||
</p>
|
||||
<ul className="mt-2 max-h-96 list-inside overflow-hidden overflow-y-auto text-sm">
|
||||
{selectedItems.map((s, index) => (
|
||||
<li key={index}>
|
||||
{s.namespace}/{s.name}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</>
|
||||
}
|
||||
data-cy="k8s-service-accounts-datatable-remove-button"
|
||||
/>
|
||||
|
||||
<CreateFromManifestButton data-cy="k8s-service-accounts-datatable-create-button" />
|
||||
</Authorized>
|
||||
);
|
||||
|
||||
async function handleRemoveClick(serviceAccounts: SelectedServiceAccount[]) {
|
||||
const payload: Record<string, string[]> = {};
|
||||
serviceAccounts.forEach((sa) => {
|
||||
payload[sa.namespace] = payload[sa.namespace] || [];
|
||||
payload[sa.namespace].push(sa.name);
|
||||
});
|
||||
|
||||
deleteServiceAccountsMutation.mutate(
|
||||
{ environmentId, data: payload },
|
||||
{
|
||||
onSuccess: () => {
|
||||
notifySuccess(
|
||||
'Service account(s) successfully removed',
|
||||
serviceAccounts.map((sa) => `${sa.namespace}/${sa.name}`).join(', ')
|
||||
);
|
||||
router.stateService.reload();
|
||||
},
|
||||
onError: (error) => {
|
||||
notifyError(
|
||||
'Unable to delete service account(s)',
|
||||
error as Error,
|
||||
serviceAccounts.map((sa) => `${sa.namespace}/${sa.name}`).join(', ')
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
import { formatDate } from '@/portainer/filters/filters';
|
||||
|
||||
import { columnHelper } from './helper';
|
||||
|
||||
export const created = columnHelper.accessor(
|
||||
(row) => formatDate(row.creationDate),
|
||||
{
|
||||
header: 'Created',
|
||||
id: 'created',
|
||||
cell: ({ getValue }) => getValue(),
|
||||
}
|
||||
);
|
|
@ -0,0 +1,5 @@
|
|||
import { createColumnHelper } from '@tanstack/react-table';
|
||||
|
||||
import { ServiceAccount } from '../../types';
|
||||
|
||||
export const columnHelper = createColumnHelper<ServiceAccount>();
|
|
@ -0,0 +1,7 @@
|
|||
import { name } from './name';
|
||||
import { namespace } from './namespace';
|
||||
import { created } from './created';
|
||||
|
||||
export function useColumns() {
|
||||
return [name, namespace, created];
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
import { SystemBadge } from '@@/Badge/SystemBadge';
|
||||
import { UnusedBadge } from '@@/Badge/UnusedBadge';
|
||||
|
||||
import { columnHelper } from './helper';
|
||||
|
||||
export const name = columnHelper.accessor(
|
||||
(row) => {
|
||||
let result = row.name;
|
||||
if (row.isSystem) {
|
||||
result += ' system';
|
||||
}
|
||||
if (row.isUnused) {
|
||||
result += ' unused';
|
||||
}
|
||||
return result;
|
||||
},
|
||||
{
|
||||
header: 'Name',
|
||||
id: 'name',
|
||||
cell: ({ row }) => (
|
||||
<div className="flex gap-2">
|
||||
<div>{row.original.name}</div>
|
||||
{row.original.isSystem && <SystemBadge />}
|
||||
{!row.original.isSystem && row.original.isUnused && <UnusedBadge />}
|
||||
</div>
|
||||
),
|
||||
}
|
||||
);
|
|
@ -0,0 +1,36 @@
|
|||
import { Row } from '@tanstack/react-table';
|
||||
|
||||
import { Link } from '@@/Link';
|
||||
import { filterHOC } from '@@/datatables/Filter';
|
||||
|
||||
import { ServiceAccount } from '../../types';
|
||||
|
||||
import { columnHelper } from './helper';
|
||||
|
||||
export const namespace = columnHelper.accessor('namespace', {
|
||||
header: 'Namespace',
|
||||
id: 'namespace',
|
||||
cell: ({ row }) => (
|
||||
<Link
|
||||
to="kubernetes.resourcePools.resourcePool"
|
||||
params={{
|
||||
id: row.original.namespace,
|
||||
}}
|
||||
title={row.original.namespace}
|
||||
data-cy={`service-account-namespace-link-${row.original.name}`}
|
||||
>
|
||||
{row.original.namespace}
|
||||
</Link>
|
||||
),
|
||||
meta: {
|
||||
filter: filterHOC('Filter by namespace'),
|
||||
},
|
||||
enableColumnFilter: true,
|
||||
filterFn: (
|
||||
row: Row<ServiceAccount>,
|
||||
_columnId: string,
|
||||
filterValue: string[]
|
||||
) =>
|
||||
filterValue.length === 0 ||
|
||||
filterValue.includes(row.original.namespace ?? ''),
|
||||
});
|
|
@ -0,0 +1 @@
|
|||
export { ServiceAccountsDatatable } from './ServiceAccountsDatatable';
|
|
@ -0,0 +1,6 @@
|
|||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
export const queryKeys = {
|
||||
list: (environmentId: EnvironmentId) =>
|
||||
['environments', environmentId, 'kubernetes', 'serviceaccounts'] as const,
|
||||
};
|
|
@ -0,0 +1,33 @@
|
|||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { withGlobalError } from '@/react-tools/react-query';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { queryKeys } from './query-keys';
|
||||
|
||||
export function useDeleteServiceAccountsMutation(environmentId: EnvironmentId) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(deleteServices, {
|
||||
onSuccess: () =>
|
||||
queryClient.invalidateQueries(queryKeys.list(environmentId)),
|
||||
...withGlobalError('Unable to delete service accounts'),
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteServices({
|
||||
environmentId,
|
||||
data,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
data: Record<string, string[]>;
|
||||
}) {
|
||||
try {
|
||||
return await axios.post(
|
||||
`kubernetes/${environmentId}/service_accounts/delete`,
|
||||
data
|
||||
);
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e, `Unable to delete service accounts`);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { withGlobalError } from '@/react-tools/react-query';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { ServiceAccount } from '../../types';
|
||||
|
||||
import { queryKeys } from './query-keys';
|
||||
|
||||
export function useGetAllServiceAccountsQuery(
|
||||
environmentId: EnvironmentId,
|
||||
options?: {
|
||||
refetchInterval?: number;
|
||||
enabled?: boolean;
|
||||
}
|
||||
) {
|
||||
return useQuery(
|
||||
queryKeys.list(environmentId),
|
||||
async () => getAllServiceAccounts(environmentId),
|
||||
{
|
||||
...withGlobalError('Unable to get service accounts'),
|
||||
...options,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function getAllServiceAccounts(environmentId: EnvironmentId) {
|
||||
try {
|
||||
const { data: services } = await axios.get<ServiceAccount[]>(
|
||||
`kubernetes/${environmentId}/service_accounts`
|
||||
);
|
||||
|
||||
return services;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e, 'Unable to get service accounts');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue