2024-04-14 14:54:25 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2023-06-11 21:46:48 +00:00
|
|
|
import { withError } from '@/react-tools/react-query';
|
2023-10-11 19:32:02 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
2022-11-07 06:03:11 +00:00
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
import { Namespaces } from '../types';
|
|
|
|
import { getSelfSubjectAccessReview } from '../getSelfSubjectAccessReview';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
export function useNamespacesQuery(
|
2023-06-11 21:46:48 +00:00
|
|
|
environmentId: EnvironmentId,
|
|
|
|
options?: { autoRefreshRate?: number }
|
|
|
|
) {
|
2022-09-21 04:49:42 +00:00
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'namespaces'],
|
2022-10-27 03:14:54 +00:00
|
|
|
async () => {
|
|
|
|
const namespaces = await getNamespaces(environmentId);
|
2023-03-07 22:22:08 +00:00
|
|
|
const namespaceNames = Object.keys(namespaces);
|
2023-03-31 00:24:57 +00:00
|
|
|
// use selfsubjectaccess reviews to avoid forbidden requests
|
2023-03-07 22:22:08 +00:00
|
|
|
const allNamespaceAccessReviews = await Promise.all(
|
|
|
|
namespaceNames.map((namespaceName) =>
|
|
|
|
getSelfSubjectAccessReview(environmentId, namespaceName)
|
2022-10-27 03:14:54 +00:00
|
|
|
)
|
|
|
|
);
|
2023-03-07 22:22:08 +00:00
|
|
|
const allowedNamespacesNames = allNamespaceAccessReviews
|
|
|
|
.filter((accessReview) => accessReview.status.allowed)
|
|
|
|
.map((accessReview) => accessReview.spec.resourceAttributes.namespace);
|
|
|
|
const allowedNamespaces = namespaceNames.reduce((acc, namespaceName) => {
|
|
|
|
if (allowedNamespacesNames.includes(namespaceName)) {
|
|
|
|
acc[namespaceName] = namespaces[namespaceName];
|
2022-10-27 03:14:54 +00:00
|
|
|
}
|
2023-03-07 22:22:08 +00:00
|
|
|
return acc;
|
|
|
|
}, {} as Namespaces);
|
|
|
|
return allowedNamespaces;
|
2022-10-27 03:14:54 +00:00
|
|
|
},
|
2022-09-21 04:49:42 +00:00
|
|
|
{
|
2023-06-11 21:46:48 +00:00
|
|
|
...withError('Unable to get namespaces.'),
|
|
|
|
refetchInterval() {
|
|
|
|
return options?.autoRefreshRate ?? false;
|
2022-09-21 04:49:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
// getNamespaces is used to retrieve namespaces using the Portainer backend with caching
|
|
|
|
async function getNamespaces(environmentId: EnvironmentId) {
|
|
|
|
try {
|
|
|
|
const { data: namespaces } = await axios.get<Namespaces>(
|
|
|
|
`kubernetes/${environmentId}/namespaces`
|
|
|
|
);
|
|
|
|
return namespaces;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(e as Error, 'Unable to retrieve namespaces');
|
|
|
|
}
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|