-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetching resources names from the Account CR (#3535)
* feat: fetching resources names from the Account CR Signed-off-by: Olga Lavtar <[email protected]> * feat: fixed unit tests Signed-off-by: Olga Lavtar <[email protected]> * feat: fixed cypress tests Signed-off-by: Olga Lavtar <[email protected]> * feat: fixed unit test after code changes. Signed-off-by: Olga Lavtar <[email protected]> * feat: applied changes based on the comments. Signed-off-by: Olga Lavtar <[email protected]> * feat: rename accounts.ts to nimAccounts.ts Signed-off-by: Olga Lavtar <[email protected]> * fix: pass resource key instead of the resource name. Signed-off-by: Olga Lavtar <[email protected]> * fix: pass resource key instead of the resource name. Signed-off-by: Olga Lavtar <[email protected]> * fix: remove NIMAccountConstants Signed-off-by: Olga Lavtar <[email protected]> * fix: unit test fix Signed-off-by: Olga Lavtar <[email protected]> * fix: using lodash to get the resourceName Signed-off-by: Olga Lavtar <[email protected]> * fix: rename listAccounts to listNIMAccounts Signed-off-by: Olga Lavtar <[email protected]> * fix: remove dashboardNamespace Signed-off-by: Olga Lavtar <[email protected]> * fix: remove namespace when calling getNIMAccount, rename createNIMSecret to manageNIMSecret Signed-off-by: Olga Lavtar <[email protected]> --------- Signed-off-by: Olga Lavtar <[email protected]>
- Loading branch information
Showing
20 changed files
with
341 additions
and
114 deletions.
There are no files selected for viewing
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,40 +1,53 @@ | ||
import { KubeFastifyInstance, OauthFastifyRequest } from '../../../types'; | ||
import { createCustomError } from '../../../utils/requestUtils'; | ||
import { logRequestDetails } from '../../../utils/fileUtils'; | ||
|
||
const secretNames = ['nvidia-nim-access', 'nvidia-nim-image-pull']; | ||
const configMapName = 'nvidia-nim-images-data'; | ||
import { getNIMAccount } from '../integrations/nim/nimUtils'; | ||
import { get } from 'lodash'; | ||
|
||
export default async (fastify: KubeFastifyInstance): Promise<void> => { | ||
const resourceMap: Record<string, { type: 'Secret' | 'ConfigMap'; path: string[] }> = { | ||
apiKeySecret: { type: 'Secret', path: ['spec', 'apiKeySecret', 'name'] }, | ||
nimPullSecret: { type: 'Secret', path: ['status', 'nimPullSecret', 'name'] }, | ||
nimConfig: { type: 'ConfigMap', path: ['status', 'nimConfig', 'name'] }, | ||
}; | ||
|
||
fastify.get( | ||
'/:nimResource', | ||
async ( | ||
request: OauthFastifyRequest<{ | ||
Params: { nimResource: string }; | ||
}>, | ||
) => { | ||
async (request: OauthFastifyRequest<{ Params: { nimResource: string } }>) => { | ||
logRequestDetails(fastify, request); | ||
const { nimResource } = request.params; | ||
const { coreV1Api, namespace } = fastify.kube; | ||
|
||
if (secretNames.includes(nimResource)) { | ||
try { | ||
return await coreV1Api.readNamespacedSecret(nimResource, namespace); | ||
} catch (e) { | ||
fastify.log.error(`Failed to fetch secret ${nimResource}: ${e.message}`); | ||
throw createCustomError('Not found', 'Secret not found', 404); | ||
} | ||
// Fetch the Account CR to determine the actual resource name dynamically | ||
const account = await getNIMAccount(fastify); | ||
if (!account) { | ||
throw createCustomError('Not found', 'NIM account not found', 404); | ||
} | ||
|
||
const resourceInfo = resourceMap[nimResource]; | ||
if (!resourceInfo) { | ||
throw createCustomError('Not found', `Invalid resource type: ${nimResource}`, 404); | ||
} | ||
|
||
const resourceName = get(account, resourceInfo.path); | ||
if (!resourceName) { | ||
fastify.log.error(`Resource name for '${nimResource}' not found in account CR.`); | ||
throw createCustomError('Not found', `${nimResource} name not found in account`, 404); | ||
} | ||
|
||
if (nimResource === configMapName) { | ||
try { | ||
return await coreV1Api.readNamespacedConfigMap(configMapName, namespace); | ||
} catch (e) { | ||
fastify.log.error(`Failed to fetch configMap ${nimResource}: ${e.message}`); | ||
throw createCustomError('Not found', 'ConfigMap not found', 404); | ||
try { | ||
// Fetch the resource from Kubernetes using the dynamically retrieved name | ||
if (resourceInfo.type === 'Secret') { | ||
return await coreV1Api.readNamespacedSecret(resourceName, namespace); | ||
} else { | ||
return await coreV1Api.readNamespacedConfigMap(resourceName, namespace); | ||
} | ||
} catch (e: any) { | ||
fastify.log.error( | ||
`Failed to fetch ${resourceInfo.type.toLowerCase()} ${resourceName}: ${e.message}`, | ||
); | ||
throw createCustomError('Not found', `${resourceInfo.type} not found`, 404); | ||
} | ||
throw createCustomError('Not found', 'Resource not found', 404); | ||
}, | ||
); | ||
}; |
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,43 @@ | ||
import { K8sCondition, NIMAccountKind } from '~/k8sTypes'; | ||
|
||
type MockResourceConfigType = { | ||
name?: string; | ||
namespace?: string; | ||
uid?: string; | ||
apiKeySecretName?: string; | ||
nimConfigName?: string; | ||
runtimeTemplateName?: string; | ||
nimPullSecretName?: string; | ||
conditions?: K8sCondition[]; | ||
}; | ||
|
||
export const mockNimAccount = ({ | ||
name = 'odh-nim-account', | ||
namespace = 'opendatahub', | ||
uid = 'test-uid', | ||
apiKeySecretName = 'mock-nvidia-nim-access', | ||
nimConfigName = 'mock-nvidia-nim-images-data', | ||
runtimeTemplateName = 'mock-nvidia-nim-serving-template', | ||
nimPullSecretName = 'mock-nvidia-nim-image-pull', | ||
conditions = [], | ||
}: MockResourceConfigType): NIMAccountKind => ({ | ||
apiVersion: 'nim.opendatahub.io/v1', | ||
kind: 'Account', | ||
metadata: { | ||
name, | ||
namespace, | ||
uid, | ||
creationTimestamp: new Date().toISOString(), | ||
}, | ||
spec: { | ||
apiKeySecret: { | ||
name: apiKeySecretName, | ||
}, | ||
}, | ||
status: { | ||
nimConfig: nimConfigName ? { name: nimConfigName } : undefined, | ||
runtimeTemplate: runtimeTemplateName ? { name: runtimeTemplateName } : undefined, | ||
nimPullSecret: nimPullSecretName ? { name: nimPullSecretName } : undefined, | ||
conditions, | ||
}, | ||
}); |
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
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
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,11 @@ | ||
import { k8sListResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import { NIMAccountModel } from '~/api/models'; | ||
import { NIMAccountKind } from '~/k8sTypes'; | ||
|
||
export const listNIMAccounts = async (namespace: string): Promise<NIMAccountKind[]> => | ||
k8sListResource<NIMAccountKind>({ | ||
model: NIMAccountModel, | ||
queryOptions: { | ||
ns: namespace, | ||
}, | ||
}).then((listResource) => listResource.items); |
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.