From 8140d43d92874186276d646247f55e1d6f062c3a 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: Tue, 17 Dec 2024 11:57:43 +0800 Subject: [PATCH] fix: install app model not create tcp rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张启航 <101104760+ZhangSetSail@users.noreply.github.com> --- db/dao/dao.go | 1 + db/mysql/dao/gateway.go | 12 +++++++ worker/appm/conversion/gateway.go | 56 +++++++++++++++++++++++++++++-- worker/appm/store/store.go | 23 +++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/db/dao/dao.go b/db/dao/dao.go index ebd70c6c4..1aa7f5c3c 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -566,6 +566,7 @@ type TCPRuleDao interface { Dao GetTCPRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.TCPRule, error) GetTCPRuleByID(id string) (*model.TCPRule, error) + GetTCPRuleByPort(port int) (*model.TCPRule, error) GetTCPRuleByServiceID(sid string) ([]*model.TCPRule, error) DeleteByID(uuid string) error DeleteTCPRuleByServiceID(serviceID string) error diff --git a/db/mysql/dao/gateway.go b/db/mysql/dao/gateway.go index b05bd9051..91d44b768 100644 --- a/db/mysql/dao/gateway.go +++ b/db/mysql/dao/gateway.go @@ -495,6 +495,18 @@ func (t *TCPRuleDaoTmpl) GetTCPRuleByID(id string) (*model.TCPRule, error) { return result, nil } +// GetTCPRuleByPort gets a TCPRule based on port +func (t *TCPRuleDaoTmpl) GetTCPRuleByPort(port int) (*model.TCPRule, error) { + result := &model.TCPRule{} + if err := t.DB.Where("port = ?", port).Find(result).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return nil, nil + } + return nil, err + } + return result, nil +} + // GetTCPRuleByServiceID gets a TCPRules based on service id. func (t *TCPRuleDaoTmpl) GetTCPRuleByServiceID(sid string) ([]*model.TCPRule, error) { var result []*model.TCPRule diff --git a/worker/appm/conversion/gateway.go b/worker/appm/conversion/gateway.go index dee54dfd1..f376606d7 100644 --- a/worker/appm/conversion/gateway.go +++ b/worker/appm/conversion/gateway.go @@ -181,10 +181,13 @@ func (a *AppServiceBuild) Build(as *v1.AppService) (*v1.K8sResources, error) { innerService = append(innerService, port) } if *port.IsOuterService { - route := a.generateOuterDomain(as, port) + route, svc := a.generateOuterDomain(as, port) if route != nil { apiSixRoutes = append(apiSixRoutes, route) } + if svc != nil { + services = append(services, svc) + } } } } @@ -382,11 +385,15 @@ func generateSVCPortName(protocol string, containerPort int) string { return fmt.Sprintf("%s-%d", strings.ToLower(protocol), containerPort) } -func (a *AppServiceBuild) generateOuterDomain(as *v1.AppService, port *model.TenantServicesPort) (outerRoutes *v2.ApisixRoute) { +func (a *AppServiceBuild) generateOuterDomain(as *v1.AppService, port *model.TenantServicesPort) (outerRoutes *v2.ApisixRoute, outerSVC *corev1.Service) { httpRules, err := a.dbmanager.HTTPRuleDao().GetHTTPRuleByServiceIDAndContainerPort(as.ServiceID, port.ContainerPort) if err != nil { logrus.Infof("Can't get HTTPRule corresponding to ServiceID(%s): %v", as.ServiceID, err) } + tcpRules, err := a.dbmanager.TCPRuleDao().GetTCPRuleByServiceIDAndContainerPort(as.ServiceID, port.ContainerPort) + if err != nil { + logrus.Infof("Can't get HTTPRule corresponding to ServiceID(%s): %v", as.ServiceID, err) + } // create http ingresses logrus.Debugf("find %d count http rule", len(httpRules)) if len(httpRules) > 0 { @@ -455,5 +462,50 @@ func (a *AppServiceBuild) generateOuterDomain(as *v1.AppService, port *model.Ten } } } + if len(tcpRules) > 0 { + tcpRule := tcpRules[0] + svcs, err := k8s2.Default().Clientset.CoreV1().Services(as.GetNamespace()).List( + context.Background(), + metav1.ListOptions{ + LabelSelector: "service_alias=" + as.ServiceAlias + ",port=" + strconv.Itoa(tcpRule.Port), + }, + ) + if err != nil { + logrus.Errorf("generate outer domain list svcs failure: %v", err) + } else { + if svcs != nil && len(svcs.Items) > 0 { + logrus.Infof("%v svc num > 0, not create", as.ServiceAlias) + } else { + labels := make(map[string]string) + labels["creator"] = "Rainbond" + labels["tcp"] = "true" + labels["app_id"] = as.AppID + labels["service_id"] = as.ServiceID + labels["service_alias"] = as.ServiceAlias + labels["outer"] = "true" + labels["port"] = fmt.Sprintf("%v", tcpRule.ContainerPort) + name := fmt.Sprintf("%v-%v", as.ServiceAlias, tcpRule.Port) + spec := corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ + { + Protocol: corev1.Protocol("TCP"), + Name: name, + Port: int32(tcpRule.ContainerPort), + TargetPort: intstr.FromInt(tcpRule.ContainerPort), + NodePort: int32(tcpRule.Port), + }, + }, + Type: corev1.ServiceTypeNodePort, + } + outerSVC = &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Labels: labels, + Name: name, + }, + Spec: spec, + } + } + } + } return } diff --git a/worker/appm/store/store.go b/worker/appm/store/store.go index 8f9a748c2..00cfe7637 100644 --- a/worker/appm/store/store.go +++ b/worker/appm/store/store.go @@ -579,6 +579,29 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) { } } if service, ok := obj.(*corev1.Service); ok { + for _, port := range service.Spec.Ports { + if port.NodePort != 0 { + nodePort := int(port.NodePort) + // 查询数据库是否存在该端口 + exist, err := a.dbmanager.TCPRuleDao().GetTCPRuleByPort(nodePort) + if err != nil { + logrus.Errorf("get tcp rule by port failure: %v", err) + } + if exist == nil { + tcpRule := &model.TCPRule{ + UUID: "", + ServiceID: "", + ContainerPort: int(port.Port), + IP: "0.0.0.0", + Port: int(port.NodePort), + } + err = a.dbmanager.TCPRuleDao().AddModel(tcpRule) + if err != nil { + logrus.Errorf("add tcp rule failure: %v", err) + } + } + } + } serviceID := service.Labels["service_id"] version := service.Labels["version"] createrID := service.Labels["creater_id"]