mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-20 23:51:26 +08:00
updates to zombie processing
This commit is contained in:
@@ -96,6 +96,7 @@ func CreateHost(h *models.Host) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h.HostPass = string(hash)
|
h.HostPass = string(hash)
|
||||||
|
checkForZombieHosts(h)
|
||||||
return UpsertHost(h)
|
return UpsertHost(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -91,7 +91,7 @@ func DeleteNode(node *models.Node, purge bool) error {
|
|||||||
if err := UpdateNode(node, &newnode); err != nil {
|
if err := UpdateNode(node, &newnode); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
newZombie <- node.ID
|
zombies = append(zombies, node.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
host, err := GetHost(node.HostID.String())
|
host, err := GetHost(node.HostID.String())
|
||||||
@@ -534,7 +534,7 @@ func createNode(node *models.Node) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
CheckZombies(node, host.MacAddress)
|
CheckZombies(node)
|
||||||
|
|
||||||
nodebytes, err := json.Marshal(&node)
|
nodebytes, err := json.Marshal(&node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -2,7 +2,6 @@ package logic
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -19,14 +18,13 @@ const (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
zombies []uuid.UUID
|
zombies []uuid.UUID
|
||||||
removeZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
hostZombies []uuid.UUID
|
||||||
newZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckZombies - checks if new node has same macaddress as existing node
|
// CheckZombies - checks if new node has same hostid as existing node
|
||||||
// if so, existing node is added to zombie node quarantine list
|
// if so, existing node is added to zombie node quarantine list
|
||||||
// also cleans up nodes past their expiration date
|
// also cleans up nodes past their expiration date
|
||||||
func CheckZombies(newnode *models.Node, mac net.HardwareAddr) {
|
func CheckZombies(newnode *models.Node) {
|
||||||
nodes, err := GetNetworkNodes(newnode.Network)
|
nodes, err := GetNetworkNodes(newnode.Network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log(1, "Failed to retrieve network nodes", newnode.Network, err.Error())
|
logger.Log(1, "Failed to retrieve network nodes", newnode.Network, err.Error())
|
||||||
@@ -39,7 +37,36 @@ func CheckZombies(newnode *models.Node, mac net.HardwareAddr) {
|
|||||||
}
|
}
|
||||||
if node.HostID == newnode.HostID || time.Now().After(node.ExpirationDateTime) {
|
if node.HostID == newnode.HostID || time.Now().After(node.ExpirationDateTime) {
|
||||||
logger.Log(0, "adding ", node.ID.String(), " to zombie list")
|
logger.Log(0, "adding ", node.ID.String(), " to zombie list")
|
||||||
newZombie <- node.ID
|
zombies = append(zombies, node.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkForZombieHosts - checks if new host has the same macAddress as an existing host
|
||||||
|
// if true, existing host is added to host zombie collection
|
||||||
|
func checkForZombieHosts(h *models.Host) {
|
||||||
|
hosts, err := GetAllHosts()
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(3, "errror retrieving all hosts", err.Error())
|
||||||
|
}
|
||||||
|
for _, existing := range hosts {
|
||||||
|
if existing.ID == h.ID {
|
||||||
|
//probably an unnecessary check as new host should not be in database yet, but just in case
|
||||||
|
//skip self
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if existing.MacAddress.String() == h.MacAddress.String() {
|
||||||
|
//add to hostZombies
|
||||||
|
hostZombies = append(hostZombies, existing.ID)
|
||||||
|
//add all nodes belonging to host to zombile list
|
||||||
|
for _, node := range existing.Nodes {
|
||||||
|
id, err := uuid.Parse(node)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(3, "error parsing uuid from host.Nodes", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
zombies = append(zombies, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,23 +79,6 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case id := <-newZombie:
|
|
||||||
logger.Log(1, "adding", id.String(), "to zombie quaratine list")
|
|
||||||
zombies = append(zombies, id)
|
|
||||||
case id := <-removeZombie:
|
|
||||||
found := false
|
|
||||||
if len(zombies) > 0 {
|
|
||||||
for i := len(zombies) - 1; i >= 0; i-- {
|
|
||||||
if zombies[i] == id {
|
|
||||||
logger.Log(1, "removing zombie from quaratine list", zombies[i].String())
|
|
||||||
zombies = append(zombies[:i], zombies[i+1:]...)
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
logger.Log(3, "no zombies found")
|
|
||||||
}
|
|
||||||
case <-time.After(time.Second * ZOMBIE_TIMEOUT):
|
case <-time.After(time.Second * ZOMBIE_TIMEOUT):
|
||||||
logger.Log(3, "checking for zombie nodes")
|
logger.Log(3, "checking for zombie nodes")
|
||||||
if len(zombies) > 0 {
|
if len(zombies) > 0 {
|
||||||
@@ -92,6 +102,23 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(hostZombies) > 0 {
|
||||||
|
logger.Log(3, "checking host zombies")
|
||||||
|
for i := len(hostZombies) - 1; i >= 0; i-- {
|
||||||
|
host, err := GetHost(hostZombies[i].String())
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(1, "error retrieving zombie host", err.Error())
|
||||||
|
logger.Log(1, "deleting ", host.ID.String(), " from zombie list")
|
||||||
|
zombies = append(zombies[:i], zombies[i+1:]...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(host.Nodes) == 0 {
|
||||||
|
if err := RemoveHost(host); err != nil {
|
||||||
|
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user