Skip to content

Commit

Permalink
fix: svc update sync apisixroute (#2055)
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail authored Nov 20, 2024
1 parent 26e3aab commit 55c6d8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/controller/apigateway/api_gateway_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions worker/appm/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,15 @@ 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)
if err == conversion.ErrServiceNotFound {
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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 55c6d8c

Please sign in to comment.