From f17de1016a8cf0056bbf89399ba4cda3647c7235 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 26 Apr 2023 10:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E7=BE=A4=E5=81=A5=E5=BA=B7=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E5=8F=AF=E4=BB=A5=E5=90=8C=E6=97=B6=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=8D=95=E8=8A=82=E7=82=B9=E7=9A=84=E5=A4=9A=E4=B8=AAIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/dns/dnsutils/dns_utils.go | 41 ++++++++++++++------ internal/db/models/node_ip_address_dao.go | 20 +++++++++- internal/tasks/health_check_executor.go | 28 +++++++------ internal/tasks/health_check_executor_test.go | 2 +- internal/tasks/health_check_result.go | 5 ++- 5 files changed, 70 insertions(+), 26 deletions(-) diff --git a/internal/db/models/dns/dnsutils/dns_utils.go b/internal/db/models/dns/dnsutils/dns_utils.go index 87d86808..4367068e 100644 --- a/internal/db/models/dns/dnsutils/dns_utils.go +++ b/internal/db/models/dns/dnsutils/dns_utils.go @@ -162,17 +162,36 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bo 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), - }, - MustFix: true, - }) + // 检查是否有离线 + anyIPAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, false, nodeconfigs.NodeRoleNode) + if err != nil { + return nil, err + } + if len(anyIPAddr) > 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), + }, + MustFix: true, + }) + } else { + 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), + }, + MustFix: true, + }) + } continue } diff --git a/internal/db/models/node_ip_address_dao.go b/internal/db/models/node_ip_address_dao.go index dd6944f6..a7c3436a 100644 --- a/internal/db/models/node_ip_address_dao.go +++ b/internal/db/models/node_ip_address_dao.go @@ -307,7 +307,7 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddressId(tx *dbs.Tx, nodeId FindInt64Col(0) } -// FindNodeAccessAndUpIPAddresses 查找节点所有的可访问的IP地址 +// FindNodeAccessAndUpIPAddresses 查找节点所有的可访问且在线的IP地址 func (this *NodeIPAddressDAO) FindNodeAccessAndUpIPAddresses(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (result []*NodeIPAddress, err error) { if len(role) == 0 { role = nodeconfigs.NodeRoleNode @@ -326,6 +326,24 @@ func (this *NodeIPAddressDAO) FindNodeAccessAndUpIPAddresses(tx *dbs.Tx, nodeId return } +// FindNodeAccessIPAddresses 查找节点所有的可访问的IP地址,包括在线和离线 +func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (result []*NodeIPAddress, err error) { + if len(role) == 0 { + role = nodeconfigs.NodeRoleNode + } + _, err = this.Query(tx). + Attr("role", role). + Attr("nodeId", nodeId). + State(NodeIPAddressStateEnabled). + Attr("canAccess", true). + Attr("isOn", true). + Desc("order"). + AscPk(). + Slice(&result). + FindAll() + return +} + // CountAllEnabledIPAddresses 计算IP地址数量 // TODO 目前支持边缘节点,将来支持NS节点 func (this *NodeIPAddressDAO) CountAllEnabledIPAddresses(tx *dbs.Tx, role string, nodeClusterId int64, upState configutils.BoolState, keyword string) (int64, error) { diff --git a/internal/tasks/health_check_executor.go b/internal/tasks/health_check_executor.go index 045e4434..0ef4213b 100644 --- a/internal/tasks/health_check_executor.go +++ b/internal/tasks/health_check_executor.go @@ -12,6 +12,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeutils" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" @@ -63,26 +64,31 @@ func (this *HealthCheckExecutor) Run() ([]*HealthCheckResult, error) { return results, nil } + var tx *dbs.Tx for _, node := range nodes { if !node.IsOn { continue } - result := &HealthCheckResult{ - Node: node, - } - ipAddr, ipAddrId, err := models.NewNodeIPAddressDAO().FindFirstNodeAccessIPAddress(nil, int64(node.Id), false, nodeconfigs.NodeRoleNode) + ipAddrs, err := models.SharedNodeIPAddressDAO.FindNodeAccessIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode) if err != nil { return nil, err } - if len(ipAddr) == 0 { - result.Error = "no ip address can be used" - } else { - result.NodeAddr = ipAddr - result.NodeAddrId = ipAddrId - } + for _, ipAddr := range ipAddrs { + var ipClusterIds = ipAddr.DecodeClusterIds() + if len(ipClusterIds) > 0 && !lists.ContainsInt64(ipClusterIds, this.clusterId) { + continue + } - results = append(results, result) + // TODO 支持备用IP + var result = &HealthCheckResult{ + Node: node, + NodeAddrId: int64(ipAddr.Id), + NodeAddr: ipAddr.Ip, + } + + results = append(results, result) + } } // 并行检查 diff --git a/internal/tasks/health_check_executor_test.go b/internal/tasks/health_check_executor_test.go index f3df8775..947b5299 100644 --- a/internal/tasks/health_check_executor_test.go +++ b/internal/tasks/health_check_executor_test.go @@ -14,7 +14,7 @@ func TestHealthCheckExecutor_Run(t *testing.T) { teaconst.IsPlus = true dbs.NotifyReady() - executor := tasks.NewHealthCheckExecutor(35) + var executor = tasks.NewHealthCheckExecutor(42) results, err := executor.Run() if err != nil { t.Fatal(err) diff --git a/internal/tasks/health_check_result.go b/internal/tasks/health_check_result.go index db9ea643..9eebfb42 100644 --- a/internal/tasks/health_check_result.go +++ b/internal/tasks/health_check_result.go @@ -2,10 +2,11 @@ package tasks import "github.com/TeaOSLab/EdgeAPI/internal/db/models" +// HealthCheckResult 健康检查结果 type HealthCheckResult struct { Node *models.Node - NodeAddr string - NodeAddrId int64 + NodeAddr string // 节点IP地址 + NodeAddrId int64 // 节点IP地址ID IsOk bool Error string CostMs float64