Duibonduil commited on
Commit
46c1eb0
·
verified ·
1 Parent(s): 77ad0bd

Upload sandbox_api.py

Browse files
aworld/sandbox/api/kubernetes/sandbox_api.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import time
3
+ import datetime
4
+ import random
5
+ import string
6
+ from typing import Dict, List, Any, Optional
7
+
8
+ from aworld.sandbox.api.base_sandbox_api import BaseSandboxApi
9
+ from aworld.sandbox.env_client.kubernetes.client import KubernetesApiClient
10
+ from aworld.sandbox.models import SandboxStatus, SandboxEnvType, SandboxK8sResponse
11
+ from aworld.sandbox.run.mcp_servers import McpServers
12
+
13
+
14
+ class KubernetesSandboxApi(BaseSandboxApi):
15
+ """
16
+ API implementation for Kubernetes sandbox operations.
17
+ """
18
+
19
+ @classmethod
20
+ def _create_sandbox(
21
+ cls,
22
+ env_type: int,
23
+ env_config: Any,
24
+ mcp_servers: Optional[List[str]] = None,
25
+ mcp_config: Optional[Any] = None,
26
+ ) -> SandboxK8sResponse:
27
+ """
28
+ Create a Kubernetes sandbox based on the reference implementation.
29
+ """
30
+ # Initialize these variables outside the try block to avoid accessing undefined variables in exception handling
31
+ client = None
32
+ pod_name = None
33
+ service_name = None
34
+
35
+ try:
36
+ # Generate current date and time as prefix, format is yymmddHHMMSS
37
+ date_prefix = datetime.datetime.now().strftime("%y%m%d%H%M%S")
38
+ random_str = cls.generate_random_string()
39
+ pod_name = f"pod-{date_prefix}-{random_str}"
40
+ service_name = f"service-{date_prefix}-{random_str}"
41
+ logging.info(f"Generated pod_name: {pod_name}")
42
+ logging.info(f"Generated service_name: {service_name}")
43
+
44
+ client = KubernetesApiClient()
45
+
46
+ pod_result = client.create_pod_from_yaml(pod_name=pod_name)
47
+ if not pod_result:
48
+ return None
49
+
50
+ max_attempts = 30
51
+ attempts = 0
52
+ wait_seconds = 2
53
+ pod_ready = False
54
+ pod_info = None
55
+
56
+ while attempts < max_attempts:
57
+ pod_info = client.get_pod_info(pod_name)
58
+ pod_ready = pod_info and pod_info.get("status") == SandboxStatus.RUNNING
59
+ if pod_ready:
60
+ break
61
+ attempts += 1
62
+ if attempts < max_attempts:
63
+ logging.info(f"Waiting for Pod to be ready, attempt {attempts}/{max_attempts}")
64
+ time.sleep(wait_seconds)
65
+
66
+ if not pod_ready:
67
+ logging.warning("Timed out waiting for Pod and Service to be ready")
68
+ client.delete_pod(pod_name)
69
+ return None
70
+
71
+ service_result = client.create_service_from_yaml(service_name=service_name, selector_name=pod_name)
72
+ if not service_result:
73
+ client.delete_pod(pod_name)
74
+ return None
75
+
76
+ max_attempts = 30
77
+ attempts = 0
78
+ wait_seconds = 2
79
+ service_ready = False
80
+ service_info = None
81
+
82
+ while attempts < max_attempts:
83
+ service_info = client.get_service_info(service_name)
84
+ if service_info and ('LoadBalancer' == service_info.get("type") and service_info.get('host')):
85
+ service_ready = True
86
+ elif service_info and 'ClusterIP' == service_info.get("type"):
87
+ service_ready = True
88
+ if service_ready:
89
+ time.sleep(wait_seconds)
90
+ break
91
+ attempts += 1
92
+ if attempts < max_attempts:
93
+ logging.info(f"Waiting for Service to be ready, attempt {attempts}/{max_attempts}")
94
+ time.sleep(wait_seconds)
95
+
96
+ if not service_ready:
97
+ client.delete_pod(pod_name)
98
+ client.delete_service(service_name)
99
+ return None
100
+
101
+ if pod_ready and service_ready:
102
+ if mcp_servers:
103
+ try:
104
+ metadata = {
105
+ "pod_name": pod_name,
106
+ "service_name": service_name,
107
+ "status": pod_info.get("status"),
108
+ "cluster_ip": service_info.get("cluster_ip"),
109
+ "host": service_info.get("host"),
110
+ }
111
+
112
+ response = cls._get_mcp_configs(
113
+ mcp_servers=mcp_servers,
114
+ mcp_config=mcp_config,
115
+ metadata=metadata,
116
+ env_type=SandboxEnvType.K8S
117
+ )
118
+
119
+ if response:
120
+ mcp_config = response
121
+ except Exception as e:
122
+ logging.warning(f"Failed to get mcp configs: {e}")
123
+
124
+ return SandboxK8sResponse(
125
+ pod_name=pod_name,
126
+ service_name=service_name,
127
+ status=pod_info.get("status"),
128
+ cluster_ip=service_info.get("cluster_ip"),
129
+ host=service_info.get("host"),
130
+ mcp_config=mcp_config,
131
+ env_type=SandboxEnvType.K8S,
132
+ )
133
+
134
+ except Exception as e:
135
+ logging.info(f"Failed to create Sandbox by k8s: {e}")
136
+ # Only attempt to delete resources if client has been initialized
137
+ if client:
138
+ if pod_name:
139
+ client.delete_pod(pod_name)
140
+ if service_name:
141
+ client.delete_service(service_name)
142
+ return None
143
+
144
+ @classmethod
145
+ def generate_random_string(cls, length=6):
146
+ """
147
+ Generate a random string of specified length.
148
+ """
149
+ characters = string.ascii_lowercase + string.digits
150
+ return ''.join(random.choice(characters) for _ in range(length))
151
+
152
+ @classmethod
153
+ def _get_mcp_configs(
154
+ cls,
155
+ mcp_servers: Optional[List[str]] = None,
156
+ mcp_config: Optional[Any] = None,
157
+ metadata: Optional[Dict[str, str]] = None,
158
+ env_type: Optional[int] = None,
159
+ ) -> Any:
160
+ """
161
+ Get MCP configurations for the sandbox.
162
+ """
163
+ try:
164
+ if not metadata or (
165
+ not metadata.get("cluster_ip") and not metadata.get("host")):
166
+ return mcp_config
167
+ host = metadata.get("host") or metadata.get("cluster_ip")
168
+
169
+ if not mcp_servers:
170
+ return None
171
+ if not mcp_config or mcp_config.get("mcpServers") is None:
172
+ mcp_config = {
173
+ "mcpServers": {}
174
+ }
175
+ _mcp_servers = mcp_config.get("mcpServers")
176
+
177
+ for server in mcp_servers:
178
+ if server not in _mcp_servers:
179
+ _mcp_servers[server] = {
180
+ "type": "api",
181
+ "url": f"http://{host}:80/{server}"
182
+ }
183
+
184
+ return mcp_config
185
+ except Exception as e:
186
+ logging.warning(f"Failed to get_mcp_configs_from_k8s: {e}")
187
+ return None
188
+
189
+ @classmethod
190
+ async def _remove_sandbox(
191
+ cls,
192
+ sandbox_id: Optional[str] = None,
193
+ metadata: Optional[Dict[str, str]] = None,
194
+ env_type: Optional[int] = None,
195
+ ) -> bool:
196
+ """
197
+ Remove the Kubernetes sandbox and clean up resources.
198
+ """
199
+ try:
200
+ if not sandbox_id or not metadata:
201
+ logging.warning(f"sandbox_id={sandbox_id} or metadata={metadata} is None")
202
+ return False
203
+
204
+ pod_name = metadata.get("pod_name")
205
+ service_name = metadata.get("service_name")
206
+
207
+ if not pod_name or not service_name:
208
+ logging.warning(f"pod_name={pod_name} or service_name={service_name} is None")
209
+ return False
210
+
211
+ client = KubernetesApiClient()
212
+ client.delete_pod(pod_name)
213
+ client.delete_service(service_name)
214
+ return True
215
+
216
+ except Exception as e:
217
+ logging.warning(f"Failed to remove Sandbox: {e}")
218
+ return False