Skip to content

Commit

Permalink
Use machineautoscalers, add logic to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
maroroman committed Sep 30, 2022
1 parent e9596d9 commit 1450cec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
42 changes: 26 additions & 16 deletions backend/src/routes/api/gpu/gpuUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,33 @@ export const getGPUData = async (
};

const getGPUScaling = async (fastify: KubeFastifyInstance): Promise<gpuScale[]> => {
let scalingList: gpuScale[] = []
const autoscalers = await fastify.kube.customObjectsApi
.listNamespacedCustomObject('autoscaling.openshift.io', 'v1beta1', 'openshift-machine-api', 'machineautoscalers')
.then((res) => {
return res.body as MachineAutoscalerList;
});
autoscalers.items.forEach(async (autoscaler) => {
const machineSetName = autoscaler.spec.scaleTargetRef.name; //also gives info about kind and apiversion if needed in the future
const machineSet = await fastify.kube.customObjectsApi
.getNamespacedCustomObject('machine.openshift.io', 'v1beta1', 'openshift-machine-api', 'machinesets', machineSetName)
.then((res) => {
return res.body as MachineSet;
});
let scalingList: gpuScale[] = [];
const autoscalerList = (
await fastify.kube.customObjectsApi.listNamespacedCustomObject(
'autoscaling.openshift.io',
'v1beta1',
'openshift-machine-api',
'machineautoscalers',
)
).body as MachineAutoscalerList;
for (let i = 0; i < autoscalerList.items.length; i++) {
const machineSetName = autoscalerList.items[i].spec.scaleTargetRef.name; //also gives info about kind and apiversion if needed in the future
const machineSet = (
await fastify.kube.customObjectsApi.getNamespacedCustomObject(
'machine.openshift.io',
'v1beta1',
'openshift-machine-api',
'machinesets',
machineSetName,
)
).body as MachineSet;
const gpuAmount = Number(machineSet?.metadata.annotations?.['machine.openshift.io/GPU']);
if (gpuAmount > 0){
scalingList.push({maxScale: autoscaler.spec.maxReplicas, gpuNumber: gpuAmount});
if (gpuAmount > 0) {
scalingList.push({
maxScale: autoscalerList.items[i].spec.maxReplicas,
gpuNumber: gpuAmount,
});
}
});
}
return scalingList;
};
2 changes: 1 addition & 1 deletion backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,5 +697,5 @@ export type gpuScale = {
export type GPUInfo = {
configured: boolean;
available: number;
autoscalers: gpuScale[]
autoscalers: gpuScale[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { FormGroup, Select, SelectOption, Skeleton } from '@patternfly/react-core';
import { getGPU } from '../../../../services/gpuService';
import useNotification from '../../../../utilities/useNotification';
import { gpuScale } from 'types';

type GPUSelectFieldProps = {
value: string;
Expand All @@ -11,7 +12,7 @@ type GPUSelectFieldProps = {
const GPUSelectField: React.FC<GPUSelectFieldProps> = ({ value, setValue }) => {
const [gpuDropdownOpen, setGpuDropdownOpen] = React.useState<boolean>(false);
const [gpuSize, setGpuSize] = React.useState<number>();
const [gpuScale, setGpuScale] = React.useState<number>();
const [gpuAutoscale, setGpuAutoscale] = React.useState<gpuScale[]>([]);
const [isFetching, setFetching] = React.useState(true);
const [areGpusAvailable, setAreGpusAvailable] = React.useState<boolean>(false);
const notification = useNotification();
Expand All @@ -26,7 +27,7 @@ const GPUSelectField: React.FC<GPUSelectFieldProps> = ({ value, setValue }) => {
if (cancelled) return;
setGpuSize(gpuInfo.available || 0);
setAreGpusAvailable(gpuInfo.configured);
setGpuScale(gpuInfo.scaleMax || 0);
setGpuAutoscale(gpuInfo.autoscalers);
setFetching(false);
});
};
Expand All @@ -36,7 +37,7 @@ const GPUSelectField: React.FC<GPUSelectFieldProps> = ({ value, setValue }) => {
setFetching(false);
setAreGpusAvailable(false);
setGpuSize(0);
setGpuScale(0);
setGpuAutoscale([]);
console.error(e);
notification.error('Failed to fetch GPU', e.message);
};
Expand All @@ -60,15 +61,27 @@ const GPUSelectField: React.FC<GPUSelectFieldProps> = ({ value, setValue }) => {
};
}, [notification, areGpusAvailable]);

React.useEffect(() => {
let maxScale = 0;
if (gpuAutoscale) {
for (let i = 0; i < gpuAutoscale.length; i++) {
const gpuNumber = gpuAutoscale[i].gpuNumber;
if (gpuNumber > maxScale) {
maxScale = gpuNumber;
}
}
}
if (gpuSize === undefined ? 0 : gpuSize < maxScale) {
setGpuSize(maxScale);
}
}, [gpuAutoscale, gpuSize]);

if (!areGpusAvailable) {
return null;
}

//TODO: We need to get the amount of gpus already scaled so as to not lie to the user about the avaiable gpus.
const gpuOptions =
gpuSize === undefined
? []
: Array.from(Array(gpuSize + (gpuScale === undefined ? 0 : gpuScale) + 1).keys());
//TODO: We need to get the amount of gpus already scaled so as to not lie to the user about the available gpus.
const gpuOptions = gpuSize === undefined ? [] : Array.from(Array(gpuSize + 1).keys());
const noAvailableGPUs = gpuOptions.length === 1;

return (
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,13 @@ export enum EventStatus {

export type UsernameMap<V> = { [username: string]: V };

export type gpuScale = {
maxScale: number;
gpuNumber: number;
};

export type GPUInfo = {
configured: boolean;
available: number;
scaleMax: number;
autoscalers: gpuScale[];
};

0 comments on commit 1450cec

Please sign in to comment.