-
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.
clean up separated components fixed js key error fix tests tooltips to popover and direction of popup wording fixes fixes removed popover for toleration table
- Loading branch information
1 parent
0b7492e
commit 79d8b86
Showing
31 changed files
with
1,565 additions
and
68 deletions.
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
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,170 @@ | ||
import { KubeFastifyInstance, AcceleratorKind } from '../../../types'; | ||
import { FastifyRequest } from 'fastify'; | ||
import createError from 'http-errors'; | ||
import { translateDisplayNameForK8s } from '../../../utils/resourceUtils'; | ||
|
||
export const getAcceleratorProfiles = async ( | ||
fastify: KubeFastifyInstance, | ||
): Promise<AcceleratorKind[]> => { | ||
const customObjectsApi = fastify.kube.customObjectsApi; | ||
const namespace = fastify.kube.namespace; | ||
return await customObjectsApi | ||
.listNamespacedCustomObject('dashboard.opendatahub.io', 'v1', namespace, 'acceleratorprofiles') | ||
.then((res) => { | ||
const list = ( | ||
res?.body as { | ||
items: AcceleratorKind[]; | ||
} | ||
).items; | ||
return list; | ||
}) | ||
.catch((e) => { | ||
fastify.log.error(e); | ||
return []; | ||
}); | ||
}; | ||
|
||
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 newResourceName = translateDisplayNameForK8s(body.displayName); | ||
const acceleratorProfiles = (await getAcceleratorProfiles(fastify)) as AcceleratorKind[]; | ||
const validName = acceleratorProfiles.filter((ap) => ap.metadata.name === newResourceName); | ||
|
||
if (validName.length > 0) { | ||
fastify.log.error('Duplicate name unable to add accelerator profile'); | ||
return { success: false, error: 'Unable to add accelerator profile: ' + newResourceName }; | ||
} | ||
|
||
const payload: AcceleratorKind = { | ||
apiVersion: 'dashboard.opendatahub.io/v1', | ||
kind: 'AcceleratorProfile', | ||
metadata: { | ||
name: newResourceName, | ||
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 }; | ||
} | ||
} | ||
}; | ||
|
||
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) { | ||
fastify.log.error(e, 'Unable to delete accelerator profile.'); | ||
return { success: false, error: 'Unable to delete accelerator profile: ' + e.message }; | ||
} | ||
}; | ||
|
||
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 | ||
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,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.