-
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.
Merge pull request #2045 from Gkrumbach07/edit-page
Added accelerator edit page
- Loading branch information
Showing
32 changed files
with
1,517 additions
and
69 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
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,150 @@ | ||
import { KubeFastifyInstance, AcceleratorKind } from '../../../types'; | ||
import { FastifyRequest } from 'fastify'; | ||
import createError from 'http-errors'; | ||
import { translateDisplayNameForK8s } from '../../../utils/resourceUtils'; | ||
|
||
export const postAcceleratorProfile = async ( | ||
fastify: KubeFastifyInstance, | ||
request: FastifyRequest, | ||
): Promise<{ success: boolean; error: string }> => { | ||
const customObjectsApi = fastify.kube.customObjectsApi; | ||
const namespace = fastify.kube.namespace; | ||
const body = request.body as AcceleratorKind['spec']; | ||
|
||
const payload: AcceleratorKind = { | ||
apiVersion: 'dashboard.opendatahub.io/v1', | ||
kind: 'AcceleratorProfile', | ||
metadata: { | ||
name: translateDisplayNameForK8s(body.displayName), | ||
namespace: namespace, | ||
annotations: { | ||
'opendatahub.io/modified-date': new Date().toISOString(), | ||
}, | ||
}, | ||
spec: body, | ||
}; | ||
|
||
try { | ||
await customObjectsApi | ||
.createNamespacedCustomObject( | ||
'dashboard.opendatahub.io', | ||
'v1', | ||
namespace, | ||
'acceleratorprofiles', | ||
payload, | ||
) | ||
.catch((e) => { | ||
throw createError(e.statusCode, e?.body?.message); | ||
}); | ||
return { success: true, error: null }; | ||
} catch (e) { | ||
if (e.response?.statusCode !== 404) { | ||
fastify.log.error(e, 'Unable to add accelerator profile.'); | ||
return { success: false, error: 'Unable to add accelerator profile: ' + e.message }; | ||
} | ||
throw e; | ||
} | ||
}; | ||
|
||
export const deleteAcceleratorProfile = 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 }; | ||
|
||
try { | ||
await customObjectsApi | ||
.deleteNamespacedCustomObject( | ||
'dashboard.opendatahub.io', | ||
'v1', | ||
namespace, | ||
'acceleratorprofiles', | ||
params.acceleratorProfileName, | ||
) | ||
.catch((e) => { | ||
throw createError(e.statusCode, e?.body?.message); | ||
}); | ||
return { success: true, error: null }; | ||
} catch (e) { | ||
if (e.response?.statusCode === 404) { | ||
fastify.log.error(e, 'Unable to delete accelerator profile.'); | ||
return { success: false, error: 'Unable to delete accelerator profile: ' + e.message }; | ||
} | ||
throw e; | ||
} | ||
}; | ||
|
||
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); | ||
}); | ||
|
||
if (body.displayName !== undefined) { | ||
currentProfile.spec.displayName = body.displayName; | ||
} | ||
if (body.enabled !== undefined) { | ||
currentProfile.spec.enabled = body.enabled; | ||
} | ||
if (body.identifier !== undefined) { | ||
currentProfile.spec.identifier = body.identifier; | ||
} | ||
if (body.description !== undefined) { | ||
currentProfile.spec.description = body.description; | ||
} | ||
if (body.tolerations !== undefined) { | ||
currentProfile.spec.tolerations = body.tolerations; | ||
} | ||
|
||
// Update the modified date annotation | ||
if (!currentProfile.metadata.annotations) { | ||
currentProfile.metadata.annotations = {}; | ||
} | ||
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) { | ||
if (e.response?.statusCode !== 404) { | ||
fastify.log.error(e, 'Unable to update accelerator profile.'); | ||
return { success: false, error: 'Unable to update accelerator profile: ' + e.message }; | ||
} | ||
throw e; | ||
} | ||
}; |
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 { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; | ||
import { secureAdminRoute } from '../../../utils/route-security'; | ||
import { | ||
deleteAcceleratorProfile, | ||
postAcceleratorProfile, | ||
updateAcceleratorProfile, | ||
} from './acceleratorProfilesUtils'; | ||
|
||
export default async (fastify: FastifyInstance): Promise<void> => { | ||
fastify.delete( | ||
'/:acceleratorProfileName', | ||
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => { | ||
return deleteAcceleratorProfile(fastify, request) | ||
.then((res) => { | ||
return res; | ||
}) | ||
.catch((res) => { | ||
reply.send(res); | ||
}); | ||
}), | ||
); | ||
|
||
fastify.put( | ||
'/:acceleratorProfileName', | ||
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => { | ||
return updateAcceleratorProfile(fastify, request) | ||
.then((res) => { | ||
return res; | ||
}) | ||
.catch((res) => { | ||
reply.send(res); | ||
}); | ||
}), | ||
); | ||
|
||
fastify.post( | ||
'/', | ||
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => { | ||
return postAcceleratorProfile(fastify, request) | ||
.then((res) => { | ||
return res; | ||
}) | ||
.catch((res) => { | ||
reply.send(res); | ||
}); | ||
}), | ||
); | ||
}; |
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
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
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 |
---|---|---|
@@ -1,22 +1,34 @@ | ||
import _ from 'lodash'; | ||
import { AcceleratorKind } from '~/k8sTypes'; | ||
import { RecursivePartial } from '~/typeHelpers'; | ||
import { TolerationEffect, TolerationOperator } from '~/types'; | ||
|
||
export const mockAcceleratorProfile = (): AcceleratorKind => ({ | ||
apiVersion: 'dashboard.opendatahub.io/v1', | ||
kind: 'AcceleratorProfile', | ||
metadata: { | ||
name: 'test-accelerator', | ||
}, | ||
spec: { | ||
displayName: 'test-accelerator', | ||
enabled: true, | ||
identifier: 'nvidia.com/gpu', | ||
description: 'Test description', | ||
tolerations: [ | ||
{ | ||
key: 'nvidia.com/gpu', | ||
operator: 'Exists', | ||
effect: 'NoSchedule', | ||
export const mockAcceleratorProfile = ( | ||
data: RecursivePartial<AcceleratorKind> = {}, | ||
): AcceleratorKind => | ||
_.merge( | ||
{ | ||
apiVersion: 'dashboard.opendatahub.io/v1', | ||
kind: 'AcceleratorProfile', | ||
metadata: { | ||
name: 'test-accelerator', | ||
annotations: { | ||
'opendatahub.io/modified-date': '2023-10-31T21:16:11.721Z', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
spec: { | ||
displayName: 'Test Accelerator', | ||
enabled: true, | ||
identifier: 'nvidia.com/gpu', | ||
description: 'Test description', | ||
tolerations: [ | ||
{ | ||
key: 'nvidia.com/gpu', | ||
operator: TolerationOperator.EXISTS, | ||
effect: TolerationEffect.NO_SCHEDULE, | ||
}, | ||
], | ||
}, | ||
} as AcceleratorKind, | ||
data, | ||
); |
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
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
Oops, something went wrong.