2023-08-27 21:01:35 +00:00
|
|
|
import {
|
|
|
|
HorizontalPodAutoscaler,
|
|
|
|
HorizontalPodAutoscalerList,
|
|
|
|
} from 'kubernetes-types/autoscaling/v1';
|
|
|
|
import { useQuery } from 'react-query';
|
2023-05-29 03:06:14 +00:00
|
|
|
|
|
|
|
import axios from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2023-08-27 21:01:35 +00:00
|
|
|
import { withError } from '@/react-tools/react-query';
|
|
|
|
|
|
|
|
import { parseKubernetesAxiosError } from '../axiosError';
|
|
|
|
|
|
|
|
// when yaml is set to true, the expected return type is a string
|
|
|
|
export function useHorizontalAutoScalarQuery<
|
2023-09-04 15:20:36 +00:00
|
|
|
T extends HorizontalPodAutoscaler | string = HorizontalPodAutoscaler,
|
2023-08-27 21:01:35 +00:00
|
|
|
>(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
name?: string,
|
|
|
|
options?: { yaml?: boolean }
|
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
[
|
|
|
|
'environments',
|
|
|
|
environmentId,
|
|
|
|
'kubernetes',
|
|
|
|
'namespaces',
|
|
|
|
namespace,
|
|
|
|
'horizontalpodautoscalers',
|
|
|
|
name,
|
|
|
|
options?.yaml,
|
|
|
|
],
|
|
|
|
() =>
|
|
|
|
name
|
|
|
|
? getNamespaceHorizontalPodAutoscaler<T>(
|
|
|
|
environmentId,
|
|
|
|
namespace,
|
|
|
|
name,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
: undefined,
|
|
|
|
{ ...withError('Unable to get horizontal pod autoscaler'), enabled: !!name }
|
|
|
|
);
|
|
|
|
}
|
2023-05-29 03:06:14 +00:00
|
|
|
|
|
|
|
export async function getNamespaceHorizontalPodAutoscalers(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string
|
|
|
|
) {
|
2023-08-27 21:01:35 +00:00
|
|
|
try {
|
|
|
|
const { data: autoScalarList } =
|
|
|
|
await axios.get<HorizontalPodAutoscalerList>(
|
|
|
|
`/endpoints/${environmentId}/kubernetes/apis/autoscaling/v1/namespaces/${namespace}/horizontalpodautoscalers`
|
|
|
|
);
|
|
|
|
return autoScalarList.items;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseKubernetesAxiosError(
|
|
|
|
e as Error,
|
|
|
|
'Unable to retrieve horizontal pod autoscalers'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getNamespaceHorizontalPodAutoscaler<
|
2023-09-04 15:20:36 +00:00
|
|
|
T extends HorizontalPodAutoscaler | string = HorizontalPodAutoscaler,
|
2023-08-27 21:01:35 +00:00
|
|
|
>(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
name: string,
|
|
|
|
options?: { yaml?: boolean }
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { data: autoScalar } = await axios.get<T>(
|
|
|
|
`/endpoints/${environmentId}/kubernetes/apis/autoscaling/v1/namespaces/${namespace}/horizontalpodautoscalers/${name}`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Accept: options?.yaml ? 'application/yaml' : 'application/json',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return autoScalar;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseKubernetesAxiosError(
|
|
|
|
e as Error,
|
|
|
|
'Unable to retrieve horizontal pod autoscaler'
|
|
|
|
);
|
|
|
|
}
|
2023-05-29 03:06:14 +00:00
|
|
|
}
|