Skip to content

Commit 67ebf38

Browse files
syangx39ADK Coding Agent Project
authored andcommitted
[08/28] fix kubeconfig
1 parent d3c1dd0 commit 67ebf38

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

‎src/google/adk/code_executors/gke_code_executor.py‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,50 @@ class GkeCodeExecutor(BaseCodeExecutor):
5353
cpu_limit: str = "500m"
5454
mem_limit: str = "512Mi"
5555

56+
kubeconfig_path: str | None = None
57+
kubeconfig_context: str | None = None
58+
5659
_batch_v1: client.BatchV1Api
5760
_core_v1: client.CoreV1Api
5861

59-
def __init__(self, **data):
62+
def __init__(
63+
self,
64+
kubeconfig_path: str | None = None,
65+
kubeconfig_context: str | None = None,
66+
**data
67+
):
6068
"""Initializes the executor and the Kubernetes API clients.
6169
62-
This constructor supports overriding default class attributes (like
63-
'namespace', 'image', etc.) by passing them as keyword arguments. It
64-
automatically configures the Kubernetes client to work either within a
65-
cluster (in-cluster config) or locally using a kubeconfig file.
70+
This constructor supports multiple authentication methods:
71+
1. Explicitly via a kubeconfig file path and context.
72+
2. Automatically via in-cluster service account (when running in GKE).
73+
3. Automatically via the default local kubeconfig file (~/.kube/config).
6674
"""
6775
super().__init__(**data)
68-
try:
69-
config.load_incluster_config()
70-
logger.info("Using in-cluster Kubernetes configuration.")
71-
except config.ConfigException:
72-
logger.info("In-cluster config not found. Falling back to local kubeconfig.")
73-
config.load_kube_config()
76+
self.kubeconfig_path = kubeconfig_path
77+
self.kubeconfig_context = kubeconfig_context
78+
79+
if self.kubeconfig_path:
80+
try:
81+
logger.info(f"Using explicit kubeconfig from '{self.kubeconfig_path}'.")
82+
config.load_kube_config(
83+
config_file=self.kubeconfig_path,
84+
context=self.kubeconfig_context
85+
)
86+
except config.ConfigException as e:
87+
logger.error(f"Failed to load explicit kubeconfig from {self.kubeconfig_path}", exc_info=True)
88+
raise RuntimeError("Failed to configure Kubernetes client from provided path.") from e
89+
else:
90+
try:
91+
config.load_incluster_config()
92+
logger.info("Using in-cluster Kubernetes configuration.")
93+
except config.ConfigException:
94+
try:
95+
logger.info("In-cluster config not found. Falling back to default local kubeconfig.")
96+
config.load_kube_config()
97+
except config.ConfigException as e:
98+
logger.error("Could not configure Kubernetes client automatically.", exc_info=True)
99+
raise RuntimeError("Failed to find any valid Kubernetes configuration.") from e
74100

75101
self._batch_v1 = client.BatchV1Api()
76102
self._core_v1 = client.CoreV1Api()

0 commit comments

Comments
 (0)