Skip to content

Commit

Permalink
[WIP] enabling accelerators API
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanshug committed Nov 10, 2023
1 parent a2a301e commit f24c52a
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { KubeFastifyInstance, AcceleratorKind } from '../../../types';
import { FastifyRequest } from 'fastify';
import createError from 'http-errors';

export const updateAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
const params = request.params as { acceleratorProfileName: string };
const body = request.body as Partial<AcceleratorKind['spec']>;

try {
const currentProfile = await customObjectsApi
.getNamespacedCustomObject(
'dashboard.opendatahub.io',
'v1',
namespace,
'acceleratorprofiles',
params.acceleratorProfileName,
)
.then((r) => r.body as AcceleratorKind)
.catch((e) => {
throw createError(e.statusCode, e?.body?.message);
});
console.log('gupta', body);
if (body !== undefined) {
console.log('dip', body);
currentProfile.spec.enabled = body.enabled;
}

// Update the modified date annotation
currentProfile.metadata.annotations['opendatahub.io/modified-date'] = new Date().toISOString();

await customObjectsApi
.patchNamespacedCustomObject(
'dashboard.opendatahub.io',
'v1',
namespace,
'acceleratorprofiles',
params.acceleratorProfileName,
currentProfile,
undefined,
undefined,
undefined,
{
headers: { 'Content-Type': 'application/merge-patch+json' },
},
)
.catch((e) => {
throw createError(e.statusCode, e?.body?.message);
});
return { success: true, error: null };
} catch (e) {
fastify.log.error(e, 'Unable to update accelerator profile.');
return { success: false, error: 'Unable to update accelerator profile: ' + e.message };
}
};
18 changes: 18 additions & 0 deletions backend/src/routes/api/accelerator-profiles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { secureAdminRoute } from '../../../utils/route-security';
import { updateAcceleratorProfile } from './acceleratorProfilesUtils';

export default async (fastify: FastifyInstance): Promise<void> => {
fastify.put(
'/:acceleratorProfileName',
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => {
return updateAcceleratorProfile(fastify, request)
.then((res) => {
return res;
})
.catch((res) => {
reply.send(res);
});
}),
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from 'react';
import { Switch } from '@patternfly/react-core';
import DisableAcceleratorProfileModal from '~/pages/acceleratorProfiles/screens/list/DisableAcceleratorProfileModal';
import { updateAcceleratorProfile } from '~/services/acceleratorProfileService';

type AcceleratorProfileEnableToggleProps = {
enabled: boolean;
name: string;
};

const AcceleratorProfileEnableToggle: React.FC<AcceleratorProfileEnableToggleProps> = ({
enabled,
name,
}) => {
const label = enabled ? 'enabled' : 'stopped';
const [isModalOpen, setIsModalOpen] = React.useState(false);
const [inProgress, setInProgress] = React.useState(false);

return (
<>
<Switch
aria-label={label}
id={`${name}-enable-switch`}
isChecked={enabled}
isDisabled={inProgress}
onClick={() => {
if (enabled) {
setIsModalOpen(true);
} else {
setInProgress(true);
updateAcceleratorProfile(name, true).then(() => setInProgress(false));
}
}}
/>
<DisableAcceleratorProfileModal
isOpen={isModalOpen}
onClose={(confirmStatus) => {
if (confirmStatus) {
updateAcceleratorProfile(name, false);
}
setIsModalOpen(false);
}}
/>
</>
);
};

export default AcceleratorProfileEnableToggle;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';
import { Button, Modal } from '@patternfly/react-core';

type DisableAcceleratorProfileModal = {
isOpen: boolean;
onClose: (confirmStatus: boolean) => void;
};

const DisableAcceleratorProfileModal: React.FC<DisableAcceleratorProfileModal> = ({
isOpen,
onClose,
}) => (
<Modal
variant="small"
title="Disable accelerator"
isOpen={isOpen}
onClose={() => onClose(false)}
actions={[
<Button key="confirm-disable" variant="primary" onClick={() => onClose(true)}>
Disable
</Button>,
<Button key="cancel" variant="secondary" onClick={() => onClose(false)}>
Cancel
</Button>,
]}
>
Disable this will disable accelerators for existing images and runtimes that want to use it.
</Modal>
);

export default DisableAcceleratorProfileModal;
12 changes: 12 additions & 0 deletions frontend/src/services/acceleratorProfileService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from 'axios';
import { ResponseStatus } from '~/types';

export const updateAcceleratorProfile = (name: string, e: boolean): Promise<ResponseStatus> => {
const url = `/api/accelerator-profiles/${name}`;
return axios
.put(url, { enabled: e })
.then((response) => response.data)
.catch((e) => {
throw new Error(e.response.data.message);
});
};

0 comments on commit f24c52a

Please sign in to comment.