-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(13045): mr permissions and rolebindings (#3249)
Signed-off-by: gitdallas <5322142+gitdallas@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
242 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify'; | ||
import { secureAdminRoute } from '../../../utils/route-security'; | ||
import { KubeFastifyInstance } from '../../../types'; | ||
import { | ||
createModelRegistryRoleBinding, | ||
deleteModelRegistriesRolebinding, | ||
listModelRegistryRoleBindings, | ||
} from './modelRegistryRolebindingsUtils'; | ||
import { V1RoleBinding } from '@kubernetes/client-node'; | ||
import { getModelRegistryNamespace } from '../modelRegistries/modelRegistryUtils'; | ||
|
||
export default async (fastify: KubeFastifyInstance): Promise<void> => { | ||
fastify.get( | ||
`/`, | ||
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => { | ||
try { | ||
const mrNamespace = getModelRegistryNamespace(fastify); | ||
return listModelRegistryRoleBindings(fastify, mrNamespace); | ||
} catch (e) { | ||
fastify.log.error( | ||
`ModelRegistry RoleBindings could not be listed, ${ | ||
e.response?.body?.message || e.message | ||
}`, | ||
); | ||
reply.send(e); | ||
} | ||
}), | ||
); | ||
|
||
fastify.post( | ||
'/', | ||
secureAdminRoute(fastify)( | ||
async (request: FastifyRequest<{ Body: V1RoleBinding }>, reply: FastifyReply) => { | ||
const rbRequest = request.body; | ||
try { | ||
const mrNamespace = getModelRegistryNamespace(fastify); | ||
return createModelRegistryRoleBinding(fastify, rbRequest, mrNamespace); | ||
} catch (e) { | ||
if (e.response?.statusCode === 409) { | ||
fastify.log.warn(`Rolebinding already present, skipping creation.`); | ||
return {}; | ||
} | ||
|
||
fastify.log.error( | ||
`rolebinding could not be created: ${e.response?.body?.message || e.message}`, | ||
); | ||
reply.send(new Error(e.response?.body?.message)); | ||
} | ||
}, | ||
), | ||
); | ||
|
||
fastify.delete( | ||
'/:name', | ||
secureAdminRoute(fastify)( | ||
async (request: FastifyRequest<{ Params: { name: string } }>, reply: FastifyReply) => { | ||
const modelRegistryNamespace = await getModelRegistryNamespace(fastify); | ||
const { name } = request.params; | ||
try { | ||
const mrNamespace = getModelRegistryNamespace(fastify); | ||
return deleteModelRegistriesRolebinding(fastify, name, mrNamespace); | ||
} catch (e) { | ||
fastify.log.error( | ||
`RoleBinding ${name} could not be deleted from ${modelRegistryNamespace}, ${ | ||
e.response?.body?.message || e.message | ||
}`, | ||
); | ||
reply.send(e); | ||
} | ||
}, | ||
), | ||
); | ||
}; |
54 changes: 54 additions & 0 deletions
54
backend/src/routes/api/modelRegistryRoleBindings/modelRegistryRolebindingsUtils.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,54 @@ | ||
import { K8sStatus, KnownLabels, KubeFastifyInstance } from '../../../types'; | ||
import { V1RoleBinding } from '@kubernetes/client-node'; | ||
|
||
const MODEL_REGISTRY_ROLE_BINDING_API_GROUP = 'rbac.authorization.k8s.io'; | ||
const MODEL_REGISTRY_ROLE_BINDING_API_VERSION = 'v1'; | ||
const MODEL_REGISTRY_ROLE_BINDING_PLURAL = 'rolebindings'; | ||
|
||
export const listModelRegistryRoleBindings = async ( | ||
fastify: KubeFastifyInstance, | ||
mrNamespace: string, | ||
): Promise<{ items: V1RoleBinding[] }> => { | ||
const response = await (fastify.kube.customObjectsApi.listNamespacedCustomObject( | ||
MODEL_REGISTRY_ROLE_BINDING_API_GROUP, | ||
MODEL_REGISTRY_ROLE_BINDING_API_VERSION, | ||
mrNamespace, | ||
MODEL_REGISTRY_ROLE_BINDING_PLURAL, | ||
undefined, | ||
undefined, | ||
undefined, | ||
KnownLabels.LABEL_SELECTOR_MODEL_REGISTRY, | ||
) as Promise<{ body: { items: V1RoleBinding[] } }>); | ||
return response.body; | ||
}; | ||
|
||
export const createModelRegistryRoleBinding = async ( | ||
fastify: KubeFastifyInstance, | ||
rbRequest: V1RoleBinding, | ||
mrNamespace: string, | ||
): Promise<V1RoleBinding> => { | ||
const response = await (fastify.kube.customObjectsApi.createNamespacedCustomObject( | ||
MODEL_REGISTRY_ROLE_BINDING_API_GROUP, | ||
MODEL_REGISTRY_ROLE_BINDING_API_VERSION, | ||
mrNamespace, | ||
MODEL_REGISTRY_ROLE_BINDING_PLURAL, | ||
rbRequest, | ||
) as Promise<{ body: V1RoleBinding }>); | ||
return response.body; | ||
}; | ||
|
||
export const deleteModelRegistriesRolebinding = async ( | ||
fastify: KubeFastifyInstance, | ||
roleBindingName: string, | ||
mrNamespace: string, | ||
): Promise<K8sStatus> => { | ||
const response = await (fastify.kube.customObjectsApi.deleteNamespacedCustomObject( | ||
MODEL_REGISTRY_ROLE_BINDING_API_GROUP, | ||
MODEL_REGISTRY_ROLE_BINDING_API_VERSION, | ||
mrNamespace, | ||
MODEL_REGISTRY_ROLE_BINDING_PLURAL, | ||
roleBindingName, | ||
) as Promise<{ body: K8sStatus }>); | ||
|
||
return response.body; | ||
}; |
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
Oops, something went wrong.