|
19 | 19 | from typing import Dict, List, Optional |
20 | 20 | from sagemaker_core.main.resources import Endpoint |
21 | 21 | from pydantic import Field, ValidationError |
| 22 | +from kubernetes import client |
22 | 23 |
|
23 | 24 |
|
24 | 25 | class HPEndpoint(_HPEndpoint, HPEndpointBase): |
@@ -211,3 +212,34 @@ def validate_instance_type(self, instance_type: str): |
211 | 212 | raise Exception( |
212 | 213 | f"Current HyperPod cluster does not have instance type {instance_type}. Supported instance types are {cluster_instance_types}" |
213 | 214 | ) |
| 215 | + |
| 216 | + @classmethod |
| 217 | + @_hyperpod_telemetry_emitter(Feature.HYPERPOD, "list_pods_endpoint") |
| 218 | + def list_pods(cls, namespace=None): |
| 219 | + cls.verify_kube_config() |
| 220 | + |
| 221 | + if not namespace: |
| 222 | + namespace = get_default_namespace() |
| 223 | + |
| 224 | + v1 = client.CoreV1Api() |
| 225 | + list_pods_response = v1.list_namespaced_pod(namespace=namespace) |
| 226 | + |
| 227 | + list_response = cls.call_list_api( |
| 228 | + kind=INFERENCE_ENDPOINT_CONFIG_KIND, |
| 229 | + namespace=namespace, |
| 230 | + ) |
| 231 | + |
| 232 | + endpoints = set() |
| 233 | + if list_response and list_response["items"]: |
| 234 | + for item in list_response["items"]: |
| 235 | + endpoints.add(item["metadata"]["name"]) |
| 236 | + |
| 237 | + pods = [] |
| 238 | + for item in list_pods_response.items: |
| 239 | + app_name = item.metadata.labels.get("app", None) |
| 240 | + if app_name in endpoints: |
| 241 | + # list_namespaced_pod will return all pods in the namespace, so we need to filter |
| 242 | + # out the pods that are created by custom endpoint |
| 243 | + pods.append(item.metadata.name) |
| 244 | + |
| 245 | + return pods |
0 commit comments