-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
backend/src/routes/api/accelerator-profiles/acceleratorProfilesUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}), | ||
); | ||
}; |
48 changes: 48 additions & 0 deletions
48
frontend/src/pages/acceleratorProfiles/screens/list/AcceleratorProfileEnableToggle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
31 changes: 31 additions & 0 deletions
31
frontend/src/pages/acceleratorProfiles/screens/list/DisableAcceleratorProfileModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |