From 785aaadbc76dc0dd8923d0ea84822d52aa210f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=90=AF=E8=88=AA?= <101104760+ZhangSetSail@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:56:07 +0800 Subject: [PATCH] fix: delete svc failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张启航 <101104760+ZhangSetSail@users.noreply.github.com> --- .../apigateway/api_gateway_route.go | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/api/controller/apigateway/api_gateway_route.go b/api/controller/apigateway/api_gateway_route.go index 9c01deeaa..de8a2d4bb 100644 --- a/api/controller/apigateway/api_gateway_route.go +++ b/api/controller/apigateway/api_gateway_route.go @@ -502,17 +502,45 @@ func (g Struct) DeleteTCPRoute(w http.ResponseWriter, r *http.Request) { tenant := r.Context().Value(ctxutil.ContextKey("tenant")).(*dbmodel.Tenants) name := chi.URLParam(r, "name") + // Split name by "-" to get alias and port + parts := strings.Split(name, "-") + if len(parts) != 2 { + httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete) + return + } + serviceAlias := parts[0] + port := parts[1] + + // Create label selector + labelSelector := fmt.Sprintf("outer=true,port=%s,service_alias=%s", port, serviceAlias) + k := k8s.Default().Clientset.CoreV1() - err := k.Services(tenant.Namespace).Delete(r.Context(), name, v1.DeleteOptions{}) + // List services with the specified labels + services, err := k.Services(tenant.Namespace).List(r.Context(), v1.ListOptions{ + LabelSelector: labelSelector, + }) if err != nil { - if errors.IsNotFound(err) { - httputil.ReturnSuccess(r, w, name) - } else { - logrus.Errorf("delete route error %s", err.Error()) + logrus.Errorf("list services error %s", err.Error()) + httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete) + return + } + + if len(services.Items) == 0 { + // No services found with these labels + httputil.ReturnSuccess(r, w, name) + return + } + + // Delete all matching services + for _, svc := range services.Items { + err := k.Services(tenant.Namespace).Delete(r.Context(), svc.Name, v1.DeleteOptions{}) + if err != nil && !errors.IsNotFound(err) { + logrus.Errorf("delete service %s error %s", svc.Name, err.Error()) httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete) + return } - return } + httputil.ReturnSuccess(r, w, name) }