enterprise_gateway icon indicating copy to clipboard operation
enterprise_gateway copied to clipboard

change V1Subject to RbacV1Subject in k8s.py

Open merqri opened this issue 1 year ago • 5 comments

This change fix - Error: module 'kubernetes.client' has no attribute 'V1Subject'

This error occurs when an attempt is made to create a role binding. The full error looks as follows:

[E 2024-06-03 08:58:36.252 EnterpriseGatewayApp] Error occurred creating role binding for namespace 'jovyan-153ca7c1-c68f-440d-b678-f8926b48afbc': module 'kubernetes.client' has no attribute 'V1Subject'

merqri avatar Jun 03 '24 10:06 merqri

Thanks for submitting your first pull request! You are awesome! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please make sure you followed the pull request template, as this will help us review your contribution more quickly. welcome You can meet the other Jovyans by joining our Discourse forum. There is also a intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

welcome[bot] avatar Jun 03 '24 10:06 welcome[bot]

Hi @merqri and @fm2022aa. I'm curious why this hasn't always been an issue. Was there a version update that triggered this? Does this only occur under certain custom role configurations? (Sorry, been out of the loop for a while and would like to better understand what prompts this change.)

kevin-bates avatar Jun 03 '24 15:06 kevin-bates

Sorry, just caught up on #1379.

Since this appears to be due to version changes, do we need to concern ourselves with breaking older installations?

@lresende - looks like you had some thoughts on this (capping client?). Does that prevent use on newer versions of k8s like 1.30 (or even 1.29)?

kevin-bates avatar Jun 03 '24 15:06 kevin-bates

Capping the client to 1.28 would make it work for current users, but would make it fail for users that have upgraded to >= 1.29 then... Another approach would be to try to check in the code for the current version and use one or the other.

lresende avatar Jun 03 '24 15:06 lresende

Does that prevent use on newer versions of k8s like 1.30 (or even 1.29)?

@lresende @kevin-bates I am running Kubernetes 1.31 with EG 3.2.2 since 3.2.3 has this issue, and everything works fine as far as I can see. That said, I think this PR is backward-compatible as looking at the commit that changes V1Subject to RbacV1Subject, only the model name is updated. I don't think this is a breaking change in the API, as I couldn't find any mentions of it in the v29 changelog.

huynhsontung avatar Feb 28 '25 02:02 huynhsontung

@merqri how about we do something like:

+def get_subject_class():
+    """
+    Returns the appropriate Subject class based on the kubernetes client version.
+
+    In kubernetes-client, V1Subject was renamed to RbacV1Subject.
+    This function returns the appropriate class based on the installed version.
+    """
+    # Check if V1Subject exists in the client
+    if hasattr(client, 'V1Subject'):
+        logging.debug("Using client.V1Subject for Kubernetes client version: %s", kubernetes.__version__)
+        return client.V1Subject
+    # Fall back to RbacV1Subject for older versions
+    logging.debug("Using client.RbacV1Subject for Kubernetes client version: %s", kubernetes.__version__)
+    return client.RbacV1Subject
+

and then something like:

-        binding_subjects = client.RbacV1Subject(
+        # Use the appropriate Subject class based on kubernetes client version
+        SubjectClass = get_subject_class()
+        binding_subjects = SubjectClass(
             api_group="", kind="ServiceAccount", name=service_account_name, namespace=namespace
         )

if you are ok I can push an update to your pr.

lresende avatar Jul 01 '25 05:07 lresende