diff --git a/Makefile b/Makefile index ec998fb0e..33259cc38 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,7 @@ clean-env: ## Remove conda env lint: ## Check code style @pip install -q -e ".[lint]" @pip install -q pipx - ruff . + ruff check . black --check --diff --color . mdformat --check *.md pipx run 'validate-pyproject[all]' pyproject.toml diff --git a/enterprise_gateway/services/processproxies/k8s.py b/enterprise_gateway/services/processproxies/k8s.py index 00e6bf170..2d064e0da 100644 --- a/enterprise_gateway/services/processproxies/k8s.py +++ b/enterprise_gateway/services/processproxies/k8s.py @@ -10,6 +10,7 @@ import re from typing import Any +import kubernetes import urllib3 from jinja2 import BaseLoader, Environment from kubernetes import client, config @@ -34,6 +35,26 @@ config.load_incluster_config() +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 + + class KubernetesProcessProxy(ContainerProcessProxy): """ Kernel lifecycle management for Kubernetes kernels. @@ -349,7 +370,9 @@ def _create_role_binding(self, namespace: str, service_account_name: str) -> Non binding_role_ref = client.V1RoleRef( api_group="", kind="ClusterRole", name=kernel_cluster_role ) - binding_subjects = client.V1Subject( + # 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 )