def remove_rolebinding()

in lambdas/kf_profile_manager/index.py [0:0]


def remove_rolebinding(user_name: str, namespace: str):
    custom_objects_api = client.CustomObjectsApi(api_client)
    rolebindings = custom_objects_api.list_namespaced_custom_object(
        group="rbac.authorization.k8s.io",
        version="v1",
        namespace=f"{namespace}",
        plural="rolebindings",
        pretty="true"
    )
    # iterate over rolebindings to find if new one should be created or old one exists
    for rb in rolebindings['items']:
        rb_metadata_name = rb['metadata']['name']

        if rb_metadata_name == f"user-{user_name}-kubeflow-org-clusterrole-edit":
            logger.info(
                f"delete_namespaced_custom_object:rolebinding user-{user_name}-kubeflow-org-clusterrole-edit in ns={namespace}")
            try:
                api_response = custom_objects_api.delete_namespaced_custom_object(
                    group="rbac.authorization.k8s.io",
                    version="v1",
                    namespace=f"{namespace}",
                    plural="rolebindings",
                    name=f"user-{user_name}-kubeflow-org-clusterrole-edit",
                    grace_period_seconds="0"
                )
            except ApiException as e:
                logger.info("Exception when calling CustomObjectsApi->delete_namespaced_custom_object: %s\n" % e)