|
| 1 | +# Agentic Sandbox Client Python |
| 2 | + |
| 3 | +This Python client provides a simple, high-level interface for creating and interacting with sandboxes managed by the Agent Sandbox controller. It's designed to be used as a context manager, ensuring that sandbox resources are properly created and cleaned up. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- A running Kubernetes cluster (e.g., `kind`). |
| 10 | +- The Agent Sandbox controller must be deployed with the extensions feature enabled. |
| 11 | +- A `SandboxTemplate` resource must be created in the cluster. |
| 12 | +- The `kubectl` command-line tool must be installed and configured to connect to your cluster. |
| 13 | + |
| 14 | +### Installation |
| 15 | + |
| 16 | +1. **Create a virtual environment:** |
| 17 | + ```bash |
| 18 | + python3 -m venv .venv |
| 19 | + ``` |
| 20 | +2. **Activate the virtual environment:** |
| 21 | + ```bash |
| 22 | + source .venv/bin/activate |
| 23 | + ``` |
| 24 | +3. **Option 1: Install from source via git:** |
| 25 | + ```bash |
| 26 | + # Replace "main" with a specific version tag (e.g., "v0.1.0") from |
| 27 | + # https://github.com/kubernetes-sigs/agent-sandbox/releases to pin a version tag. |
| 28 | + export VERSION="main" |
| 29 | +
|
| 30 | + pip install "git+https://github.com/kubernetes-sigs/agent-sandbox.git@${VERSION}#subdirectory=clients/python/agentic-sandbox-client" |
| 31 | + ``` |
| 32 | +4. **Option 2: Install from source in editable mode:** |
| 33 | + ```bash |
| 34 | + git clone https://github.com/kubernetes-sigs/agent-sandbox.git |
| 35 | + cd agent-sandbox/clients/agentic-sandbox-client-python |
| 36 | + cd ~/path_to_venv |
| 37 | + pip install -e . |
| 38 | + ``` |
| 39 | + |
| 40 | +### Example: |
| 41 | + |
| 42 | +```python |
| 43 | +from agentic_sandbox import SandboxClient |
| 44 | +
|
| 45 | +with SandboxClient(template_name="sandbox-python-template", namespace="default") as sandbox: |
| 46 | + result = sandbox.run("echo 'Hello, World!'") |
| 47 | + print(result.stdout) |
| 48 | +``` |
| 49 | + |
| 50 | +## How It Works |
| 51 | + |
| 52 | +The `SandboxClient` client automates the entire lifecycle of a temporary sandbox environment: |
| 53 | + |
| 54 | +1. **Initialization (`with SandboxClient(...)`):** The client is initialized with the name of the `SandboxTemplate` you want to use and the namespace where the resources should be created: |
| 55 | + |
| 56 | + - **`template_name` (str):** The name of the `SandboxTemplate` resource to use for creating the sandbox. |
| 57 | + - **`namespace` (str, optional):** The Kubernetes namespace to create the `SandboxClaim` in. Defaults to "default". |
| 58 | + - When you create a `SandboxClient` instance within a `with` block, it initiates the process of creating a sandbox. |
| 59 | +2. **Claim Creation:** It creates a `SandboxClaim` Kubernetes resource. This claim tells the agent-sandbox controller to provision a new sandbox using a predefined `SandboxTemplate`. |
| 60 | +3. **Waiting for Readiness:** The client watches the Kubernetes API for the corresponding `Sandbox` resource to be created and become "Ready". This indicates that the pod is running and the server inside is active. |
| 61 | +4. **Port Forwarding:** Once the sandbox pod is ready, the client automatically starts a `kubectl port-forward` process in the background. This creates a secure tunnel from your local machine to the sandbox pod, allowing you to communicate with the server running inside. |
| 62 | +5. **Interaction:** The `SandboxClient` object provides three main methods to interact with the running sandbox: |
| 63 | + * `run(command)`: Executes a shell command inside the sandbox. |
| 64 | + * `write(path, content)`: Uploads a file to the sandbox. |
| 65 | + * `read(path)`: Downloads a file from the sandbox. |
| 66 | +6. **Cleanup (`__exit__`):** When the `with` block is exited (either normally or due to an error), the client automatically cleans up all resources. It terminates the `kubectl port-forward` process and deletes the `SandboxClaim`, which in turn causes the controller to delete the `Sandbox` pod. |
| 67 | + |
| 68 | + |
| 69 | +## How to Test the Client |
| 70 | + |
| 71 | +A test script, `test_client.py`, is included to verify the client's functionality. |
| 72 | +You should see output indicating that the tests for command execution and file operations have passed. |
| 73 | +
|
| 74 | +## Packaging and Installation |
| 75 | +
|
| 76 | +This client is configured as a standard Python package using `pyproject.toml`. |
| 77 | +
|
| 78 | +### Prerequisites |
| 79 | +
|
| 80 | +- Python 3.7+ |
| 81 | +- `pip` |
| 82 | +- `build` (install with `pip install build`) |
| 83 | +
|
| 84 | +### Building the Package |
| 85 | +
|
| 86 | +To build the package from the source, navigate to the `agentic-sandbox-client` directory and run the following command: |
| 87 | +
|
| 88 | +```bash |
| 89 | +python -m build |
| 90 | +``` |
| 91 | +
|
| 92 | +This will create a `dist` directory containing the packaged distributables: a `.whl` (wheel) file and a `.tar.gz` (source archive). |
0 commit comments