节点IP地址可以设置专属集群

This commit is contained in:
GoEdgeLab
2023-03-01 11:38:53 +08:00
parent 074d668644
commit cb6919a073
8 changed files with 200 additions and 72 deletions

View File

@@ -113,7 +113,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bo
// TODO 检查节点数量不能为0
for _, node := range nodes {
nodeId := int64(node.Id)
var nodeId = int64(node.Id)
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
if err != nil {

View File

@@ -121,7 +121,7 @@ func (this *NodeIPAddressDAO) FindAddressIsHealthy(tx *dbs.Tx, addressId int64)
}
// CreateAddress 创建IP地址
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool, groupId int64) (addressId int64, err error) {
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool, groupId int64, clusterIds []int64) (addressId int64, err error) {
if len(role) == 0 {
role = nodeconfigs.NodeRoleNode
}
@@ -135,6 +135,17 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId in
op.IsUp = isUp
op.GroupId = groupId
// 集群
if len(clusterIds) == 0 {
op.ClusterIds = "[]"
} else {
clusterIdsJSON, err := json.Marshal(clusterIds)
if err != nil {
return 0, err
}
op.ClusterIds = clusterIdsJSON
}
op.State = NodeIPAddressStateEnabled
addressId, err = this.SaveInt64(tx, op)
if err != nil {
@@ -156,7 +167,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId in
}
// UpdateAddress 修改IP地址
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, adminId int64, addressId int64, name string, ip string, canAccess bool, isOn bool, isUp bool) (err error) {
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, adminId int64, addressId int64, name string, ip string, canAccess bool, isOn bool, isUp bool, clusterIds []int64) (err error) {
if addressId <= 0 {
return errors.New("invalid addressId")
}
@@ -169,6 +180,17 @@ func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, adminId int64, addressId
op.IsOn = isOn
op.IsUp = isUp
// 集群
if len(clusterIds) == 0 {
op.ClusterIds = "[]"
} else {
clusterIdsJSON, err := json.Marshal(clusterIds)
if err != nil {
return err
}
op.ClusterIds = clusterIdsJSON
}
op.State = NodeIPAddressStateEnabled // 恢复状态
err = this.Save(tx, op)
if err != nil {

View File

@@ -6,6 +6,7 @@ import "github.com/iwind/TeaGo/dbs"
type NodeIPAddress struct {
Id uint32 `field:"id"` // ID
NodeId uint32 `field:"nodeId"` // 节点ID
ClusterIds dbs.JSON `field:"clusterIds"` // 所属集群IDs
Role string `field:"role"` // 节点角色
GroupId uint32 `field:"groupId"` // 所属分组ID
Name string `field:"name"` // 名称
@@ -26,25 +27,26 @@ type NodeIPAddress struct {
}
type NodeIPAddressOperator struct {
Id interface{} // ID
NodeId interface{} // 节点ID
Role interface{} // 节点角色
GroupId interface{} // 所属分组ID
Name interface{} // 名称
Ip interface{} // IP地址
Description interface{} // 描述
State interface{} // 状态
Order interface{} // 排序
CanAccess interface{} // 是否可以访问
IsOn interface{} // 是否启用
IsUp interface{} // 是否上线
IsHealthy interface{} // 是否健康
Thresholds interface{} // 上线阈值
Connectivity interface{} // 连通性状态
BackupIP interface{} // 备用IP
BackupThresholdId interface{} // 触发备用IP的阈值
CountUp interface{} // UP状态次数
CountDown interface{} // DOWN状态次数
Id any // ID
NodeId any // 节点ID
ClusterIds any // 所属集群IDs
Role any // 节点角色
GroupId any // 所属分组ID
Name any // 名称
Ip any // IP地址
Description any // 描述
State any // 状态
Order any // 排序
CanAccess any // 是否可以访问
IsOn any // 是否启用
IsUp any // 是否上线
IsHealthy any // 是否健康
Thresholds any // 上线阈值
Connectivity any // 连通性状态
BackupIP any // 备用IP
BackupThresholdId any // 触发备用IP的阈值
CountUp any // UP状态次数
CountDown any // DOWN状态次数
}
func NewNodeIPAddressOperator() *NodeIPAddressOperator {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/lists"
)
// DecodeConnectivity 解析联通数值
@@ -12,7 +13,7 @@ func (this *NodeIPAddress) DecodeConnectivity() *nodeconfigs.Connectivity {
if len(this.Connectivity) > 0 {
err := json.Unmarshal(this.Connectivity, connectivity)
if err != nil {
remotelogs.Error("NodeIPAddress.DecodeConnectivity", "decode failed: "+err.Error())
remotelogs.Error("NodeIPAddress", "DecodeConnectivity(): decode failed: "+err.Error())
}
}
return connectivity
@@ -33,7 +34,7 @@ func (this *NodeIPAddress) DecodeBackupIP() string {
// 阈值是否存在
b, err := SharedNodeIPAddressThresholdDAO.ExistsEnabledThreshold(nil, int64(this.BackupThresholdId))
if err != nil {
remotelogs.Error("NodeIPAddress.DNSIP", "check enabled threshold failed: "+err.Error())
remotelogs.Error("NodeIPAddress", "DecodeBackupIP(): check enabled threshold failed: "+err.Error())
} else {
if b {
return this.BackupIP
@@ -42,3 +43,26 @@ func (this *NodeIPAddress) DecodeBackupIP() string {
}
return ""
}
// DecodeClusterIds 解析集群ID
func (this *NodeIPAddress) DecodeClusterIds() []int64 {
if IsNull(this.ClusterIds) {
return nil
}
var clusterIds = []int64{}
err := json.Unmarshal(this.ClusterIds, &clusterIds)
if err != nil {
remotelogs.Error("NodeIPAddress", "DecodeClusterIds(): "+err.Error())
}
return clusterIds
}
// IsValidInCluster 检查在某个集群中是否有效
func (this *NodeIPAddress) IsValidInCluster(clusterId int64) bool {
var clusterIds = this.DecodeClusterIds()
if len(clusterIds) == 0 {
return true
}
return lists.ContainsInt64(clusterIds, clusterId)
}