2022-09-21 04:49:42 +00:00
|
|
|
import { useQuery } from 'react-query';
|
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-09-21 04:49:42 +00:00
|
|
|
import { error as notifyError } from '@/portainer/services/notifications';
|
2023-06-11 21:46:48 +00:00
|
|
|
import { withError } from '@/react-tools/react-query';
|
2022-11-07 06:03:11 +00:00
|
|
|
|
2023-03-07 22:22:08 +00:00
|
|
|
import {
|
|
|
|
getNamespaces,
|
|
|
|
getNamespace,
|
|
|
|
getSelfSubjectAccessReview,
|
|
|
|
} from './service';
|
2022-10-27 03:14:54 +00:00
|
|
|
import { Namespaces } from './types';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2023-06-11 21:46:48 +00:00
|
|
|
export function useNamespaces(
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useNamespace(environmentId: EnvironmentId, namespace: string) {
|
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'namespaces', namespace],
|
|
|
|
() => getNamespace(environmentId, namespace),
|
|
|
|
{
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to get namespace.');
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|