Skip to content

Commit dd3154b

Browse files
revise documentation
1 parent 1483efa commit dd3154b

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

website/docs/topics/code-execution/kubernetes-pod-commandline-code-executor.ipynb

+68-47
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"The `PodCommandLineCodeExecutor` of the autogen.coding.kubernetes module is to execute a code block using a pod in Kubernetes.\n",
15-
"It is like DockerCommandLineCodeExecutor, but creates container on kubernetes Pod.\n",
14+
"The `PodCommandLineCodeExecutor` in the `autogen.coding.kubernetes` module is designed to execute code blocks using a pod in Kubernetes.\n",
15+
"It functions similarly to the `DockerCommandLineCodeExecutor`, but specifically creates container within Kubernetes environments.\n",
1616
"\n",
1717
"There are two condition to use PodCommandLineCodeExecutor.\n",
18-
"- accessible to kubernetes cluster\n",
19-
"- install autogen with extra feature kubernetes\n",
18+
"- Access to a Kubernetes cluster\n",
19+
"- installation `autogen` with the extra requirements `'pyautogen[kubernetes]'`\n",
2020
"\n",
21-
"This documents uses minikube cluster on local environment.\n",
22-
"You can refer to the link below for installation and execution of minikube.\n",
21+
"For local development and testing, this document uses a Minikube cluster.\n",
2322
"\n",
24-
"https://minikube.sigs.k8s.io/docs/start/"
23+
"Minikube is a tool that allows you to run a single-node Kubernetes cluster on you local machine. \n",
24+
"You can refer to the link below for installation and setup of Minikube.\n",
25+
"\n",
26+
"🔗 https://minikube.sigs.k8s.io/docs/start/"
2527
]
2628
},
2729
{
@@ -37,9 +39,9 @@
3739
"source": [
3840
"There are four options PodCommandLineCodeExecutor to access kubernetes API server.\n",
3941
"- default kubeconfig file path: `~/.kube/config`\n",
40-
"- To provide kubeconfig file path to `kube_config_file` argument of `PodCommandLineCodeExecutor`.\n",
41-
"- To provide kubeconfig file path to `KUBECONFIG` environment variable.\n",
42-
"- To provide token from kubernetes ServiceAccount which has enough permissions"
42+
"- Provide a custom kubeconfig file path using the `kube_config_file` argument of `PodCommandLineCodeExecutor`.\n",
43+
"- Set the kubeconfig file path using the `KUBECONFIG` environment variable.\n",
44+
"- Provide token from Kubernetes ServiceAccount with sufficient permissions"
4345
]
4446
},
4547
{
@@ -48,7 +50,7 @@
4850
"source": [
4951
"Generally, if kubeconfig file is located in `~/.kube/config`, there's no need to provide kubeconfig file path on parameter or environment variables.\n",
5052
"\n",
51-
"the Tutorial of providing ServiceAccount Token is in the last section"
53+
"The tutorial of providing ServiceAccount Token is in the last section"
5254
]
5355
},
5456
{
@@ -57,8 +59,25 @@
5759
"source": [
5860
"## Example\n",
5961
"\n",
60-
"In order to use kubernetes Pod based code executor, kubernetes python sdk is required.\n",
61-
"It can be installed by `pip install 'kubernetes>=27'` or with the extra kubernetes"
62+
"In order to use kubernetes Pod based code executor, you need to install Kubernetes Python SDK.\n",
63+
"\n",
64+
"You can do this by running the following command:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"pip install 'kubernetes>=27'"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"Alternatively, you can install it with the extra features for Kubernetes:"
6281
]
6382
},
6483
{
@@ -67,9 +86,7 @@
6786
"metadata": {},
6887
"outputs": [],
6988
"source": [
70-
"%pip install 'pyautogen[kubernetes]'\n",
71-
"# or\n",
72-
"# pip install pyautogen 'kubernetes>=27'"
89+
"pip install 'pyautogen[kubernetes]'"
7390
]
7491
},
7592
{
@@ -85,10 +102,10 @@
85102
"metadata": {},
86103
"outputs": [],
87104
"source": [
88-
"# import os\n",
89-
"# # If kubeconfig file is not located on ~/.kube/config but other path, \n",
90-
"# # you can set path of kubeconfig file on KUBECONFIG environment variables \n",
91-
"# os.environ[\"KUBECONFIG\"] = \"kubeconfig/file/path\""
105+
"import os\n",
106+
"# Set the KUBECONFIG environment variable \n",
107+
"# if the kubeconfig file is not in the default location(~/.kube/cofig).\n",
108+
"os.environ[\"KUBECONFIG\"] = \"path/to/your/kubeconfig\""
92109
]
93110
},
94111
{
@@ -121,6 +138,7 @@
121138
" ) as executor:\n",
122139
" print(\n",
123140
" executor.execute_code_blocks(\n",
141+
" # Example of executing a simple Python code block within a Kubernetes pod.\n",
124142
" code_blocks=[\n",
125143
" CodeBlock(language=\"python\", code=\"print('Hello, World!')\"),\n",
126144
" ]\n",
@@ -132,9 +150,9 @@
132150
"cell_type": "markdown",
133151
"metadata": {},
134152
"source": [
135-
"With context manager(with statement), the pod created by PodCommandLineCodeExecutor deleted automatically after tasks done.\n",
153+
"Using a context manager(the `with` statement), the pod created by `PodCommandLineCodeExecutor` is automatically deleted after the tasks are completed.\n",
136154
"\n",
137-
"To delete the pod manually, you can use `stop()` method"
155+
"Although the pod is automatically deleted when using a context manager, you might sometimes need to delete it manually. You can do this using `stop()` method, as shown below:"
138156
]
139157
},
140158
{
@@ -162,7 +180,8 @@
162180
],
163181
"source": [
164182
"%%bash\n",
165-
"# default pod name is autogen-code-exec-{uuid.uuid4()}\n",
183+
"# This command lists all pods in the default namespace. \n",
184+
"# The default pod name follows the format autogen-code-exec-{uuid.uuid4()}.\n",
166185
"kubectl get pod -n default"
167186
]
168187
},
@@ -181,7 +200,8 @@
181200
],
182201
"source": [
183202
"%%bash\n",
184-
"# default container image is python:3-slim\n",
203+
"# This command shows container's image in the pod.\n",
204+
"# The default container image is python:3-slim\n",
185205
"kubectl get pod autogen-code-exec-afd217ac-f77b-4ede-8c53-1297eca5ec64 -o jsonpath={.spec.containers[0].image}"
186206
]
187207
},
@@ -198,11 +218,9 @@
198218
"cell_type": "markdown",
199219
"metadata": {},
200220
"source": [
201-
"If you use another container image for code executor pod, you can provide image tag to `image` argument.\n",
221+
"To use a different container image for code executor pod, specify the desired image tag using `image` argument.\n",
202222
"\n",
203-
"PodCommandLineCode Executor has default execution policy that allows python and shell script code blocks.\n",
204-
"\n",
205-
"It can be allowed other languages with `execution_policies` argument."
223+
"`PodCommandLineCodeExecutor` has a default execution policy that allows Python and shell script code blocks. You can enable other languages with `execution_policies` argument."
206224
]
207225
},
208226
{
@@ -220,11 +238,11 @@
220238
],
221239
"source": [
222240
"with PodCommandLineCodeExecutor(\n",
223-
" image=\"node:22-alpine\", # you can provide runtime environments through container image\n",
241+
" image=\"node:22-alpine\", # Specifies the runtime environments using a container image\n",
224242
" namespace=\"default\",\n",
225-
" work_dir=\"./app\", # workspace directory for code block files\n",
226-
" timeout=10, # timeout(seconds) value for waiting pod creation, execution of code blocks. default is 60 seconds\n",
227-
" execution_policies = {\"javascript\": True} # allowed execution languages can be updated with execution_policy dictionary\n",
243+
" work_dir=\"./app\", # Directory within the container where code block files are stored\n",
244+
" timeout=10, # Timeout in seconds for pod creation and code block execution (default is 60 seconds)\n",
245+
" execution_policies = {\"javascript\": True} # Enable execution of Javascript code blocks by updating execution policies\n",
228246
") as executor:\n",
229247
" print(\n",
230248
" executor.execute_code_blocks(\n",
@@ -239,10 +257,10 @@
239257
"cell_type": "markdown",
240258
"metadata": {},
241259
"source": [
242-
"If you want to give custom settings for executor pod, such as annotations, environment variables, commands, volumes etc., \n",
243-
"you can provide custom pod specification with `kubernetes.client.V1Pod` format.\n",
260+
"If you want to apply custom settings for executor pod, such as annotations, environment variables, commands, volumes etc., \n",
261+
"you can provide a custom pod specification using `kubernetes.client.V1Pod` format.\n",
244262
"\n",
245-
"`container_name` argument should also be provided because PodCommandLineCodeExecutor do not automatically recognize a container where code blocks will be executed "
263+
"The `container_name` argument should also be provided because `PodCommandLineCodeExecutor` does not automatically recognize the container where code blocks will be executed."
246264
]
247265
},
248266
{
@@ -261,7 +279,7 @@
261279
" containers=[client.V1Container(\n",
262280
" args=[\"-c\", \"while true;do sleep 5; done\"],\n",
263281
" command=[\"/bin/sh\"],\n",
264-
" name=\"abcd\", # container name where code blocks will be executed should be provided to `container_name` argument\n",
282+
" name=\"abcd\", # container name where code blocks will be executed should be provided using `container_name` argument\n",
265283
" image=\"python:3.11-slim\",\n",
266284
" env=[\n",
267285
" client.V1EnvVar(\n",
@@ -299,7 +317,7 @@
299317
"source": [
300318
"with PodCommandLineCodeExecutor(\n",
301319
" pod_spec=pod, # custom executor pod spec\n",
302-
" container_name=\"abcd\", # If custom executor pod spec is provided, container_name where code block will be executed should be specified\n",
320+
" container_name=\"abcd\", # To use custom executor pod spec, container_name where code block will be executed should be specified\n",
303321
" work_dir=\"/autogen\",\n",
304322
" timeout=60,\n",
305323
") as executor:\n",
@@ -331,7 +349,7 @@
331349
"cell_type": "markdown",
332350
"metadata": {},
333351
"source": [
334-
"PodCommandLineCodeExecutor can be integrated with Agents."
352+
"`PodCommandLineCodeExecutor` can be integrated with Agents."
335353
]
336354
},
337355
{
@@ -427,7 +445,7 @@
427445
"from autogen import ConversableAgent\n",
428446
"\n",
429447
"# The code writer agent's system message is to instruct the LLM on how to\n",
430-
"# use the Jupyter code executor with IPython kernel.\n",
448+
"# use the code executor with python or shell script code\n",
431449
"code_writer_system_message = \"\"\"\n",
432450
"You have been given coding capability to solve tasks using Python code.\n",
433451
"In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.\n",
@@ -585,11 +603,12 @@
585603
"cell_type": "markdown",
586604
"metadata": {},
587605
"source": [
588-
"If a PodCommandLineCodeExecutor instance executes on inside of kubernetes Pod, instance can use token which is generated from ServiceAccount to access kubernetes API server.\n",
606+
"If a `PodCommandLineCodeExecutor` instance runs inside of Kubernetes Pod, it can use a token generated from a ServiceAccount to access Kubernetes API server.\n",
589607
"\n",
590-
"PodCommandLineCodeExecutor needs permissions of verbs `create`, `get`, `delete` on resource `pods`, and verb `get` for resources `pods/status`, `pods/exec`.\n",
608+
"The `PodCommandLineCodeExecutor` requires the following permissions:\n",
609+
"the verbs `create`, `get`, `delete` for `pods` resource, and the verb `get` for resources `pods/status`, `pods/exec`.\n",
591610
"\n",
592-
"You can make ServiceAccount, ClusterRole and RoleBinding with kubectl."
611+
"You can create a ServiceAccount, ClusterRole and RoleBinding with `kubectl` as shown below:"
593612
]
594613
},
595614
{
@@ -607,7 +626,7 @@
607626
],
608627
"source": [
609628
"%%bash\n",
610-
"# create ServiceAccount on default namespace\n",
629+
"# Create ServiceAccount on default namespace\n",
611630
"kubectl create sa autogen-executor-sa"
612631
]
613632
},
@@ -626,7 +645,7 @@
626645
],
627646
"source": [
628647
"%%bash\n",
629-
"# create ClusterRole\n",
648+
"# Create ClusterRole that has sufficient permissions\n",
630649
"kubectl create clusterrole autogen-executor-role \\\n",
631650
" --verb=get,create,delete --resource=pods,pods/status,pods/exec"
632651
]
@@ -646,7 +665,7 @@
646665
],
647666
"source": [
648667
"%%bash\n",
649-
"# create RoleBinding\n",
668+
"# Create RoleBinding that binds ClusterRole and ServiceAccount\n",
650669
"kubectl create rolebinding autogen-executor-rolebinding \\\n",
651670
" --clusterrole autogen-executor-role --serviceaccount default:autogen-executor-sa"
652671
]
@@ -655,7 +674,7 @@
655674
"cell_type": "markdown",
656675
"metadata": {},
657676
"source": [
658-
"Pod with serviceAccount created before can be launched by below"
677+
"A pod with a previously created ServiceAccount can be launched using the following command."
659678
]
660679
},
661680
{
@@ -683,7 +702,9 @@
683702
"cell_type": "markdown",
684703
"metadata": {},
685704
"source": [
686-
"Execute PodCommandLineCodeExecutor in autogen-executor Pod"
705+
"You can execute `PodCommandLineCodeExecutor` inside the Python interpreter process from `autogen-executor` Pod.\n",
706+
"\n",
707+
"It creates new pod for code execution using token generated from `autogen-executor-sa` ServiceAccount."
687708
]
688709
},
689710
{

0 commit comments

Comments
 (0)