19
19
20
20
@router .post ("/" )
21
21
async def create_pod (name : str ) -> PodRef :
22
- """Creates a Kubernetes pod."""
22
+ """Creates a managed pod."""
23
23
# add random string to pod name to avoid conflicts
24
24
pod_name = f"{ name } -{ secrets .token_hex (12 )} "
25
25
pod_manifest = {
@@ -45,14 +45,9 @@ async def create_pod(name: str) -> PodRef:
45
45
#return PodRef(name=pod_name, image=_config.APP_IMAGE)
46
46
return await get_pod (pod_name )
47
47
48
- @router .delete ("/{pod_name}" , status_code = 204 )
49
- async def delete_pod (pod_name : str ):
50
- """Deletes a Kubernetes pod."""
51
- v1 .delete_namespaced_pod (name = pod_name , namespace = _config .NAMESPACE )
52
-
53
48
@router .get ("/{pod_name}" )
54
49
async def get_pod (pod_name : str ) -> PodRef :
55
- """Fetches the status of a specific Kubernetes pod."""
50
+ """Fetches the status of a specific managed pod."""
56
51
try :
57
52
pod = v1 .read_namespaced_pod (name = pod_name , namespace = _config .NAMESPACE )
58
53
image = pod .spec .containers [0 ].image
@@ -66,9 +61,24 @@ async def get_pod(pod_name: str) -> PodRef:
66
61
raise HTTPException (status_code = 404 , detail = "Pod not found" )
67
62
raise HTTPException (status_code = 500 , detail = f"Error retrieving pod status: { str (e )} " )
68
63
64
+ @router .delete ("/{pod_name}" , status_code = 204 )
65
+ async def delete_pod (pod_name : str ):
66
+ """Deletes a managed pod."""
67
+ pods = await list_pods ()
68
+ if pod_name not in [pod .name for pod in pods .items ]:
69
+ raise HTTPException (status_code = 404 , detail = "Pod not found" )
70
+ v1 .delete_namespaced_pod (name = pod_name , namespace = _config .NAMESPACE )
71
+
69
72
@router .get ("/" )
70
73
async def list_pods () -> PodRefs :
71
- """Lists all pods in the namespace."""
74
+ """Lists all managed pods in the namespace."""
72
75
pods = v1 .list_namespaced_pod (namespace = _config .NAMESPACE )
73
76
pod_list = [PodRef (name = pod .metadata .name , image = _config .APP_IMAGE , status = pod .status .phase , ip = pod .status .pod_ip , port = _config .APP_PORT ) for pod in pods .items if pod .spec .containers [0 ].image == _config .APP_IMAGE ]
74
- return PodRefs (items = pod_list )
77
+ return PodRefs (items = pod_list )
78
+
79
+ @router .delete ("/" , status_code = 204 )
80
+ async def delete_pods ():
81
+ """Deletes all managed pods."""
82
+ pods = await list_pods ()
83
+ for pod in pods .items :
84
+ v1 .delete_namespaced_pod (name = pod .name , namespace = _config .NAMESPACE )
0 commit comments