节点IP增加是否启用、是否在线状态

This commit is contained in:
刘祥超
2021-08-18 09:24:18 +08:00
parent 0df358d70d
commit 71dbf86572
10 changed files with 151 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package models
import (
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -36,21 +37,27 @@ func init() {
}
// EnableAddress 启用条目
func (this *NodeIPAddressDAO) EnableAddress(tx *dbs.Tx, id int64) (err error) {
func (this *NodeIPAddressDAO) EnableAddress(tx *dbs.Tx, addressId int64) (err error) {
_, err = this.Query(tx).
Pk(id).
Pk(addressId).
Set("state", NodeIPAddressStateEnabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, addressId)
}
// DisableAddress 禁用IP地址
func (this *NodeIPAddressDAO) DisableAddress(tx *dbs.Tx, id int64) (err error) {
func (this *NodeIPAddressDAO) DisableAddress(tx *dbs.Tx, addressId int64) (err error) {
_, err = this.Query(tx).
Pk(id).
Pk(addressId).
Set("state", NodeIPAddressStateDisabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, addressId)
}
// DisableAllAddressesWithNodeId 禁用节点的所有的IP地址
@@ -66,7 +73,11 @@ func (this *NodeIPAddressDAO) DisableAllAddressesWithNodeId(tx *dbs.Tx, nodeId i
Attr("role", role).
Set("state", NodeIPAddressStateDisabled).
Update()
if err != nil {
return err
}
return SharedNodeDAO.NotifyDNSUpdate(tx, nodeId)
}
// FindEnabledAddress 查找启用中的IP地址
@@ -116,7 +127,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, role nodec
}
// UpdateAddress 修改IP地址
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name string, ip string, canAccess bool) (err error) {
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name string, ip string, canAccess bool, isOn bool) (err error) {
if addressId <= 0 {
return errors.New("invalid addressId")
}
@@ -126,9 +137,13 @@ func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name st
op.Name = name
op.Ip = ip
op.CanAccess = canAccess
op.IsOn = isOn
op.State = NodeIPAddressStateEnabled // 恢复状态
err = this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, addressId)
}
// UpdateAddressIP 修改IP地址中的IP
@@ -140,7 +155,11 @@ func (this *NodeIPAddressDAO) UpdateAddressIP(tx *dbs.Tx, addressId int64, ip st
op.Id = addressId
op.Ip = ip
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, addressId)
}
// UpdateAddressNodeId 修改IP地址所属节点
@@ -209,8 +228,8 @@ func (this *NodeIPAddressDAO) FindFirstNodeAccessIPAddressId(tx *dbs.Tx, nodeId
FindInt64Col(0)
}
// FindNodeAccessIPAddresses 查找节点所有的可访问的IP地址
func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole) (result []*NodeIPAddress, err error) {
// 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
}
@@ -219,9 +238,38 @@ func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64
Attr("nodeId", nodeId).
State(NodeIPAddressStateEnabled).
Attr("canAccess", true).
Attr("isOn", true).
Attr("isUp", true).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// NotifyUpdate 通知更新
func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
address, err := this.Query(tx).
Pk(addressId).
Result("nodeId", "role").
Find()
if err != nil {
return err
}
if address == nil {
return nil
}
var nodeId = int64(address.(*NodeIPAddress).NodeId)
if nodeId == 0 {
return nil
}
var role = address.(*NodeIPAddress).Role
switch role {
case nodeconfigs.NodeRoleNode:
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, nodeId, dns.DNSTaskTypeNodeChange)
}
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,28 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
type NodeIPAddressLogDAO dbs.DAO
func NewNodeIPAddressLogDAO() *NodeIPAddressLogDAO {
return dbs.NewDAO(&NodeIPAddressLogDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeIPAddressLogs",
Model: new(NodeIPAddressLog),
PkName: "id",
},
}).(*NodeIPAddressLogDAO)
}
var SharedNodeIPAddressLogDAO *NodeIPAddressLogDAO
func init() {
dbs.OnReady(func() {
SharedNodeIPAddressLogDAO = NewNodeIPAddressLogDAO()
})
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,24 @@
package models
// NodeIPAddressLog IP状态变更日志
type NodeIPAddressLog struct {
Id uint64 `field:"id"` // ID
AddressId uint64 `field:"addressId"` // 地址ID
AdminId uint32 `field:"adminId"` // 管理员ID
Description string `field:"description"` // 描述
CreatedAt uint64 `field:"createdAt"` // 操作时间
Day string `field:"day"` // YYYYMMDD用来清理
}
type NodeIPAddressLogOperator struct {
Id interface{} // ID
AddressId interface{} // 地址ID
AdminId interface{} // 管理员ID
Description interface{} // 描述
CreatedAt interface{} // 操作时间
Day interface{} // YYYYMMDD用来清理
}
func NewNodeIPAddressLogOperator() *NodeIPAddressLogOperator {
return &NodeIPAddressLogOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -11,6 +11,9 @@ type NodeIPAddress struct {
State uint8 `field:"state"` // 状态
Order uint32 `field:"order"` // 排序
CanAccess uint8 `field:"canAccess"` // 是否可以访问
IsOn uint8 `field:"isOn"` // 是否启用
IsUp uint8 `field:"isUp"` // 是否上线
Thresholds string `field:"thresholds"` // 上线阈值
}
type NodeIPAddressOperator struct {
@@ -23,6 +26,9 @@ type NodeIPAddressOperator struct {
State interface{} // 状态
Order interface{} // 排序
CanAccess interface{} // 是否可以访问
IsOn interface{} // 是否启用
IsUp interface{} // 是否上线
Thresholds interface{} // 上线阈值
}
func NewNodeIPAddressOperator() *NodeIPAddressOperator {

View File

@@ -413,7 +413,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
// 新增的节点域名
nodeKeys := []string{}
for _, node := range nodes {
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
if err != nil {
return nil, nil, nil, 0, 0, false, false, err
}

View File

@@ -1203,7 +1203,7 @@ func (this *NodeService) FindAllEnabledNodesDNSWithNodeClusterId(ctx context.Con
}
result := []*pb.NodeDNSInfo{}
for _, node := range nodes {
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
ipAddresses, err := models.SharedNodeIPAddressDAO.FindNodeAccessAndUpIPAddresses(tx, int64(node.Id), nodeconfigs.NodeRoleNode)
if err != nil {
return nil, err
}

View File

@@ -38,7 +38,7 @@ func (this *NodeIPAddressService) UpdateNodeIPAddress(ctx context.Context, req *
tx := this.NullTx()
err = models.SharedNodeIPAddressDAO.UpdateAddress(tx, req.AddressId, req.Name, req.Ip, req.CanAccess)
err = models.SharedNodeIPAddressDAO.UpdateAddress(tx, req.AddressId, req.Name, req.Ip, req.CanAccess, req.IsOn)
if err != nil {
return nil, err
}
@@ -126,6 +126,9 @@ func (this *NodeIPAddressService) FindEnabledNodeIPAddress(ctx context.Context,
State: int64(address.State),
Order: int64(address.Order),
CanAccess: address.CanAccess == 1,
IsOn: address.IsOn == 1,
IsUp: address.IsUp == 1,
ThresholdsJSON: []byte(address.Thresholds),
}
}
@@ -158,6 +161,9 @@ func (this *NodeIPAddressService) FindAllEnabledIPAddressesWithNodeId(ctx contex
State: int64(address.State),
Order: int64(address.Order),
CanAccess: address.CanAccess == 1,
IsOn: address.IsOn == 1,
IsUp: address.IsUp == 1,
ThresholdsJSON: []byte(address.Thresholds),
})
}

View File

@@ -312,7 +312,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
}
for _, ipAddress := range ipAddresses {
ip := ipAddress.Ip
if len(ip) == 0 || ipAddress.CanAccess == 0 {
if len(ip) == 0 || ipAddress.CanAccess == 0 || ipAddress.IsUp == 0 || ipAddress.IsOn == 0 {
continue
}
if net.ParseIP(ip) == nil {