From 843f468c29c200765599537d92bcb03fdd2d2a69 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, 16 Dec 2024 13:41:13 +0800 Subject: [PATCH] fix: fix the problem of unable to delete routes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张启航 <101104760+ZhangSetSail@users.noreply.github.com> --- api/controller/apigateway/api_gateway_route.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api/controller/apigateway/api_gateway_route.go b/api/controller/apigateway/api_gateway_route.go index c2ab66ccb..3a2b0d5b2 100644 --- a/api/controller/apigateway/api_gateway_route.go +++ b/api/controller/apigateway/api_gateway_route.go @@ -3,6 +3,7 @@ package apigateway import ( "fmt" "net/http" + "regexp" "strings" v2 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2" @@ -248,7 +249,7 @@ func (g Struct) CreateHTTPAPIRoute(w http.ResponseWriter, r *http.Request) { if err == nil { name := r.URL.Query().Get("name") if name != "" { - name = name[1:] + name = removeLeadingDigits(name) err = c.ApisixRoutes(tenant.Namespace).Delete(r.Context(), name, v1.DeleteOptions{}) if err != nil { logrus.Errorf("delete route %v failure: %v", name, err) @@ -302,7 +303,7 @@ func (g Struct) DeleteHTTPAPIRoute(w http.ResponseWriter, r *http.Request) { var deleteName = make([]string, 0) tenant := r.Context().Value(ctxutil.ContextKey("tenant")).(*dbmodel.Tenants) name := chi.URLParam(r, "name") - name = name[1:] + name = removeLeadingDigits(name) c := k8s.Default().ApiSixClient.ApisixV2() err := c.ApisixRoutes(tenant.Namespace).Delete(r.Context(), name, v1.DeleteOptions{}) @@ -515,3 +516,10 @@ func (g Struct) DeleteTCPRoute(w http.ResponseWriter, r *http.Request) { } httputil.ReturnSuccess(r, w, name) } + +func removeLeadingDigits(name string) string { + // 使用正则表达式匹配前面的数字 + re := regexp.MustCompile(`^\d+`) + // 将匹配到的数字替换为空字符串 + return re.ReplaceAllString(name, "") +}