|
18 | 18 | import uuid |
19 | 19 |
|
20 | 20 | from agentic_sandbox import SandboxClient |
21 | | - |
22 | 21 | import kubernetes as k8s |
23 | 22 | from kubernetes.watch import Watch |
24 | 23 |
|
@@ -92,7 +91,7 @@ class GkeCodeExecutor(BaseCodeExecutor): |
92 | 91 |
|
93 | 92 | kubeconfig_path: str | None = None |
94 | 93 | kubeconfig_context: str | None = None |
95 | | - |
| 94 | + |
96 | 95 | # Sandbox constants |
97 | 96 | sandbox_template: str = "python-sandbox-template" |
98 | 97 |
|
@@ -122,9 +121,10 @@ def __init__( |
122 | 121 | self.kubeconfig_path = kubeconfig_path |
123 | 122 | self.kubeconfig_context = kubeconfig_context |
124 | 123 |
|
125 | | - if executor_type not in ['job', 'sandbox']: |
| 124 | + if executor_type not in ["job", "sandbox"]: |
126 | 125 | raise ValueError( |
127 | | - f"Invalid executor_type: '{executor_type}'. Must be 'job' or 'sandbox'." |
| 126 | + f"Invalid executor_type: '{executor_type}'. Must be 'job' or" |
| 127 | + " 'sandbox'." |
128 | 128 | ) |
129 | 129 | if self.kubeconfig_path: |
130 | 130 | try: |
@@ -166,24 +166,26 @@ def __init__( |
166 | 166 | def _execute_in_sandbox(self, code: str) -> CodeExecutionResult: |
167 | 167 | """Executes code using Agent Sandbox Client.""" |
168 | 168 | try: |
169 | | - with SandboxClient( |
170 | | - template_name=self.sandbox_template, |
171 | | - gateway_name=self.sandbox_gateway_name, |
172 | | - namespace=self.namespace |
173 | | - ) as sandbox: |
174 | | - # Execute the code as a python script |
175 | | - logger.debug("Executing code in sandbox:\n```\n%s\n```", code) |
176 | | - sandbox.write("script.py", code) |
177 | | - result = sandbox.run("python3 script.py") |
178 | | - |
179 | | - return CodeExecutionResult(stdout=result.stdout) |
| 169 | + with SandboxClient( |
| 170 | + template_name=self.sandbox_template, |
| 171 | + gateway_name=self.sandbox_gateway_name, |
| 172 | + namespace=self.namespace, |
| 173 | + ) as sandbox: |
| 174 | + # Execute the code as a python script |
| 175 | + logger.debug("Executing code in sandbox:\n```\n%s\n```", code) |
| 176 | + sandbox.write("script.py", code) |
| 177 | + result = sandbox.run("python3 script.py") |
| 178 | + |
| 179 | + return CodeExecutionResult(stdout=result.stdout) |
180 | 180 | except Exception as e: |
181 | | - logger.error("Sandbox execution failed", exc_info=True) |
182 | | - return CodeExecutionResult( |
183 | | - stderr=f"Sandbox execution failed: {str(e)}", |
184 | | - ) |
| 181 | + logger.error("Sandbox execution failed", exc_info=True) |
| 182 | + return CodeExecutionResult( |
| 183 | + stderr=f"Sandbox execution failed: {str(e)}", |
| 184 | + ) |
185 | 185 |
|
186 | | - def _execute_as_job(self, code: str, invocation_context: InvocationContext) -> CodeExecutionResult: |
| 186 | + def _execute_as_job( |
| 187 | + self, code: str, invocation_context: InvocationContext |
| 188 | + ) -> CodeExecutionResult: |
187 | 189 | """Orchestrates the secure execution of a code snippet on GKE.""" |
188 | 190 | job_name = f"adk-exec-{uuid.uuid4().hex[:10]}" |
189 | 191 | configmap_name = f"code-src-{job_name}" |
@@ -234,14 +236,13 @@ def execute_code( |
234 | 236 | invocation_context: InvocationContext, |
235 | 237 | code_execution_input: CodeExecutionInput, |
236 | 238 | ) -> CodeExecutionResult: |
237 | | - |
238 | 239 | """Overrides the base method to route execution based on executor_type.""" |
239 | 240 | code = code_execution_input.code |
240 | 241 | if self.executor_type == "sandbox": |
241 | | - return self._execute_in_sandbox(code) |
| 242 | + return self._execute_in_sandbox(code) |
242 | 243 | else: |
243 | | - # Fallback to existing GKE Job logic |
244 | | - return self._execute_as_job(code, invocation_context) |
| 244 | + # Fallback to existing GKE Job logic |
| 245 | + return self._execute_as_job(code, invocation_context) |
245 | 246 |
|
246 | 247 | def _create_job_manifest( |
247 | 248 | self, |
|
0 commit comments