mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	单个节点支持多个DNS线路
This commit is contained in:
		@@ -19,7 +19,7 @@ func (this *DNSDomain) DecodeRoutes() ([]*dnsclients.Route, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 检查是否包含某个线路
 | 
			
		||||
func (this *DNSDomain) ContainsRoute(route string) (bool, error) {
 | 
			
		||||
func (this *DNSDomain) ContainsRouteCode(route string) (bool, error) {
 | 
			
		||||
	routes, err := this.DecodeRoutes()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
 
 | 
			
		||||
@@ -458,11 +458,11 @@ func (this *NodeClusterDAO) CheckClusterDNS(cluster *NodeCluster) (issues []*pb.
 | 
			
		||||
	for _, node := range nodes {
 | 
			
		||||
		nodeId := int64(node.Id)
 | 
			
		||||
 | 
			
		||||
		route, err := node.DNSRoute(domainId)
 | 
			
		||||
		routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		if len(route) == 0 {
 | 
			
		||||
		if len(routeCodes) == 0 {
 | 
			
		||||
			issues = append(issues, &pb.DNSIssue{
 | 
			
		||||
				Target:      node.Name,
 | 
			
		||||
				TargetId:    nodeId,
 | 
			
		||||
@@ -477,22 +477,24 @@ func (this *NodeClusterDAO) CheckClusterDNS(cluster *NodeCluster) (issues []*pb.
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 检查线路是否在已有线路中
 | 
			
		||||
		routeOk, err := domain.ContainsRoute(route)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		if !routeOk {
 | 
			
		||||
			issues = append(issues, &pb.DNSIssue{
 | 
			
		||||
				Target:      node.Name,
 | 
			
		||||
				TargetId:    nodeId,
 | 
			
		||||
				Type:        "node",
 | 
			
		||||
				Description: "线路已经失效,请重新选择",
 | 
			
		||||
				Params: map[string]string{
 | 
			
		||||
					"clusterName": cluster.Name,
 | 
			
		||||
					"clusterId":   numberutils.FormatInt64(clusterId),
 | 
			
		||||
				},
 | 
			
		||||
			})
 | 
			
		||||
			continue
 | 
			
		||||
		for _, routeCode := range routeCodes {
 | 
			
		||||
			routeOk, err := domain.ContainsRouteCode(routeCode)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			if !routeOk {
 | 
			
		||||
				issues = append(issues, &pb.DNSIssue{
 | 
			
		||||
					Target:      node.Name,
 | 
			
		||||
					TargetId:    nodeId,
 | 
			
		||||
					Type:        "node",
 | 
			
		||||
					Description: "线路已经失效,请重新选择",
 | 
			
		||||
					Params: map[string]string{
 | 
			
		||||
						"clusterName": cluster.Name,
 | 
			
		||||
						"clusterId":   numberutils.FormatInt64(clusterId),
 | 
			
		||||
					},
 | 
			
		||||
				})
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 检查IP地址
 | 
			
		||||
 
 | 
			
		||||
@@ -629,12 +629,12 @@ func (this *NodeDAO) FindEnabledNodeDNS(nodeId int64) (*Node, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 修改节点的DNS信息
 | 
			
		||||
func (this *NodeDAO) UpdateNodeDNS(nodeId int64, routes map[int64]string) error {
 | 
			
		||||
func (this *NodeDAO) UpdateNodeDNS(nodeId int64, routes map[int64][]string) error {
 | 
			
		||||
	if nodeId <= 0 {
 | 
			
		||||
		return errors.New("invalid nodeId")
 | 
			
		||||
	}
 | 
			
		||||
	if routes == nil {
 | 
			
		||||
		routes = map[int64]string{}
 | 
			
		||||
		routes = map[int64][]string{}
 | 
			
		||||
	}
 | 
			
		||||
	routesJSON, err := json.Marshal(routes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -41,28 +41,28 @@ func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 所有的DNS线路
 | 
			
		||||
func (this *Node) DNSRoutes() (map[int64]string, error) {
 | 
			
		||||
	routes := map[int64]string{} // domainId => route
 | 
			
		||||
func (this *Node) DNSRouteCodes() (map[int64][]string, error) {
 | 
			
		||||
	routes := map[int64][]string{} // domainId => routes
 | 
			
		||||
	if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
 | 
			
		||||
		return routes, nil
 | 
			
		||||
	}
 | 
			
		||||
	err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return map[int64]string{}, err
 | 
			
		||||
		return map[int64][]string{}, err
 | 
			
		||||
	}
 | 
			
		||||
	return routes, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DNS线路
 | 
			
		||||
func (this *Node) DNSRoute(dnsDomainId int64) (string, error) {
 | 
			
		||||
	routes := map[int64]string{} // domainId => route
 | 
			
		||||
func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) {
 | 
			
		||||
	routes := map[int64][]string{} // domainId => routes
 | 
			
		||||
	if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
 | 
			
		||||
		return "", nil
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
	err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	route, _ := routes[dnsDomainId]
 | 
			
		||||
	return route, nil
 | 
			
		||||
	domainRoutes, _ := routes[dnsDomainId]
 | 
			
		||||
	return domainRoutes, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -382,30 +382,32 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
 | 
			
		||||
		if len(ipAddr) == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		route, err := node.DNSRoute(int64(cluster.DnsDomainId))
 | 
			
		||||
		routeCodes, err := node.DNSRouteCodesForDomainId(int64(cluster.DnsDomainId))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, nil, false, false, err
 | 
			
		||||
		}
 | 
			
		||||
		if len(route) == 0 {
 | 
			
		||||
		if len(routeCodes) == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		key := ipAddr + "_" + route
 | 
			
		||||
		nodeKeys = append(nodeKeys, key)
 | 
			
		||||
		record, ok := nodeRecordMapping[key]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			result = append(result, maps.Map{
 | 
			
		||||
				"action": "create",
 | 
			
		||||
				"record": &dnsclients.Record{
 | 
			
		||||
					Id:    "",
 | 
			
		||||
					Name:  clusterDnsName,
 | 
			
		||||
					Type:  dnsclients.RecordTypeA,
 | 
			
		||||
					Value: ipAddr,
 | 
			
		||||
					Route: route,
 | 
			
		||||
				},
 | 
			
		||||
			})
 | 
			
		||||
			nodesChanged = true
 | 
			
		||||
		} else {
 | 
			
		||||
			doneNodeRecords = append(doneNodeRecords, record)
 | 
			
		||||
		for _, route := range routeCodes {
 | 
			
		||||
			key := ipAddr + "_" + route
 | 
			
		||||
			nodeKeys = append(nodeKeys, key)
 | 
			
		||||
			record, ok := nodeRecordMapping[key]
 | 
			
		||||
			if !ok {
 | 
			
		||||
				result = append(result, maps.Map{
 | 
			
		||||
					"action": "create",
 | 
			
		||||
					"record": &dnsclients.Record{
 | 
			
		||||
						Id:    "",
 | 
			
		||||
						Name:  clusterDnsName,
 | 
			
		||||
						Type:  dnsclients.RecordTypeA,
 | 
			
		||||
						Value: ipAddr,
 | 
			
		||||
						Route: route,
 | 
			
		||||
					},
 | 
			
		||||
				})
 | 
			
		||||
				nodesChanged = true
 | 
			
		||||
			} else {
 | 
			
		||||
				doneNodeRecords = append(doneNodeRecords, record)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/events"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/installers"
 | 
			
		||||
@@ -55,9 +56,9 @@ func (this *NodeService) CreateNode(ctx context.Context, req *pb.CreateNodeReque
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存DNS相关
 | 
			
		||||
	if req.DnsDomainId > 0 && len(req.DnsRoute) > 0 {
 | 
			
		||||
		err = models.SharedNodeDAO.UpdateNodeDNS(nodeId, map[int64]string{
 | 
			
		||||
			req.DnsDomainId: req.DnsRoute,
 | 
			
		||||
	if req.DnsDomainId > 0 && len(req.DnsRoutes) > 0 {
 | 
			
		||||
		err = models.SharedNodeDAO.UpdateNodeDNS(nodeId, map[int64][]string{
 | 
			
		||||
			req.DnsDomainId: req.DnsRoutes,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
@@ -150,6 +151,20 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clusterDNS, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(req.ClusterId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	dnsDomainId := int64(clusterDNS.DnsDomainId)
 | 
			
		||||
	domainRoutes := []*dnsclients.Route{}
 | 
			
		||||
	if clusterDNS.DnsDomainId > 0 {
 | 
			
		||||
		domainRoutes, err = models.SharedDNSDomainDAO.FindDomainRoutes(dnsDomainId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(req.Offset, req.Size, req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.GroupId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -194,6 +209,24 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// DNS线路
 | 
			
		||||
		routeCodes, err := node.DNSRouteCodesForDomainId(dnsDomainId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		pbRoutes := []*pb.DNSRoute{}
 | 
			
		||||
		for _, routeCode := range routeCodes {
 | 
			
		||||
			for _, route := range domainRoutes {
 | 
			
		||||
				if route.Code == routeCode {
 | 
			
		||||
					pbRoutes = append(pbRoutes, &pb.DNSRoute{
 | 
			
		||||
						Name: route.Name,
 | 
			
		||||
						Code: route.Code,
 | 
			
		||||
					})
 | 
			
		||||
					break
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		result = append(result, &pb.Node{
 | 
			
		||||
			Id:          int64(node.Id),
 | 
			
		||||
			Name:        node.Name,
 | 
			
		||||
@@ -209,6 +242,7 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
 | 
			
		||||
			IsOn:          node.IsOn == 1,
 | 
			
		||||
			IsUp:          node.IsUp == 1,
 | 
			
		||||
			Group:         pbGroup,
 | 
			
		||||
			DnsRoutes:     pbRoutes,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -305,6 +339,16 @@ func (this *NodeService) UpdateNode(ctx context.Context, req *pb.UpdateNodeReque
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存DNS相关
 | 
			
		||||
	if req.DnsDomainId > 0 && len(req.DnsRoutes) > 0 {
 | 
			
		||||
		err = models.SharedNodeDAO.UpdateNodeDNS(req.NodeId, map[int64][]string{
 | 
			
		||||
			req.DnsDomainId: req.DnsRoutes,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 同步DNS
 | 
			
		||||
	go func() {
 | 
			
		||||
		// TODO 只有状态变化的时候才需要同步
 | 
			
		||||
@@ -941,15 +985,21 @@ func (this *NodeService) FindAllEnabledNodesDNSWithClusterId(ctx context.Context
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		route, err := node.DNSRoute(dnsDomainId)
 | 
			
		||||
		domainRouteCodes, err := node.DNSRouteCodesForDomainId(dnsDomainId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		routeName := ""
 | 
			
		||||
		for _, r := range routes {
 | 
			
		||||
			if r.Code == route {
 | 
			
		||||
				routeName = r.Name
 | 
			
		||||
		pbRoutes := []*pb.DNSRoute{}
 | 
			
		||||
		for _, routeCode := range domainRouteCodes {
 | 
			
		||||
			for _, r := range routes {
 | 
			
		||||
				if r.Code == routeCode {
 | 
			
		||||
					pbRoutes = append(pbRoutes, &pb.DNSRoute{
 | 
			
		||||
						Name: r.Name,
 | 
			
		||||
						Code: r.Code,
 | 
			
		||||
					})
 | 
			
		||||
					break
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -957,10 +1007,7 @@ func (this *NodeService) FindAllEnabledNodesDNSWithClusterId(ctx context.Context
 | 
			
		||||
			Id:     int64(node.Id),
 | 
			
		||||
			Name:   node.Name,
 | 
			
		||||
			IpAddr: ipAddr,
 | 
			
		||||
			Route: &pb.DNSRoute{
 | 
			
		||||
				Name: routeName,
 | 
			
		||||
				Code: route,
 | 
			
		||||
			},
 | 
			
		||||
			Routes: pbRoutes,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.FindAllEnabledNodesDNSWithClusterIdResponse{Nodes: result}, nil
 | 
			
		||||
@@ -998,17 +1045,22 @@ func (this *NodeService) FindEnabledNodeDNS(ctx context.Context, req *pb.FindEna
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var route = ""
 | 
			
		||||
	var routeName = ""
 | 
			
		||||
	pbRoutes := []*pb.DNSRoute{}
 | 
			
		||||
	if dnsDomainId > 0 {
 | 
			
		||||
		route, err = node.DNSRoute(dnsDomainId)
 | 
			
		||||
		routeCodes, err := node.DNSRouteCodesForDomainId(dnsDomainId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		routeName, err = models.SharedDNSDomainDAO.FindDomainRouteName(dnsDomainId, route)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		for _, routeCode := range routeCodes {
 | 
			
		||||
			routeName, err := models.SharedDNSDomainDAO.FindDomainRouteName(dnsDomainId, routeCode)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			pbRoutes = append(pbRoutes, &pb.DNSRoute{
 | 
			
		||||
				Name: routeName,
 | 
			
		||||
				Code: routeCode,
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -1019,13 +1071,10 @@ func (this *NodeService) FindEnabledNodeDNS(ctx context.Context, req *pb.FindEna
 | 
			
		||||
 | 
			
		||||
	return &pb.FindEnabledNodeDNSResponse{
 | 
			
		||||
		Node: &pb.NodeDNSInfo{
 | 
			
		||||
			Id:     int64(node.Id),
 | 
			
		||||
			Name:   node.Name,
 | 
			
		||||
			IpAddr: ipAddr,
 | 
			
		||||
			Route: &pb.DNSRoute{
 | 
			
		||||
				Name: routeName,
 | 
			
		||||
				Code: route,
 | 
			
		||||
			},
 | 
			
		||||
			Id:            int64(node.Id),
 | 
			
		||||
			Name:          node.Name,
 | 
			
		||||
			IpAddr:        ipAddr,
 | 
			
		||||
			Routes:        pbRoutes,
 | 
			
		||||
			ClusterId:     clusterId,
 | 
			
		||||
			DnsDomainId:   dnsDomainId,
 | 
			
		||||
			DnsDomainName: dnsDomainName,
 | 
			
		||||
@@ -1050,15 +1099,15 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
 | 
			
		||||
		return nil, errors.New("node not found")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	routes, err := node.DNSRoutes()
 | 
			
		||||
	routeCodeMap, err := node.DNSRouteCodes()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if req.DnsDomainId > 0 && len(req.Route) > 0 {
 | 
			
		||||
		routes[req.DnsDomainId] = req.Route
 | 
			
		||||
	if req.DnsDomainId > 0 && len(req.Routes) > 0 {
 | 
			
		||||
		routeCodeMap[req.DnsDomainId] = req.Routes
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeDAO.UpdateNodeDNS(req.NodeId, routes)
 | 
			
		||||
	err = models.SharedNodeDAO.UpdateNodeDNS(req.NodeId, routeCodeMap)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user