1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 06:49:40 +02:00
portainer/app/react/kubernetes/applications/pod.service.ts
Ali 96ead31a8d
fix(kubeapi): fix ts api error handling [EE-5558] (#10488)
* fix(kubeapi): fix ts api error handling [EE-5558]

* use portainer errors for mapped functions

* don't parse long patch responses

* allow nested kube error that's thrown to bubble up

---------

Co-authored-by: testa113 <testa113>
2023-10-23 20:52:40 +01:00

95 lines
2.1 KiB
TypeScript

import { Pod, PodList } from 'kubernetes-types/core/v1';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { parseKubernetesAxiosError } from '../axiosError';
import { ApplicationPatch } from './types';
export async function getNamespacePods(
environmentId: EnvironmentId,
namespace: string,
labelSelector?: string
) {
try {
const { data } = await axios.get<PodList>(
buildUrl(environmentId, namespace),
{
params: {
labelSelector,
},
}
);
return data.items;
} catch (e) {
throw parseKubernetesAxiosError(
e,
`Unable to retrieve pods in namespace '${namespace}'`
);
}
}
export async function getPod<T extends Pod | string = Pod>(
environmentId: EnvironmentId,
namespace: string,
name: string,
yaml?: boolean
) {
try {
const { data } = await axios.get<T>(
buildUrl(environmentId, namespace, name),
{
headers: { Accept: yaml ? 'application/yaml' : 'application/json' },
}
);
return data;
} catch (e) {
throw parseKubernetesAxiosError(e, 'Unable to retrieve pod');
}
}
export async function patchPod(
environmentId: EnvironmentId,
namespace: string,
name: string,
patch: ApplicationPatch
) {
try {
return await axios.patch<Pod>(
buildUrl(environmentId, namespace, name),
patch,
{
headers: {
'Content-Type': 'application/json-patch+json',
},
}
);
} catch (e) {
throw parseAxiosError(e, 'Unable to update pod');
}
}
export async function deletePod(
environmentId: EnvironmentId,
namespace: string,
name: string
) {
try {
return await axios.delete<Pod>(buildUrl(environmentId, namespace, name));
} catch (e) {
throw parseKubernetesAxiosError(e as Error, 'Unable to delete pod');
}
}
export function buildUrl(
environmentId: EnvironmentId,
namespace: string,
name?: string
) {
let baseUrl = `/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/pods`;
if (name) {
baseUrl += `/${name}`;
}
return baseUrl;
}