feat: add delete functionality for namespace resources

This commit is contained in:
loveuer
2025-11-13 15:27:54 +08:00
parent 8ffb0eec09
commit 0536ce9755
3 changed files with 54 additions and 5 deletions

View File

@@ -71,6 +71,7 @@ func Init(ctx context.Context, address string, db *gorm.DB, store store.Store) (
k8sAPI.Post("/resource/update", k8s.K8sResourceUpdate(ctx, db, store))
// resource list
k8sAPI.Get("/namespace/list", k8s.K8sNamespaceList(ctx, db, store))
k8sAPI.Delete("/namespace/delete", k8s.K8sNamespaceDelete(ctx, db, store))
k8sAPI.Get("/deployment/list", k8s.K8sDeploymentList(ctx, db, store))
k8sAPI.Delete("/deployment/delete", k8s.K8sDeploymentDelete(ctx, db, store))
k8sAPI.Get("/statefulset/list", k8s.K8sStatefulSetList(ctx, db, store))

View File

@@ -927,3 +927,33 @@ func K8sConfigMapDelete(ctx context.Context, db *gorm.DB, store store.Store) fib
})
}
}
func K8sNamespaceDelete(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler {
return func(c fiber.Ctx) error {
var req struct {
Name string `json:"name"`
}
if err := json.Unmarshal(c.Body(), &req); err != nil {
return resp.R400(c, "", nil, err)
}
if req.Name == "" {
return resp.R400(c, "", nil, fmt.Errorf("name is required"))
}
clientset, err := getK8sClient(db)
if err != nil {
return resp.R500(c, "", nil, err)
}
err = clientset.CoreV1().Namespaces().Delete(c.Context(), req.Name, metav1.DeleteOptions{})
if err != nil {
return resp.R500(c, "", nil, fmt.Errorf("failed to delete namespace: %w", err))
}
return resp.R200(c, map[string]any{
"name": req.Name,
})
}
}