Name | autogen-kubernetes JSON |
Version |
0.4.0.dev10
JSON |
| download |
home_page | None |
Summary | autogen kubernetes extension |
upload_time | 2024-12-13 07:59:09 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | MIT License Copyright (c) Microsoft Corporation. Copyright (c) 2024 kiyoung you Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE --- This project includes code from the autogen project, which is licensed under the MIT License. Copyright (c) Microsoft Corporation. Modification Copyright (c) 2024 kiyoung you(questcollector) This project is licensed under the MIT License. See the full license text above --- Third-Party Licenses --------------------- This project uses the following third-party dependencies: 1. Kubernetes Python SDK(https://github.com/kubernetes-client/python) Licensed under the Apache License, Version 2.0. --- 2. httpx(https://github.com/encode/httpx) Licensed under BSD 3-Clause "New" or "Revised" License: Copyright © 2019, Encode OSS Ltd. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- 3. websockets(https://github.com/python-websockets/websockets) Licensed under the BSD 3-Clause "New" or "Revised" License: Copyright (c) Aymeric Augustin and contributors Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- 4. PyYAML(https://github.com/yaml/pyyaml) Licensed under the MIT License: Copyright (c) 2017-2021 Ingy döt Net Copyright (c) 2006-2016 Kirill Simonov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
ai
agent
autogen
kubernetes
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# autogen-kubernetes
This is autogen(>=0.4) kubernetes extension which provides code executor on kuberentes pod
We plan to add "autogen" features needs Kubernetes features.
## Usage
### PodCommandLineCodeExecutor
Like DockerCommandLineCodeExecutor, this code executor runs codes on a container in a kubernetes pod
Unlike DockerCommandLineCodeExecutor, PodCommandLineCodeExecutor is not support container restart feature.
```python
from autogen_core import CancellationToken
from autogen_core.code_executor import CodeBlock
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
async with PodCommandLineCodeExecutor() as executor:
code_result = await executor.execute_code_blocks(
code_blocks=[
CodeBlock(language="python", code="print('Hello, World!')"),
],
cancellation_token=CancellationToken(),
)
print(code_result)
```
```
CommandLineCodeResult(exit_code=0, output='Hello, World!\n', code_file='/workspace/tmp_code_07da107bb575cc4e02b0e1d6d99cc204.py')
```
in default options, pod will be created like
```python
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
# name is like autogen-code-exec-{uuid4}
"name": "autogen-code-exec-a2826c87-9b8d-46ec-be36-5fffc5d8f899",
# created on default namespace
"namespace": "default",
...
},
"spec": {
"containers": [
{
# container named autogen-executor
"name": "autogen-executor",
# default image python:3-slim
"image": "python:3-slim",
# the container is kept alive by running keep-alive loop: /bin/sh -c while true;do sleep 5; done
"command": [
"/bin/sh"
],
"args": [
"-c",
"while true;do sleep 5; done"
],
...
],
...
}
}
```
There are arguments that change pod specification of PodCommandLineCodeExecutor.
There are several arguments to change pod settings
|argument|datatype|describe|
|--|--|--|
|image|str|Container image|
|pod_name|str|Pod name|
|namespace|str|Pod namespace|
|workspace_path|str, Path|Path in container where the LLM generated code script will be saved|
|volume|dict, str, Path, V1Volume, None|Volume to be mounted on pods|
|pod_spec|dict, str, Path, V1Pod, None|Pod specification for command line code executor|
Other parameters image, pod_name, namespace and volume are ignored when pod_spec parameter is provided.
#### volume parameter usage
"volume" parameter is to mount pre-existing volume on code executor container
The volume will be mounted in the container's workspace path provided as the executor's parameter "workspace_path".
"volume" parameter can be provided like:
- kubernetes.client.models.V1Volume
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
from kubernetes.client.models import V1Volume
volume = V1Volume(
name="test-volume",
empty_dir=V1EmptyDirVolumeSource(medium="Memory", size_limit="5Mi"),
)
executor = PodCommandLineCodeExecutor(volume=volume)
...
```
- dictionary, must conform kubernetes volume specification.
reference: [
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Volume.md,
https://kubernetes.io/docs/concepts/storage/volumes/
]
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
volume = {"name": "test-volume", "emptyDir": {"medium": "Memory", "sizeLimit": "5Mi"}}
executor = PodCommandLineCodeExecutor(volume=volume)
...
```
- string which is expresses yaml or json format
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
volume = """
name: "test-volume"
emptyDir:
medium: "Memory"
sizeLimit: "5Mi"
"""
executor = PodCommandLineCodeExecutor(volume=volume)
...
```
- file path of yaml or json format
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
volume = "./test/test-volume.yaml"
executor = PodCommandLineCodeExecutor(volume=volume)
...
```
#### pod_spec parameter usage
To create more complex pod specification of PodCommandLineCodeExecutor, use pod_spec parameter.
Like volume parameter, pod_spec parameter can be provided like:
- kubernetes.client.models.V1Volume
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
pod_spec = V1Pod(
metadata=V1ObjectMeta(name="test-pod", namespace="default"),
spec=V1PodSpec(
restart_policy="Never",
containers=[
V1Container(
args=[
"-c",
"echo 'test container'; while true;do sleep 5; done",
],
command=["/bin/sh"],
name="autogen-executor", # container named "autogen-executor" must be included
image="python:3-slim",
)
],
),
)
executor = PodCommandLineCodeExecutor(pod_spec=pod_spec)
...
```
- dictionary, must conform kubernetes pod specification.
reference: [
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Pod.md,
https://kubernetes.io/docs/concepts/workloads/pods/
]
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
pod = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {"test": "true"},
"name": "test-pod",
"namespace": "default",
},
"spec": {
"containers": [
{
"image": "python:3-slim",
"name": "autogen-executor", # container named "autogen-executor" must be included
"args": [
"/bin/sh",
"-c",
"echo 'test container'; while true;do sleep 5; done",
],
},
],
},
}
executor = PodCommandLineCodeExecutor(pod_spec=pod_spec)
...
```
- string which is expresses yaml or json format
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
pod = (
"apiVersion: v1\n"
"kind: Pod\n"
"metadata:\n"
" annotations:\n"
" test: 'true'\n"
" name: test-pod\n"
" namespace: default\n"
"spec:\n"
" containers:\n"
" - args:\n"
" - sh\n"
" - -c\n"
" - while true;do sleep 5; done\n"
" image: python:3-slim\n"
" name: autogen-executor\n"
)
executor = PodCommandLineCodeExecutor(pod_spec=pod_spec)
...
```
- file path of yaml or json format
```python
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
pod = "./test/test-pod.yaml"
executor = PodCommandLineCodeExecutor(pod_spec=pod_spec)
...
```
#### Kubeconfig usage
By default, PodCommandLineCodeExecutor uses default kubeconfig file to communicate with kubernetes API server.
Default kubeconfig file path is provded by Environment variable "KUBECONFIG"
To use other kubeconfig file, provide it's path on "KUBECONFIG" environment variable.
https://github.com/kubernetes-client/python/blob/master/kubernetes/base/config/kube_config.py#L48
```python
...
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
...
```
If the PodCommandLineExecutor is initialized on kubernetes object, incluster config(serviceAccount tokens) is used.
To use incluster config, make sure to have sufficient permissions.
below is minimum permission:
|resource|verb|
|--|--|
|pods|get, create, delete|
|pods/status|get|
|pods/exec|create|
|pods/log|get|
For example, create serviceAccount and bind role with sufficient permissions.
creatae ServiceAccount
```sh
kubectl create serviceaccount autogen-executor-sa
```
create clusterRole/Role(namespaced role) with sufficient permissions
```sh
kubectl create clusterrole autogen-executor-role \
--resource=pods --verb=get,create,delete \
--resource=pods/exec --verb=create \
--resource=pods/status,pods/log --verb=get
```
bind clusterRole/role with ServiceAccount
```sh
kubectl create rolebinding autogen-executor-rolebinding \
--clusterrole autogen-executor-role --serviceaccount default:autogen-executor-sa
```
Then, PodCommandLineCodeExecutor will work alright where the pod uses the serviceAccount created before.
create pod uses serviceAccount
```sh
kubectl run autogen-executor --image python:3 \
--overrides='{"spec": {"serviceAccount": "autogen-executor-sa"}}' \
-- sh -c 'pip install autogen-kubernetes && sleep infinity'
```
execute the pod
```sh
kubectl exec autogen-executor -it -- python
```
execute PodCommandLineCodeExecutor
```python
from autogen_core import CancellationToken
from autogen_core.code_executor import CodeBlock
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor
async with PodCommandLineCodeExecutor() as executor:
code_result = await executor.execute_code_blocks(
code_blocks=[
CodeBlock(language="python", code="print('Hello, World!')"),
],
cancellation_token=CancellationToken(),
)
print(code_result)
```
```
CommandLineCodeResult(exit_code=0, output='Hello, World!\n', code_file='/workspace/tmp_code_07da107bb575cc4e02b0e1d6d99cc204.py')
```
#### Function module usage
To make code executor pod to have pre-installed packages, provide "functions" parameter.
```python
import pandas as pd
from autogen_core import CancellationToken
from autogen_core.code_executor import CodeBlock
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor, Alias, with_requirements
@with_requirements(python_packages=["pandas"], global_imports=[Alias(name="pandas", alias="pd")])
def load_data() -> pd.DataFrame:
"""
Load pandas sample dataframe
Returns:
pd.DataFrame: sample Dataframe with columns name(str), age(int)
"""
data = {
"name": ["Sam", "Brown"],
"age": [37, 57],
}
return pd.DataFrame(data)
async with PodCommandLineCodeExecutor(functions=[load_data]) as executor:
code = f"from {executor._functions_module} import load_data\nprint(load_data())"
code_result = await executor.execute_code_blocks(
code_blocks=[
CodeBlock(language="python", code=f"from "),
],
cancellation_token=CancellationToken(),
)
print(code_result)
```
```
CommandLineCodeResult(exit_code=0, output=' name age\n0 Sam 37\n1 Brown 57\n', code_file='/workspace/tmp_code_bd92ac3930fbd4f6f627885646227d5ff54753166555b98206cc01fdc023a7ef.py')
```
Another example with FunctionWithRequirements and ImportFromModule.
```python
import inspect
from autogen_core import CancellationToken
from autogen_core.code_executor import CodeBlock
from autogen_kubernetes.code_executors import PodCommandLineCodeExecutor, ImportFromModule, FunctionWithRequirements
## this section will be written on function module file(workspace_path/functions.py)
from kubernetes.client import CoreV1Api
from kubernetes.config import load_config
def kubernetes_enabled() -> bool:
try:
load_config() # type: ignore
api_client = CoreV1Api()
api_client.list_namespace()
return True
except Exception:
return False
##
test_function = FunctionWithRequirements.from_str(
inspect.getsource(kubernetes_enabled),
["kubernetes"],
[
ImportFromModule(module="kubernetes.client", imports=("CoreV1Api",)),
ImportFromModule(module="kubernetes.config", imports=("load_config",)),
],
)
async with PodCommandLineCodeExecutor(functions=[test_function]) as executor:
code = f"from {executor._functions_module} import kubernetes_enabled\nprint(kubernetes_enabled())"
code_result = await executor.execute_code_blocks(
code_blocks=[
CodeBlock(language="python", code=code),
],
cancellation_token=CancellationToken(),
)
print(code_result)
```
```
CommandLineCodeResult(exit_code=0, output='kube_config_path not provided and default location (~/.kube/config) does not exist. Using inCluster Config. This might not work.\nTrue\n', code_file='/workspace/tmp_code_c61a3c1e421357bd54041ad195e242d8205b86a3a4a0778b8e2684bc373aac22.py')
```
## Contribute
This project's structure conforms microsoft/autogen python package
```
├── LICENSE
└── python
├── run_task_in_pkgs_if_exist.py
├── pyproject.toml
├── shared_tasks.toml
├── uv.lock
└── packages
└── autogen-kubernetes
├── tests
│ ├── test_utils.py
│ ├── conftest.py
│ ├── test_kubernetees_code_executor.py
│ ├── test-pod.yaml
│ └── test-volume.yaml
├── LICENSE-CODE
├── pyproject.toml
├── README.md
└── src
└── autogen_kubernetes
├── py.typed
└── code_executors
├── _utils.py
└── _kubernetes_code_executor.py
```
### Install uv
Install uv according to your environment.
https://docs.astral.sh/uv/getting-started/installation/
### Sync project
```sh
cd autogen-kubernetes/python
uv venv --python python >=3.10
source .venv/bin/activate
uv sync --locked --all-extras
```
### Common tasks
- Format: `poe format`
- Lint: `poe lint`
- Test: `poe test`
- Mypy: `poe mypy`
- Check all: `poe check`
## Licensing
This project is licensed under the MIT License.
### Code Modification
This project includes code from the microsoft/autogen project (licensed under the MIT License),
with modifications made by kiyoung you(questcollector), See the [LICENSE-CODE](python/packages/autogen-kubernetes/LICENSE-CODE) file for details
### Third-Party Dependencies
This project uses the following third-party dependencies:
1. **kubernetes**
License: Apache License, Version 2.0
Source: https://github.com/kubernetes-client/python
2. **httpx**
License: BSD 3-Clause "New" or "Revised"
Source: https://github.com/encode/httpx
3. **websockets**
License: BSD 3-Clause "New" or "Revised"
Source: https://github.com/python-websockets/websockets
4. **PyYAML**
License: MIT License
Source: https://github.com/yaml/pyyaml
For details, see the LICENCE-THIRD-PARTY file.
Raw data
{
"_id": null,
"home_page": null,
"name": "autogen-kubernetes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "AI, agent, autogen, kubernetes",
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/91/44/2ef8e5e1aef80327d3d4069b1138db804f637218d8c4ebea54297ac1c7f7/autogen_kubernetes-0.4.0.dev10.tar.gz",
"platform": null,
"description": "# autogen-kubernetes\n\nThis is autogen(>=0.4) kubernetes extension which provides code executor on kuberentes pod\n\nWe plan to add \"autogen\" features needs Kubernetes features.\n\n## Usage\n\n### PodCommandLineCodeExecutor\n\nLike DockerCommandLineCodeExecutor, this code executor runs codes on a container in a kubernetes pod\n\nUnlike DockerCommandLineCodeExecutor, PodCommandLineCodeExecutor is not support container restart feature.\n\n```python\nfrom autogen_core import CancellationToken\nfrom autogen_core.code_executor import CodeBlock\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\nasync with PodCommandLineCodeExecutor() as executor:\n code_result = await executor.execute_code_blocks(\n code_blocks=[\n CodeBlock(language=\"python\", code=\"print('Hello, World!')\"),\n ],\n cancellation_token=CancellationToken(),\n )\n print(code_result)\n```\n```\nCommandLineCodeResult(exit_code=0, output='Hello, World!\\n', code_file='/workspace/tmp_code_07da107bb575cc4e02b0e1d6d99cc204.py')\n```\n\nin default options, pod will be created like\n\n```python\n{\n \"kind\": \"Pod\",\n \"apiVersion\": \"v1\",\n \"metadata\": {\n # name is like autogen-code-exec-{uuid4}\n \"name\": \"autogen-code-exec-a2826c87-9b8d-46ec-be36-5fffc5d8f899\",\n # created on default namespace\n \"namespace\": \"default\",\n ...\n },\n \"spec\": {\n \"containers\": [\n {\n # container named autogen-executor\n \"name\": \"autogen-executor\",\n # default image python:3-slim\n \"image\": \"python:3-slim\",\n # the container is kept alive by running keep-alive loop: /bin/sh -c while true;do sleep 5; done\n \"command\": [\n \"/bin/sh\"\n ],\n \"args\": [\n \"-c\",\n \"while true;do sleep 5; done\"\n ],\n ...\n ],\n ...\n }\n}\n```\n\nThere are arguments that change pod specification of PodCommandLineCodeExecutor.\n\nThere are several arguments to change pod settings\n\n|argument|datatype|describe|\n|--|--|--|\n|image|str|Container image|\n|pod_name|str|Pod name|\n|namespace|str|Pod namespace|\n|workspace_path|str, Path|Path in container where the LLM generated code script will be saved|\n|volume|dict, str, Path, V1Volume, None|Volume to be mounted on pods|\n|pod_spec|dict, str, Path, V1Pod, None|Pod specification for command line code executor|\n\nOther parameters image, pod_name, namespace and volume are ignored when pod_spec parameter is provided.\n\n#### volume parameter usage\n\n\"volume\" parameter is to mount pre-existing volume on code executor container\n\nThe volume will be mounted in the container's workspace path provided as the executor's parameter \"workspace_path\".\n\n\"volume\" parameter can be provided like:\n\n- kubernetes.client.models.V1Volume\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\nfrom kubernetes.client.models import V1Volume\n\nvolume = V1Volume(\n name=\"test-volume\",\n empty_dir=V1EmptyDirVolumeSource(medium=\"Memory\", size_limit=\"5Mi\"),\n)\n\nexecutor = PodCommandLineCodeExecutor(volume=volume)\n...\n\n```\n\n- dictionary, must conform kubernetes volume specification.\n reference: [\n https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Volume.md, \n https://kubernetes.io/docs/concepts/storage/volumes/\n ]\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\nvolume = {\"name\": \"test-volume\", \"emptyDir\": {\"medium\": \"Memory\", \"sizeLimit\": \"5Mi\"}}\n\nexecutor = PodCommandLineCodeExecutor(volume=volume)\n...\n```\n\n- string which is expresses yaml or json format\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\nvolume = \"\"\"\nname: \"test-volume\"\nemptyDir:\n medium: \"Memory\"\n sizeLimit: \"5Mi\"\n\"\"\"\n\nexecutor = PodCommandLineCodeExecutor(volume=volume)\n...\n```\n\n- file path of yaml or json format\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\nvolume = \"./test/test-volume.yaml\"\n\nexecutor = PodCommandLineCodeExecutor(volume=volume)\n...\n```\n\n#### pod_spec parameter usage\n\nTo create more complex pod specification of PodCommandLineCodeExecutor, use pod_spec parameter.\n\nLike volume parameter, pod_spec parameter can be provided like:\n\n- kubernetes.client.models.V1Volume\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\npod_spec = V1Pod(\n metadata=V1ObjectMeta(name=\"test-pod\", namespace=\"default\"),\n spec=V1PodSpec(\n restart_policy=\"Never\",\n containers=[\n V1Container(\n args=[\n \"-c\",\n \"echo 'test container'; while true;do sleep 5; done\",\n ],\n command=[\"/bin/sh\"],\n name=\"autogen-executor\", # container named \"autogen-executor\" must be included\n image=\"python:3-slim\",\n )\n ],\n ),\n)\n\nexecutor = PodCommandLineCodeExecutor(pod_spec=pod_spec)\n...\n```\n\n- dictionary, must conform kubernetes pod specification.\n reference: [\n https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Pod.md, \n https://kubernetes.io/docs/concepts/workloads/pods/\n ]\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\npod = {\n \"apiVersion\": \"v1\",\n \"kind\": \"Pod\",\n \"metadata\": {\n \"annotations\": {\"test\": \"true\"},\n \"name\": \"test-pod\",\n \"namespace\": \"default\",\n },\n \"spec\": {\n \"containers\": [\n {\n \"image\": \"python:3-slim\",\n \"name\": \"autogen-executor\", # container named \"autogen-executor\" must be included\n \"args\": [\n \"/bin/sh\",\n \"-c\",\n \"echo 'test container'; while true;do sleep 5; done\",\n ],\n },\n ],\n },\n}\n\nexecutor = PodCommandLineCodeExecutor(pod_spec=pod_spec)\n...\n```\n\n- string which is expresses yaml or json format\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\npod = (\n \"apiVersion: v1\\n\"\n \"kind: Pod\\n\"\n \"metadata:\\n\"\n \" annotations:\\n\"\n \" test: 'true'\\n\"\n \" name: test-pod\\n\"\n \" namespace: default\\n\"\n \"spec:\\n\"\n \" containers:\\n\"\n \" - args:\\n\"\n \" - sh\\n\"\n \" - -c\\n\"\n \" - while true;do sleep 5; done\\n\"\n \" image: python:3-slim\\n\"\n \" name: autogen-executor\\n\"\n)\n\nexecutor = PodCommandLineCodeExecutor(pod_spec=pod_spec)\n...\n```\n\n- file path of yaml or json format\n\n```python\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\npod = \"./test/test-pod.yaml\"\n\nexecutor = PodCommandLineCodeExecutor(pod_spec=pod_spec)\n...\n```\n\n#### Kubeconfig usage\n\nBy default, PodCommandLineCodeExecutor uses default kubeconfig file to communicate with kubernetes API server.\n\nDefault kubeconfig file path is provded by Environment variable \"KUBECONFIG\"\n\nTo use other kubeconfig file, provide it's path on \"KUBECONFIG\" environment variable.\n\nhttps://github.com/kubernetes-client/python/blob/master/kubernetes/base/config/kube_config.py#L48\n```python\n...\nKUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')\n...\n```\n\nIf the PodCommandLineExecutor is initialized on kubernetes object, incluster config(serviceAccount tokens) is used.\n\nTo use incluster config, make sure to have sufficient permissions.\n\nbelow is minimum permission:\n\n|resource|verb|\n|--|--|\n|pods|get, create, delete|\n|pods/status|get|\n|pods/exec|create|\n|pods/log|get|\n\n\nFor example, create serviceAccount and bind role with sufficient permissions.\n\ncreatae ServiceAccount\n```sh\nkubectl create serviceaccount autogen-executor-sa\n```\n\ncreate clusterRole/Role(namespaced role) with sufficient permissions\n```sh\nkubectl create clusterrole autogen-executor-role \\\n --resource=pods --verb=get,create,delete \\\n --resource=pods/exec --verb=create \\\n --resource=pods/status,pods/log --verb=get\n```\n\nbind clusterRole/role with ServiceAccount\n```sh\nkubectl create rolebinding autogen-executor-rolebinding \\\n --clusterrole autogen-executor-role --serviceaccount default:autogen-executor-sa\n```\n\nThen, PodCommandLineCodeExecutor will work alright where the pod uses the serviceAccount created before.\n\ncreate pod uses serviceAccount\n```sh\nkubectl run autogen-executor --image python:3 \\\n --overrides='{\"spec\": {\"serviceAccount\": \"autogen-executor-sa\"}}' \\\n -- sh -c 'pip install autogen-kubernetes && sleep infinity'\n```\n\nexecute the pod\n```sh\nkubectl exec autogen-executor -it -- python\n```\n\nexecute PodCommandLineCodeExecutor\n```python\nfrom autogen_core import CancellationToken\nfrom autogen_core.code_executor import CodeBlock\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor\n\nasync with PodCommandLineCodeExecutor() as executor:\n code_result = await executor.execute_code_blocks(\n code_blocks=[\n CodeBlock(language=\"python\", code=\"print('Hello, World!')\"),\n ],\n cancellation_token=CancellationToken(),\n )\n print(code_result)\n```\n```\nCommandLineCodeResult(exit_code=0, output='Hello, World!\\n', code_file='/workspace/tmp_code_07da107bb575cc4e02b0e1d6d99cc204.py')\n```\n\n#### Function module usage\n\nTo make code executor pod to have pre-installed packages, provide \"functions\" parameter.\n\n```python\nimport pandas as pd\nfrom autogen_core import CancellationToken\nfrom autogen_core.code_executor import CodeBlock\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor, Alias, with_requirements\n\n@with_requirements(python_packages=[\"pandas\"], global_imports=[Alias(name=\"pandas\", alias=\"pd\")])\ndef load_data() -> pd.DataFrame:\n \"\"\"\n Load pandas sample dataframe\n\n Returns:\n pd.DataFrame: sample Dataframe with columns name(str), age(int)\n \"\"\"\n data = {\n \"name\": [\"Sam\", \"Brown\"],\n \"age\": [37, 57],\n }\n return pd.DataFrame(data)\n\nasync with PodCommandLineCodeExecutor(functions=[load_data]) as executor:\n code = f\"from {executor._functions_module} import load_data\\nprint(load_data())\"\n code_result = await executor.execute_code_blocks(\n code_blocks=[\n CodeBlock(language=\"python\", code=f\"from \"),\n ],\n cancellation_token=CancellationToken(),\n )\n print(code_result)\n\n```\n\n```\nCommandLineCodeResult(exit_code=0, output=' name age\\n0 Sam 37\\n1 Brown 57\\n', code_file='/workspace/tmp_code_bd92ac3930fbd4f6f627885646227d5ff54753166555b98206cc01fdc023a7ef.py')\n```\n\nAnother example with FunctionWithRequirements and ImportFromModule.\n\n```python\nimport inspect\nfrom autogen_core import CancellationToken\nfrom autogen_core.code_executor import CodeBlock\nfrom autogen_kubernetes.code_executors import PodCommandLineCodeExecutor, ImportFromModule, FunctionWithRequirements\n\n## this section will be written on function module file(workspace_path/functions.py)\nfrom kubernetes.client import CoreV1Api\nfrom kubernetes.config import load_config\n\ndef kubernetes_enabled() -> bool:\n try:\n load_config() # type: ignore\n api_client = CoreV1Api()\n api_client.list_namespace()\n return True\n except Exception:\n return False\n\n##\n\ntest_function = FunctionWithRequirements.from_str(\n inspect.getsource(kubernetes_enabled),\n [\"kubernetes\"],\n [\n ImportFromModule(module=\"kubernetes.client\", imports=(\"CoreV1Api\",)),\n ImportFromModule(module=\"kubernetes.config\", imports=(\"load_config\",)),\n ],\n)\nasync with PodCommandLineCodeExecutor(functions=[test_function]) as executor:\n code = f\"from {executor._functions_module} import kubernetes_enabled\\nprint(kubernetes_enabled())\"\n code_result = await executor.execute_code_blocks(\n code_blocks=[\n CodeBlock(language=\"python\", code=code),\n ],\n cancellation_token=CancellationToken(),\n )\n print(code_result)\n```\n```\nCommandLineCodeResult(exit_code=0, output='kube_config_path not provided and default location (~/.kube/config) does not exist. Using inCluster Config. This might not work.\\nTrue\\n', code_file='/workspace/tmp_code_c61a3c1e421357bd54041ad195e242d8205b86a3a4a0778b8e2684bc373aac22.py')\n```\n\n\n## Contribute\n\nThis project's structure conforms microsoft/autogen python package\n\n```\n\u251c\u2500\u2500 LICENSE\n\u2514\u2500\u2500 python\n \u251c\u2500\u2500 run_task_in_pkgs_if_exist.py\n \u251c\u2500\u2500 pyproject.toml\n \u251c\u2500\u2500 shared_tasks.toml\n \u251c\u2500\u2500 uv.lock\n \u2514\u2500\u2500 packages\n \u2514\u2500\u2500 autogen-kubernetes\n \u251c\u2500\u2500 tests\n \u2502 \u251c\u2500\u2500 test_utils.py\n \u2502 \u251c\u2500\u2500 conftest.py\n \u2502 \u251c\u2500\u2500 test_kubernetees_code_executor.py\n \u2502 \u251c\u2500\u2500 test-pod.yaml\n \u2502 \u2514\u2500\u2500 test-volume.yaml\n \u251c\u2500\u2500 LICENSE-CODE\n \u251c\u2500\u2500 pyproject.toml\n \u251c\u2500\u2500 README.md\n \u2514\u2500\u2500 src\n \u2514\u2500\u2500 autogen_kubernetes\n \u251c\u2500\u2500 py.typed\n \u2514\u2500\u2500 code_executors\n \u251c\u2500\u2500 _utils.py\n \u2514\u2500\u2500 _kubernetes_code_executor.py\n```\n\n### Install uv\n\nInstall uv according to your environment.\n\nhttps://docs.astral.sh/uv/getting-started/installation/\n\n### Sync project\n\n```sh\ncd autogen-kubernetes/python\nuv venv --python python >=3.10\nsource .venv/bin/activate\nuv sync --locked --all-extras\n```\n\n### Common tasks\n\n- Format: `poe format`\n- Lint: `poe lint`\n- Test: `poe test`\n- Mypy: `poe mypy`\n- Check all: `poe check`\n\n## Licensing\n\nThis project is licensed under the MIT License.\n\n### Code Modification\n\nThis project includes code from the microsoft/autogen project (licensed under the MIT License), \n\nwith modifications made by kiyoung you(questcollector), See the [LICENSE-CODE](python/packages/autogen-kubernetes/LICENSE-CODE) file for details\n\n\n### Third-Party Dependencies\n\nThis project uses the following third-party dependencies:\n\n1. **kubernetes**\n License: Apache License, Version 2.0\n Source: https://github.com/kubernetes-client/python\n2. **httpx**\n License: BSD 3-Clause \"New\" or \"Revised\"\n Source: https://github.com/encode/httpx\n3. **websockets**\n License: BSD 3-Clause \"New\" or \"Revised\"\n Source: https://github.com/python-websockets/websockets\n4. **PyYAML**\n License: MIT License\n Source: https://github.com/yaml/pyyaml\n\nFor details, see the LICENCE-THIRD-PARTY file.",
"bugtrack_url": null,
"license": "MIT License Copyright (c) Microsoft Corporation. Copyright (c) 2024 kiyoung you Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE --- This project includes code from the autogen project, which is licensed under the MIT License. Copyright (c) Microsoft Corporation. Modification Copyright (c) 2024 kiyoung you(questcollector) This project is licensed under the MIT License. See the full license text above --- Third-Party Licenses --------------------- This project uses the following third-party dependencies: 1. Kubernetes Python SDK(https://github.com/kubernetes-client/python) Licensed under the Apache License, Version 2.0. --- 2. httpx(https://github.com/encode/httpx) Licensed under BSD 3-Clause \"New\" or \"Revised\" License: Copyright \u00a9 2019, Encode OSS Ltd. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- 3. websockets(https://github.com/python-websockets/websockets) Licensed under the BSD 3-Clause \"New\" or \"Revised\" License: Copyright (c) Aymeric Augustin and contributors Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- 4. PyYAML(https://github.com/yaml/pyyaml) Licensed under the MIT License: Copyright (c) 2017-2021 Ingy d\u00f6t Net Copyright (c) 2006-2016 Kirill Simonov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"summary": "autogen kubernetes extension",
"version": "0.4.0.dev10",
"project_urls": null,
"split_keywords": [
"ai",
" agent",
" autogen",
" kubernetes"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "34b2b405d36f288e7176edf5c7ff1db984cd35399ff205166a3a3a741aa37b0f",
"md5": "f0876350b9224c839371955459d1a1dc",
"sha256": "4089c4a31bd56aa7c80ccf8b8679713bdad1b8c876fe75d04047bad89a112fc3"
},
"downloads": -1,
"filename": "autogen_kubernetes-0.4.0.dev10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f0876350b9224c839371955459d1a1dc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 19876,
"upload_time": "2024-12-13T07:59:07",
"upload_time_iso_8601": "2024-12-13T07:59:07.455236Z",
"url": "https://files.pythonhosted.org/packages/34/b2/b405d36f288e7176edf5c7ff1db984cd35399ff205166a3a3a741aa37b0f/autogen_kubernetes-0.4.0.dev10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "91442ef8e5e1aef80327d3d4069b1138db804f637218d8c4ebea54297ac1c7f7",
"md5": "83d0a61bdee0c01a12921f8da542f3aa",
"sha256": "ef36b0bc465bb00bcf2cca5c2a0533b0c08266ce94f31d773de55f5e9fb4a4bc"
},
"downloads": -1,
"filename": "autogen_kubernetes-0.4.0.dev10.tar.gz",
"has_sig": false,
"md5_digest": "83d0a61bdee0c01a12921f8da542f3aa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 20585,
"upload_time": "2024-12-13T07:59:09",
"upload_time_iso_8601": "2024-12-13T07:59:09.879804Z",
"url": "https://files.pythonhosted.org/packages/91/44/2ef8e5e1aef80327d3d4069b1138db804f637218d8c4ebea54297ac1c7f7/autogen_kubernetes-0.4.0.dev10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-13 07:59:09",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "autogen-kubernetes"
}