Skip to content

Commit

Permalink
fix: install app model not create tcp rule
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail committed Dec 17, 2024
1 parent c22e386 commit 8140d43
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions db/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions db/mysql/dao/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 54 additions & 2 deletions worker/appm/conversion/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
23 changes: 23 additions & 0 deletions worker/appm/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 8140d43

Please sign in to comment.