1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 05:19:39 +02:00
portainer/app/react/kubernetes/applications/useJobs.ts
Steven Kang d32b0f8b7e feat(kubernetes): support for jobs and cron jobs - r8s-182 (#260)
Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com>
Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io>
Co-authored-by: andres-portainer <91705312+andres-portainer@users.noreply.github.com>
Co-authored-by: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com>
Co-authored-by: Yajith Dayarathna <yajith.dayarathna@portainer.io>
Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
Co-authored-by: oscarzhou <oscar.zhou@portainer.io>
Co-authored-by: testA113 <aliharriss1995@gmail.com>
2025-01-10 13:21:27 +13:00

74 lines
1.7 KiB
TypeScript

import { Job, JobList } from 'kubernetes-types/batch/v1';
import { useQuery } from '@tanstack/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { withGlobalError } from '@/react-tools/react-query';
import axios from '@/portainer/services/axios';
import { parseKubernetesAxiosError } from '../axiosError';
const queryKeys = {
jobsForCluster: (environmentId: EnvironmentId) => [
'environments',
environmentId,
'kubernetes',
'jobs',
],
};
export function useJobs(environmentId: EnvironmentId, namespaces?: string[]) {
return useQuery(
queryKeys.jobsForCluster(environmentId),
() => getJobsForCluster(environmentId, namespaces),
{
...withGlobalError('Unable to retrieve Jobs'),
enabled: !!namespaces?.length,
}
);
}
export async function getJobsForCluster(
environmentId: EnvironmentId,
namespaceNames?: string[]
) {
if (!namespaceNames) {
return [];
}
const jobs = await Promise.all(
namespaceNames.map((namespace) =>
getNamespaceJobs(environmentId, namespace)
)
);
return jobs.flat();
}
export async function getNamespaceJobs(
environmentId: EnvironmentId,
namespace: string,
labelSelector?: string
) {
try {
const { data } = await axios.get<JobList>(
`/endpoints/${environmentId}/kubernetes/apis/batch/v1/namespaces/${namespace}/jobs`,
{
params: {
labelSelector,
},
}
);
const items = (data.items || []).map(
(job) =>
<Job>{
...job,
kind: 'Job',
apiVersion: data.apiVersion,
}
);
return items;
} catch (e) {
throw parseKubernetesAxiosError(
e,
`Unable to retrieve Jobs in namespace '${namespace}'`
);
}
}