fix: implement proper resource deletion for all resource types
This commit is contained in:
@@ -191,6 +191,74 @@ export default function K8sResourceList() {
|
||||
return () => eventSource.close()
|
||||
}
|
||||
|
||||
const handleDeleteResource = async () => {
|
||||
if (!deleteTarget) return
|
||||
|
||||
setDeleting(true)
|
||||
try {
|
||||
let endpoint = '';
|
||||
let kind = '';
|
||||
|
||||
// Determine the correct endpoint based on the selected resource kind
|
||||
switch (selectedKind.key) {
|
||||
case 'pod':
|
||||
endpoint = '/api/v1/k8s/pod/delete'
|
||||
kind = 'Pod'
|
||||
break
|
||||
case 'deployment':
|
||||
endpoint = '/api/v1/k8s/deployment/delete'
|
||||
kind = 'Deployment'
|
||||
break
|
||||
case 'statefulset':
|
||||
endpoint = '/api/v1/k8s/statefulset/delete'
|
||||
kind = 'StatefulSet'
|
||||
break
|
||||
case 'service':
|
||||
endpoint = '/api/v1/k8s/service/delete'
|
||||
kind = 'Service'
|
||||
break
|
||||
case 'configmap':
|
||||
endpoint = '/api/v1/k8s/configmap/delete'
|
||||
kind = 'ConfigMap'
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported resource kind: ${selectedKind.key}`)
|
||||
}
|
||||
|
||||
const res = await fetch(endpoint, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
name: deleteTarget.name,
|
||||
namespace: deleteTarget.namespace
|
||||
}),
|
||||
})
|
||||
|
||||
const result = await res.json()
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(result.err || `Failed to delete ${kind}`)
|
||||
}
|
||||
|
||||
setSnackbar({
|
||||
open: true,
|
||||
message: `${kind} 删除成功`,
|
||||
severity: 'success'
|
||||
})
|
||||
setDeleteDialogOpen(false)
|
||||
setDeleteTarget(null)
|
||||
fetchResources()
|
||||
} catch (e: any) {
|
||||
setSnackbar({
|
||||
open: true,
|
||||
message: `删除失败: ${e.message}`,
|
||||
severity: 'error'
|
||||
})
|
||||
} finally {
|
||||
setDeleting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDeletePod = async () => {
|
||||
if (!deleteTarget) return
|
||||
|
||||
@@ -806,7 +874,7 @@ export default function K8sResourceList() {
|
||||
<Button
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={handleDeletePod}
|
||||
onClick={handleDeleteResource}
|
||||
disabled={deleting}
|
||||
>
|
||||
{deleting ? <CircularProgress size={24} /> : '删除'}
|
||||
|
||||
Reference in New Issue
Block a user