2024-04-14 14:54:25 +00:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
2023-09-05 16:06:36 +00:00
|
|
|
import { Operation } from 'fast-json-patch';
|
2023-09-10 22:52:00 +00:00
|
|
|
import _ from 'lodash';
|
2023-09-05 16:06:36 +00:00
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
import { withError, withInvalidate } from '@/react-tools/react-query';
|
2023-09-05 16:06:36 +00:00
|
|
|
import { environmentQueryKeys } from '@/react/portainer/environments/queries/query-keys';
|
|
|
|
import {
|
|
|
|
UpdateEnvironmentPayload,
|
|
|
|
updateEnvironment,
|
|
|
|
} from '@/react/portainer/environments/queries/useUpdateEnvironmentMutation';
|
2023-10-23 19:52:40 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
2023-09-05 16:06:36 +00:00
|
|
|
|
|
|
|
import { updateIngressControllerClassMap } from '../../ingressClass/useIngressControllerClassMap';
|
2023-10-11 19:32:02 +00:00
|
|
|
import { IngressControllerClassMap } from '../../ingressClass/types';
|
2023-09-05 16:06:36 +00:00
|
|
|
|
|
|
|
export type ConfigureClusterPayloads = {
|
|
|
|
id: number;
|
|
|
|
updateEnvironmentPayload: Partial<UpdateEnvironmentPayload>;
|
2023-10-11 19:32:02 +00:00
|
|
|
initialIngressControllers: IngressControllerClassMap[];
|
|
|
|
ingressControllers: IngressControllerClassMap[];
|
2023-09-05 16:06:36 +00:00
|
|
|
storageClassPatches: {
|
|
|
|
name: string;
|
|
|
|
patch: Operation[];
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// useConfigureClusterMutation updates the environment, the ingress classes and the storage classes
|
|
|
|
export function useConfigureClusterMutation() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
|
|
async ({
|
|
|
|
id,
|
|
|
|
updateEnvironmentPayload,
|
2023-09-10 22:52:00 +00:00
|
|
|
initialIngressControllers,
|
2023-09-05 16:06:36 +00:00
|
|
|
ingressControllers,
|
|
|
|
storageClassPatches,
|
|
|
|
}: ConfigureClusterPayloads) => {
|
|
|
|
await updateEnvironment({ id, payload: updateEnvironmentPayload });
|
|
|
|
await Promise.all(
|
|
|
|
storageClassPatches.map(({ name, patch }) =>
|
|
|
|
patchStorageClass(id, name, patch)
|
|
|
|
)
|
|
|
|
);
|
2023-09-10 22:52:00 +00:00
|
|
|
// only update the ingress classes if they have changed
|
|
|
|
if (!_.isEqual(initialIngressControllers, ingressControllers)) {
|
|
|
|
await updateIngressControllerClassMap(id, ingressControllers);
|
|
|
|
}
|
2023-09-05 16:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-11 19:32:02 +00:00
|
|
|
...withInvalidate(queryClient, [environmentQueryKeys.base()], {
|
|
|
|
skipRefresh: true,
|
|
|
|
}),
|
2023-09-05 16:06:36 +00:00
|
|
|
...withError('Unable to apply configuration', 'Failure'),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function patchStorageClass(
|
|
|
|
environmentId: number,
|
|
|
|
name: string,
|
|
|
|
storageClassPatch: Operation[]
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await axios.patch(
|
|
|
|
`/endpoints/${environmentId}/kubernetes/apis/storage.k8s.io/v1/storageclasses/${name}`,
|
|
|
|
storageClassPatch,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json-patch+json',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseAxiosError(e, `Unable to patch StorageClass ${name}`);
|
2023-09-05 16:06:36 +00:00
|
|
|
}
|
|
|
|
}
|