Skip to content

Commit 6b9e0dc

Browse files
committedMar 14, 2025·
feat: delete all managed pods
1 parent 0605b6e commit 6b9e0dc

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed
 

‎rock_spawner/views/pod.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
@router.post("/")
2121
async def create_pod(name: str) -> PodRef:
22-
"""Creates a Kubernetes pod."""
22+
"""Creates a managed pod."""
2323
# add random string to pod name to avoid conflicts
2424
pod_name = f"{name}-{secrets.token_hex(12)}"
2525
pod_manifest = {
@@ -45,14 +45,9 @@ async def create_pod(name: str) -> PodRef:
4545
#return PodRef(name=pod_name, image=_config.APP_IMAGE)
4646
return await get_pod(pod_name)
4747

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-
5348
@router.get("/{pod_name}")
5449
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."""
5651
try:
5752
pod = v1.read_namespaced_pod(name=pod_name, namespace=_config.NAMESPACE)
5853
image = pod.spec.containers[0].image
@@ -66,9 +61,24 @@ async def get_pod(pod_name: str) -> PodRef:
6661
raise HTTPException(status_code=404, detail="Pod not found")
6762
raise HTTPException(status_code=500, detail=f"Error retrieving pod status: {str(e)}")
6863

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+
6972
@router.get("/")
7073
async def list_pods() -> PodRefs:
71-
"""Lists all pods in the namespace."""
74+
"""Lists all managed pods in the namespace."""
7275
pods = v1.list_namespaced_pod(namespace=_config.NAMESPACE)
7376
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

Comments
 (0)
Please sign in to comment.