mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-25 14:36:35 +08:00
实现完整的集群、域名同步
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -142,6 +143,7 @@ func (this *DNSDomainDAO) UpdateDomainRecords(domainId int64, recordsJSON []byte
|
||||
op := NewDNSDomainOperator()
|
||||
op.Id = domainId
|
||||
op.Records = recordsJSON
|
||||
op.DataUpdatedAt = time.Now().Unix()
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
@@ -154,6 +156,7 @@ func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte)
|
||||
op := NewDNSDomainOperator()
|
||||
op.Id = domainId
|
||||
op.Routes = routesJSON
|
||||
op.DataUpdatedAt = time.Now().Unix()
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1 +1,43 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
)
|
||||
|
||||
// 获取所有的线路
|
||||
func (this *DNSDomain) DecodeRoutes() ([]string, error) {
|
||||
if len(this.Routes) == 0 || this.Routes == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
result := []string{}
|
||||
err := json.Unmarshal([]byte(this.Routes), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 检查是否包含某个线路
|
||||
func (this *DNSDomain) ContainsRoute(route string) (bool, error) {
|
||||
routes, err := this.DecodeRoutes()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return lists.ContainsString(routes, route), nil
|
||||
}
|
||||
|
||||
// 获取所有的记录
|
||||
func (this *DNSDomain) DecodeRecords() ([]*dnsclients.Record, error) {
|
||||
records := this.Records
|
||||
if len(records) == 0 || records == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
result := []*dnsclients.Record{}
|
||||
err := json.Unmarshal([]byte(records), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package models
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -319,7 +321,18 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(dnsDomainId in
|
||||
_, err = this.Query().
|
||||
State(NodeClusterStateEnabled).
|
||||
Attr("dnsDomainId", dnsDomainId).
|
||||
Result("id", "name", "dnsName").
|
||||
Result("id", "name", "dnsName", "dnsDomainId").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 查询已经设置了域名的集群
|
||||
func (this *NodeClusterDAO) FindAllEnabledClustersHaveDNSDomain() (result []*NodeCluster, err error) {
|
||||
_, err = this.Query().
|
||||
State(NodeClusterStateEnabled).
|
||||
Gt("dnsDomainId", 0).
|
||||
Result("id", "name", "dnsName", "dnsDomainId").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
@@ -337,7 +350,7 @@ func (this *NodeClusterDAO) FindClusterGrantId(clusterId int64) (int64, error) {
|
||||
func (this *NodeClusterDAO) FindClusterDNSInfo(clusterId int64) (*NodeCluster, error) {
|
||||
one, err := this.Query().
|
||||
Pk(clusterId).
|
||||
Result("dnsName", "dnsDomainId").
|
||||
Result("id", "name", "dnsName", "dnsDomainId").
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -371,6 +384,116 @@ func (this *NodeClusterDAO) UpdateClusterDNS(clusterId int64, dnsName string, dn
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查集群的DNS问题
|
||||
func (this *NodeClusterDAO) CheckClusterDNS(cluster *NodeCluster) (issues []*pb.DNSIssue, err error) {
|
||||
clusterId := int64(cluster.Id)
|
||||
domainId := int64(cluster.DnsDomainId)
|
||||
|
||||
// 检查域名
|
||||
domain, err := SharedDNSDomainDAO.FindEnabledDNSDomain(domainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domain == nil {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: cluster.Name,
|
||||
TargetId: clusterId,
|
||||
Type: "cluster",
|
||||
Description: "域名选择错误,需要重新选择",
|
||||
Params: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查二级域名
|
||||
if len(cluster.DnsName) == 0 {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: cluster.Name,
|
||||
TargetId: clusterId,
|
||||
Type: "cluster",
|
||||
Description: "没有设置二级域名",
|
||||
Params: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// TODO 检查域名格式
|
||||
|
||||
// TODO 检查域名是否已解析
|
||||
|
||||
// 检查节点
|
||||
nodes, err := SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(clusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 检查节点数量不能为0
|
||||
|
||||
for _, node := range nodes {
|
||||
nodeId := int64(node.Id)
|
||||
|
||||
route, err := node.DNSRoute(domainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(route) == 0 {
|
||||
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
|
||||
}
|
||||
|
||||
// 检查线路是否在已有线路中
|
||||
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
|
||||
}
|
||||
|
||||
// 检查IP地址
|
||||
ipAddr, err := SharedNodeIPAddressDAO.FindFirstNodeIPAddress(nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ipAddr) == 0 {
|
||||
issues = append(issues, &pb.DNSIssue{
|
||||
Target: node.Name,
|
||||
TargetId: nodeId,
|
||||
Type: "node",
|
||||
Description: "没有设置IP地址",
|
||||
Params: map[string]string{
|
||||
"clusterName": cluster.Name,
|
||||
"clusterId": numberutils.FormatInt64(clusterId),
|
||||
},
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO 检查是否有解析记录
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *NodeClusterDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
|
||||
@@ -604,7 +604,7 @@ func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(clusterId int64) (resul
|
||||
State(NodeStateEnabled).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("isOn", true).
|
||||
Result("id", "name", "dnsRoutes").
|
||||
Result("id", "name", "dnsRoutes", "isOn").
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
|
||||
@@ -828,26 +828,6 @@ func (this *ServerDAO) FindAllServerDNSNamesWithDNSDomainId(dnsDomainId int64) (
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 查找DNS名称为空的所有服务
|
||||
func (this *ServerDAO) FindAllServersToFixWithDNSDomainId(dnsDomainId int64) (result []*Server, err error) {
|
||||
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledClusterIdsWithDNSDomainId(dnsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(clusterIds) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
_, err = this.Query().
|
||||
State(ServerStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Attr("clusterId", clusterIds).
|
||||
Result("dnsName").
|
||||
Reuse(false). // 避免因为IN语句造成内存占用过多
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取某个集群下的服务DNS信息
|
||||
func (this *ServerDAO) FindAllServersDNSWithClusterId(clusterId int64) (result []*Server, err error) {
|
||||
_, err = this.Query().
|
||||
|
||||
Reference in New Issue
Block a user