From 55c6d8c22d6dcd03e994a87f4f045d459d2bc334 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: Wed, 20 Nov 2024 16:41:34 +0800 Subject: [PATCH] fix: svc update sync apisixroute (#2055) 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 | 8 +++- worker/appm/store/store.go | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/api/controller/apigateway/api_gateway_route.go b/api/controller/apigateway/api_gateway_route.go index d6757cd040..e52a6d5f3f 100644 --- a/api/controller/apigateway/api_gateway_route.go +++ b/api/controller/apigateway/api_gateway_route.go @@ -180,11 +180,13 @@ func (g Struct) CreateHTTPAPIRoute(w http.ResponseWriter, r *http.Request) { if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &apisixRouteHTTP, nil) { return } - sLabel := strings.Split(r.URL.Query().Get("service_alias"), ",") + sa := r.URL.Query().Get("service_alias") + sLabel := strings.Split(sa, ",") // 如果没有绑定appId,那么不要加这个lable labels := make(map[string]string) labels["creator"] = "Rainbond" labels["port"] = r.URL.Query().Get("port") + labels["component_sort"] = sa if r.URL.Query().Get("appID") != "" { labels["app_id"] = r.URL.Query().Get("appID") } @@ -276,6 +278,10 @@ func (g Struct) CreateHTTPAPIRoute(w http.ResponseWriter, r *http.Request) { httputil.ReturnSuccess(r, w, marshalApisixRoute(update)) } +func (g Struct) UpdateHTTPAPIRoute() { + +} + func marshalApisixRoute(r *v2.ApisixRoute) map[string]interface{} { r.TypeMeta.Kind = util.ApisixRoute r.TypeMeta.APIVersion = util.APIVersion diff --git a/worker/appm/store/store.go b/worker/appm/store/store.go index 91c45a3cef..0f508d6586 100644 --- a/worker/appm/store/store.go +++ b/worker/appm/store/store.go @@ -582,6 +582,7 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) { serviceID := service.Labels["service_id"] version := service.Labels["version"] createrID := service.Labels["creater_id"] + serviceAlias := service.Labels["service_alias"] appID := service.Labels["app_id"] if serviceID != "" && createrID != "" { appservice, err := a.getAppService(serviceID, version, createrID, true) @@ -589,6 +590,7 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) { a.k8sClient.Clientset.CoreV1().Services(service.Namespace).Delete(context.Background(), service.Name, metav1.DeleteOptions{}) } if appservice != nil { + a.SyncUpdateApisixRoute(service.Namespace, serviceAlias, service.Name) appservice.SetService(service) return } @@ -1964,6 +1966,42 @@ func (a *appRuntimeStore) ListServices(namespace string, selector labels.Selecto return a.listers.Service.Services(namespace).List(selector) } +func (a *appRuntimeStore) SyncUpdateApisixRoute(namespace, serviceAlias, serviceName string) { + routes, err := a.k8sClient.ApiSixClient.ApisixV2().ApisixRoutes(namespace).List(context.Background(), metav1.ListOptions{ + LabelSelector: "service_alias=" + serviceAlias, + }) + if err != nil { + logrus.Errorf("list routes failure: %v", err) + } + for _, route := range routes.Items { + sort := route.Labels["component_sort"] + sortList := strings.Split(sort, ",") + for i, sa := range sortList { + if sa != serviceAlias { + continue + } + routeHTTP := route.Spec.HTTP + if routeHTTP != nil && len(routeHTTP) > 0 { + backends := routeHTTP[0].Backends + if backends != nil && len(backends) > 0 { + backend := backends[i] + if backend.ServiceName != serviceName { + // 更新 ServiceName + backend.ServiceName = serviceName + // 更新路由 + _, err := a.k8sClient.ApiSixClient.ApisixV2().ApisixRoutes(namespace).Update(context.Background(), &route, metav1.UpdateOptions{}) + if err != nil { + logrus.Errorf("update route failure: %v", err) + } else { + logrus.Infof("successfully updated route %s with new service name %s", route.Name, serviceName) + } + } + } + } + } + } +} + func isImagePullSecretEqual(a, b *corev1.Secret) bool { if len(a.Data) != len(b.Data) { return false