2024-04-14 14:54:25 +00:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
2023-05-29 03:06:14 +00:00
|
|
|
import { ServiceList } from 'kubernetes-types/core/v1';
|
2023-03-02 19:45:19 +00:00
|
|
|
|
2024-10-01 01:15:51 +00:00
|
|
|
import { withGlobalError } from '@/react-tools/react-query';
|
2023-03-02 19:45:19 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2024-10-01 01:15:51 +00:00
|
|
|
import { Service } from '@/react/kubernetes/services/types';
|
2023-05-02 06:42:16 +00:00
|
|
|
|
2023-08-27 21:01:35 +00:00
|
|
|
import { parseKubernetesAxiosError } from '../axiosError';
|
|
|
|
|
2023-03-02 19:45:19 +00:00
|
|
|
export const queryKeys = {
|
2023-05-29 03:06:14 +00:00
|
|
|
clusterServices: (environmentId: EnvironmentId) =>
|
2023-03-02 19:45:19 +00:00
|
|
|
['environments', environmentId, 'kubernetes', 'services'] as const,
|
|
|
|
};
|
|
|
|
|
2024-10-01 01:15:51 +00:00
|
|
|
/**
|
|
|
|
* Custom hook to fetch cluster services for a specific environment.
|
|
|
|
*
|
|
|
|
* @param environmentId - The ID of the environment.
|
|
|
|
* @param options - Additional options for fetching services.
|
|
|
|
* @param options.autoRefreshRate - The auto refresh rate for refetching services.
|
|
|
|
* @param options.withApplications - Whether to lookup applications for the services.
|
|
|
|
*
|
|
|
|
* @returns The result of the query.
|
|
|
|
*/
|
|
|
|
export function useClusterServices(
|
2023-06-11 21:46:48 +00:00
|
|
|
environmentId: EnvironmentId,
|
2024-10-01 01:15:51 +00:00
|
|
|
options?: { autoRefreshRate?: number; withApplications?: boolean }
|
2023-06-11 21:46:48 +00:00
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
queryKeys.clusterServices(environmentId),
|
2024-10-01 01:15:51 +00:00
|
|
|
async () => getClusterServices(environmentId, options?.withApplications),
|
2023-06-11 21:46:48 +00:00
|
|
|
{
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to get services.'),
|
2023-06-11 21:46:48 +00:00
|
|
|
refetchInterval() {
|
|
|
|
return options?.autoRefreshRate ?? false;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:01:35 +00:00
|
|
|
// get a list of services, based on an array of service names, for a specific namespace
|
|
|
|
export function useServicesQuery<T extends Service | string = Service>(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
serviceNames: string[],
|
|
|
|
options?: { yaml?: boolean }
|
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'services', serviceNames],
|
|
|
|
async () => {
|
|
|
|
// promise.all is best in this case because I want to return an error if even one service request has an error
|
|
|
|
const services = await Promise.all(
|
|
|
|
serviceNames.map((serviceName) =>
|
|
|
|
getService<T>(environmentId, namespace, serviceName, options?.yaml)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return services;
|
|
|
|
},
|
|
|
|
{
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to retrieve services.'),
|
2023-08-27 21:01:35 +00:00
|
|
|
enabled: !!serviceNames?.length,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-11 21:46:48 +00:00
|
|
|
export function useMutationDeleteServices(environmentId: EnvironmentId) {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(deleteServices, {
|
|
|
|
onSuccess: () =>
|
|
|
|
queryClient.invalidateQueries(queryKeys.clusterServices(environmentId)),
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to delete service(s)'),
|
2023-06-11 21:46:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-29 03:06:14 +00:00
|
|
|
// get a list of services for a specific namespace from the Portainer API
|
2023-08-27 21:01:35 +00:00
|
|
|
export async function getServices(
|
2023-03-02 19:45:19 +00:00
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
2024-10-01 01:15:51 +00:00
|
|
|
withApplications?: boolean
|
2023-03-02 19:45:19 +00:00
|
|
|
) {
|
|
|
|
try {
|
2023-05-02 06:42:16 +00:00
|
|
|
const { data: services } = await axios.get<Array<Service>>(
|
2023-03-02 19:45:19 +00:00
|
|
|
`kubernetes/${environmentId}/namespaces/${namespace}/services`,
|
|
|
|
{
|
|
|
|
params: {
|
2024-10-01 01:15:51 +00:00
|
|
|
withApplications,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return services;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(e, 'Unable to retrieve services');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getClusterServices(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
withApplications?: boolean
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { data: services } = await axios.get<Array<Service>>(
|
|
|
|
`kubernetes/${environmentId}/services`,
|
|
|
|
{
|
|
|
|
params: {
|
|
|
|
withApplications,
|
2023-03-02 19:45:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return services;
|
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseAxiosError(e, 'Unable to retrieve services');
|
2023-03-02 19:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:01:35 +00:00
|
|
|
// getNamespaceServices is used to get a list of services for a specific namespace
|
|
|
|
// it calls the kubernetes api directly and not the portainer api
|
2023-05-29 03:06:14 +00:00
|
|
|
export async function getNamespaceServices(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
queryParams?: Record<string, string>
|
|
|
|
) {
|
2023-08-27 21:01:35 +00:00
|
|
|
try {
|
|
|
|
const { data: services } = await axios.get<ServiceList>(
|
|
|
|
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/services`,
|
|
|
|
{
|
|
|
|
params: queryParams,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return services.items;
|
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseKubernetesAxiosError(e, 'Unable to retrieve services');
|
2023-08-27 21:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getService<T extends Service | string = Service>(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
serviceName: string,
|
|
|
|
yaml?: boolean
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { data: service } = await axios.get<T>(
|
|
|
|
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/services/${serviceName}`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Accept: yaml ? 'application/yaml' : 'application/json',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return service;
|
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseKubernetesAxiosError(e, 'Unable to retrieve service');
|
2023-08-27 21:01:35 +00:00
|
|
|
}
|
2023-05-29 03:06:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:45:19 +00:00
|
|
|
export async function deleteServices({
|
|
|
|
environmentId,
|
|
|
|
data,
|
|
|
|
}: {
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
data: Record<string, string[]>;
|
|
|
|
}) {
|
|
|
|
try {
|
|
|
|
return await axios.post(
|
|
|
|
`kubernetes/${environmentId}/services/delete`,
|
|
|
|
data
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2024-10-01 01:15:51 +00:00
|
|
|
throw parseAxiosError(e, 'Unable to delete service(s)');
|
2023-07-24 00:16:29 +00:00
|
|
|
}
|
|
|
|
}
|