mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
Merge https://github.com/google/adk-python/pull/1629 close https://github.com/google/adk-python/issues/2170 ### Summary This PR introduces `GkeCodeExecutor`, a new code executor that provides a secure and scalable method for running LLM-generated code by leveraging GKE Sandbox. It serves as a robust alternative to local or standard containerized executors by leveraging the **GKE Sandbox** environment, which uses gVisor for workload isolation. For each code execution request, it dynamically creates an ephemeral Kubernetes Job with a hardened Pod configuration, offering significant security benefits and ensuring that each code execution runs in a clean, isolated environment. ### Key Features of GkeCodeExecutor * **Dynamic Job Creation**: Uses the Kubernetes `batch/v1` API to create a new Job for each code snippet. * **Secure Code Mounting**: Injects code into the Pod via a temporary `ConfigMap`, which is mounted to a read-only file. * **gVisor Sandboxing**: Enforces execution within a `gvisor` runtime for kernel-level isolation. * **Hardened Security Context**: Pods run as non-root with all Linux capabilities dropped and a read-only root filesystem. * **Resource Management**: Applies configurable CPU and memory limits to prevent abuse. * **Automatic Cleanup**: Uses the `ttl_seconds_after_finished` feature on Jobs for robust, automatic garbage collection of completed Pods and Jobs. * **Node Scheduling**: The executor uses Kubernetes `tolerations` in its Pod specification. This allows the k8s scheduler to place the execution Pod onto a **_pre-configured_** gVisor-enabled node. * **Module Integration**: The `GkeCodeExecutor` is registered in the `code_executors/__init__.py`, making it available for use by agents. The `ImportError` handling is configured to check for the required `kubernetes` SDK. ### Execution Flow: 1. Agent invokes `GkeCodeExecutor` with the LLM-generated code. 2. The `GkeCodeExecutor` will `execute_code` – creates a temporary `ConfigMap`, and then create a k8s `Job` to run it. 3. This Job runs a standard `python:3.11-slim` container. The image is pulled once to the node and cached. The Job will mount the ConfigMap as `/app/code.py` 4. The GkeCodeExecutor will monitor the Job to completion, fetch `stdout/stderr` logs from the container, return `CodeExecutionResult` to the LlmAgent, and ensure all temp resources are deleted. 5. The calling agent formats the result and provides a final response to the user. If the result contains error, it will retry up to `error_retry_attempts` times. PiperOrigin-RevId: 804511467
51 lines
1.4 KiB
YAML
51 lines
1.4 KiB
YAML
apiVersion: v1
|
|
kind: Namespace
|
|
metadata:
|
|
name: agent-sandbox
|
|
---
|
|
apiVersion: v1
|
|
kind: ServiceAccount
|
|
metadata:
|
|
name: adk-agent-sa
|
|
namespace: agent-sandbox
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: Role
|
|
metadata:
|
|
name: adk-agent-role
|
|
namespace: agent-sandbox
|
|
rules:
|
|
- apiGroups: ["batch"]
|
|
resources: ["jobs"]
|
|
# create: Needed for _batch_v1.create_namespaced_job().
|
|
# watch: Needed for watch.stream(self._batch_v1.list_namespaced_job, ...) to wait for completion
|
|
# list/get: Required for the watch to initialize and to get job details.
|
|
verbs: ["create", "get", "watch", "list", "delete"]
|
|
- apiGroups: [""]
|
|
resources: ["configmaps"]
|
|
# create: Needed mount the agent's code into the Job's Pod.
|
|
# delete: Needed for cleanup in the finally block
|
|
verbs: ["create", "get", "list", "delete"]
|
|
- apiGroups: [""]
|
|
resources: ["pods"]
|
|
# list: Needed to find the correct Pod _core_v1.list_namespaced_pod(label_selector=...)
|
|
verbs: ["get", "list", "delete"]
|
|
- apiGroups: [""]
|
|
# get: Needed for _core_v1.read_namespaced_pod_log() to get the code execution results and logs.
|
|
resources: ["pods/log"]
|
|
verbs: ["get", "list"]
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: RoleBinding
|
|
metadata:
|
|
name: adk-agent-binding
|
|
namespace: agent-sandbox
|
|
subjects:
|
|
- kind: ServiceAccount
|
|
name: adk-agent-sa
|
|
namespace: agent-sandbox
|
|
roleRef:
|
|
kind: Role
|
|
name: adk-agent-role
|
|
apiGroup: rbac.authorization.k8s.io
|