Skip to content

Commit 07d131b

Browse files
committed
Support both Kubernetes RbacV1Subject and V1Subject
1 parent 5034915 commit 07d131b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ clean-env: ## Remove conda env
6767
lint: ## Check code style
6868
@pip install -q -e ".[lint]"
6969
@pip install -q pipx
70-
ruff .
70+
ruff check .
7171
black --check --diff --color .
7272
mdformat --check *.md
7373
pipx run 'validate-pyproject[all]' pyproject.toml

enterprise_gateway/services/processproxies/k8s.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
from typing import Any
1212

13+
import kubernetes
1314
import urllib3
1415
from jinja2 import BaseLoader, Environment
1516
from kubernetes import client, config
@@ -34,6 +35,26 @@
3435
config.load_incluster_config()
3536

3637

38+
def get_subject_class():
39+
"""
40+
Returns the appropriate Subject class based on the kubernetes client version.
41+
42+
In kubernetes-client, V1Subject was renamed to RbacV1Subject.
43+
This function returns the appropriate class based on the installed version.
44+
"""
45+
# Check if V1Subject exists in the client
46+
if hasattr(client, 'V1Subject'):
47+
logging.debug(
48+
"Using client.V1Subject for Kubernetes client version: %s", kubernetes.__version__
49+
)
50+
return client.V1Subject
51+
# Fall back to RbacV1Subject for older versions
52+
logging.debug(
53+
"Using client.RbacV1Subject for Kubernetes client version: %s", kubernetes.__version__
54+
)
55+
return client.RbacV1Subject
56+
57+
3758
class KubernetesProcessProxy(ContainerProcessProxy):
3859
"""
3960
Kernel lifecycle management for Kubernetes kernels.
@@ -349,7 +370,9 @@ def _create_role_binding(self, namespace: str, service_account_name: str) -> Non
349370
binding_role_ref = client.V1RoleRef(
350371
api_group="", kind="ClusterRole", name=kernel_cluster_role
351372
)
352-
binding_subjects = client.RbacV1Subject(
373+
# Use the appropriate Subject class based on kubernetes client version
374+
SubjectClass = get_subject_class()
375+
binding_subjects = SubjectClass(
353376
api_group="", kind="ServiceAccount", name=service_account_name, namespace=namespace
354377
)
355378

0 commit comments

Comments
 (0)