-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(code_executors): Add GkeCodeExecutor for sandboxed code execution on GKE #1629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
0c4d3f5
[06/24] Add gke_code_executor.py
syangx39 f28f4b7
[06/24] Add gke_code_executor.py
syangx39 760fb7f
[06/24] Add gke_code_executor.py
syangx39 6b0e772
[06/24] Add gke_code_executor.py
syangx39 33446f8
[07/10] Add deployemnt_rbac.yaml into samples folder
syangx39 d09c319
[07/10] Add deployemnt_rbac.yaml into samples folder
syangx39 1f06fe1
[07/10] Add deployemnt_rbac.yaml into samples folder
syangx39 254bdcb
[07/10] Make cpu/memory requests configurable
syangx39 285b53a
[07/10] Add annotation
syangx39 6a93e12
[07/10] Raise error in _get_pod_logs()
syangx39 2b16d7b
[07/10] Add owner_reference
syangx39 9982d39
[07/10] Add owner_reference
syangx39 6685f63
[07/25] add the sample
syangx39 efd7a76
[07/25] modify pyproject.toml to add gke-specific dependency
syangx39 6914958
[08/15] rename cpu_request
syangx39 5b7595c
[08/15] rename cpu_request
syangx39 1a6eca2
[08/15] rename cpu_request
syangx39 6010a3b
[08/25] Modify ADK
syangx39 4dc4753
[08/25] Modify ADK
syangx39 9ec5e8b
[08/25] Modify ADK
syangx39 3c962ff
[08/25] Modify ADK
syangx39 ae13800
[08/25] Modify ADK
syangx39 d3c1dd0
[08/25] Modify ADK
syangx39 67ebf38
[08/28] fix kubeconfig
syangx39 5a7002a
fix(gke): add future annotations for py3.9 compatibility #non-breaking
GWeale 7b40d22
fix(gke_code_executor): add annotations to the fil
GWeale 2049d68
style(gke): use relative imports; no cli imports #non-breaking
GWeale adb2ad4
style(gke): make imports relative; avoid cli import pattern #non-brea…
GWeale 75814e9
test(gke): expose client/config for monkeypatch; fix imports #non-bre…
GWeale cd6d518
chore(pyproject): keep-sorted ordering for kubernetes entries #non-br…
GWeale 3f57383
Merge branch 'main' into main
GWeale 869ae1a
Merge branch 'main' into main
hangfei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """A Python coding agent using the GkeCodeExecutor for secure execution.""" | ||
|
|
||
| from google.adk.agents import LlmAgent | ||
| from google.adk.code_executors import GkeCodeExecutor | ||
|
|
||
|
|
||
| def gke_agent_system_instruction(): | ||
| """Returns: The system instruction for the GKE-based coding agent.""" | ||
| return """You are a helpful and capable AI agent that can write and execute Python code to answer questions and perform tasks. | ||
|
|
||
| When a user asks a question, follow these steps: | ||
| 1. Analyze the request. | ||
| 2. Write a complete, self-contained Python script to accomplish the task. | ||
| 3. Your code will be executed in a secure, sandboxed environment. | ||
| 4. Return the full and complete output from the code execution, including any text, results, or error messages.""" | ||
|
|
||
|
|
||
| gke_executor = GkeCodeExecutor( | ||
| # This must match the namespace in your deployment_rbac.yaml where the | ||
| # agent's ServiceAccount and Role have permissions. | ||
| namespace="agent-sandbox", | ||
| # Setting an explicit timeout prevents a stuck job from running forever. | ||
| timeout_seconds=600, | ||
| ) | ||
|
|
||
| root_agent = LlmAgent( | ||
| name="gke_coding_agent", | ||
| model="gemini-2.0-flash", | ||
| description=( | ||
| "A general-purpose agent that executes Python code in a secure GKE" | ||
| " Sandbox." | ||
| ), | ||
| instruction=gke_agent_system_instruction(), | ||
| code_executor=gke_executor, | ||
| ) |
50 changes: 50 additions & 0 deletions
50
contributing/samples/gke_agent_sandbox/deployment_rbac.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.