From 287c9f496c2d81b5a98505bc5f0bee45b2da3d9c Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Mon, 10 Jul 2023 14:18:50 +0530 Subject: [PATCH 1/4] send delete peer update always --- controllers/node.go | 11 +++-------- logic/peers.go | 13 ++++++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/controllers/node.go b/controllers/node.go index 667fbb519..5b893313b 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -770,17 +770,12 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { if !fromNode { // notify node change runUpdates(&node, false) } - go func(deletedNode *models.Node, fromNode bool) { // notify of peer change + go func(deletedNode *models.Node) { // notify of peer change var err error - if fromNode { - err = mq.PublishDeletedNodePeerUpdate(deletedNode) - } else { - err = mq.PublishPeerUpdate() - } + err = mq.PublishDeletedNodePeerUpdate(deletedNode) if err != nil { logger.Log(1, "error publishing peer update ", err.Error()) } - host, err := logic.GetHost(node.HostID.String()) if err != nil { logger.Log(1, "failed to retrieve host for node", node.ID.String(), err.Error()) @@ -788,7 +783,7 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { if err := mq.PublishDNSDelete(&node, host); err != nil { logger.Log(1, "error publishing dns update", err.Error()) } - }(&node, fromNode) + }(&node) } func runUpdates(node *models.Node, ifaceDelta bool) { diff --git a/logic/peers.go b/logic/peers.go index 909a2e32d..6b251922f 100644 --- a/logic/peers.go +++ b/logic/peers.go @@ -312,7 +312,6 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N } hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, nodePeer) } - //} } var extPeers []wgtypes.PeerConfig var extPeerIDAndAddrs []models.IDandAddr @@ -387,6 +386,18 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N } hostPeerUpdate.Peers[i] = peer } + if deletedNode != nil { + peerHost, err := GetHost(deletedNode.HostID.String()) + if err == nil && host.ID != peerHost.ID { + if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok { + hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{ + PublicKey: peerHost.PublicKey, + Remove: true, + }) + } + } + + } for i := range hostPeerUpdate.NodePeers { peer := hostPeerUpdate.NodePeers[i] From 3f11ddd8c57b09fe75abb3f012c9f6bba739089d Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Mon, 10 Jul 2023 16:05:36 +0530 Subject: [PATCH 2/4] fix add/remove host api calls --- controllers/hosts.go | 35 ++++++++++++++++++++++++----------- controllers/node.go | 7 +++---- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 92f0fd897..06e7b975f 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -9,7 +9,6 @@ import ( "github.com/gorilla/mux" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logic" - "github.com/gravitl/netmaker/logic/hostactions" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/mq" "github.com/gravitl/netmaker/servercfg" @@ -259,18 +258,13 @@ func addHostToNetwork(w http.ResponseWriter, r *http.Request) { return } logger.Log(1, "added new node", newNode.ID.String(), "to host", currHost.Name) - hostactions.AddAction(models.HostUpdate{ + + mq.HostUpdate(&models.HostUpdate{ Action: models.JoinHostToNetwork, Host: *currHost, Node: *newNode, }) - if servercfg.IsMessageQueueBackend() { - mq.HostUpdate(&models.HostUpdate{ - Action: models.RequestAck, - Host: *currHost, - }) - } - + go mq.PublishPeerUpdate() logger.Log(2, r.Header.Get("user"), fmt.Sprintf("added host %s to network %s", currHost.Name, network)) w.WriteHeader(http.StatusOK) } @@ -309,6 +303,25 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } + if node.IsRelayed { + // cleanup node from relayednodes on relay node + relayNode, err := logic.GetNodeByID(node.RelayedBy) + if err == nil { + relayedNodes := []string{} + for _, relayedNodeID := range relayNode.RelayedNodes { + if relayedNodeID == node.ID.String() { + continue + } + relayedNodes = append(relayedNodes, relayedNodeID) + } + relayNode.RelayedNodes = relayedNodes + logic.UpsertNode(&relayNode) + } + } + if node.IsRelay { + // unset all the relayed nodes + logic.SetRelayedNodes(false, node.ID.String(), node.RelayedNodes) + } node.Action = models.NODE_DELETE node.PendingDelete = true logger.Log(1, "deleting node", node.ID.String(), "from host", currHost.Name) @@ -317,10 +330,10 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) { return } // notify node change - runUpdates(node, false) go func() { // notify of peer change - if err := mq.PublishPeerUpdate(); err != nil { + err = mq.PublishDeletedNodePeerUpdate(node) + if err != nil { logger.Log(1, "error publishing peer update ", err.Error()) } if err := mq.PublishDNSDelete(node, currHost); err != nil { diff --git a/controllers/node.go b/controllers/node.go index 5b893313b..7be7c2fac 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -754,7 +754,6 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { relayNode.RelayedNodes = relayedNodes logic.UpsertNode(&relayNode) } - } if node.IsRelay { // unset all the relayed nodes @@ -770,9 +769,9 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { if !fromNode { // notify node change runUpdates(&node, false) } - go func(deletedNode *models.Node) { // notify of peer change + go func() { // notify of peer change var err error - err = mq.PublishDeletedNodePeerUpdate(deletedNode) + err = mq.PublishDeletedNodePeerUpdate(&node) if err != nil { logger.Log(1, "error publishing peer update ", err.Error()) } @@ -783,7 +782,7 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { if err := mq.PublishDNSDelete(&node, host); err != nil { logger.Log(1, "error publishing dns update", err.Error()) } - }(&node) + }() } func runUpdates(node *models.Node, ifaceDelta bool) { From 504ce7feb99d387f582ee62218d3f222d18bc3d4 Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Mon, 10 Jul 2023 16:56:03 +0530 Subject: [PATCH 3/4] keep mq updates in a single go func --- controllers/hosts.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 06e7b975f..fa27cfdfa 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -258,13 +258,14 @@ func addHostToNetwork(w http.ResponseWriter, r *http.Request) { return } logger.Log(1, "added new node", newNode.ID.String(), "to host", currHost.Name) - - mq.HostUpdate(&models.HostUpdate{ - Action: models.JoinHostToNetwork, - Host: *currHost, - Node: *newNode, - }) - go mq.PublishPeerUpdate() + go func() { + mq.HostUpdate(&models.HostUpdate{ + Action: models.JoinHostToNetwork, + Host: *currHost, + Node: *newNode, + }) + mq.PublishPeerUpdate() + }() logger.Log(2, r.Header.Get("user"), fmt.Sprintf("added host %s to network %s", currHost.Name, network)) w.WriteHeader(http.StatusOK) } From fc3261c90ad378f04673a9a4d648f1f63f88514a Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Tue, 11 Jul 2023 19:08:29 +0530 Subject: [PATCH 4/4] handle IOT OS --- logic/peers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logic/peers.go b/logic/peers.go index 6b251922f..a90f7e739 100644 --- a/logic/peers.go +++ b/logic/peers.go @@ -386,7 +386,7 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N } hostPeerUpdate.Peers[i] = peer } - if deletedNode != nil { + if deletedNode != nil && host.OS != models.OS_Types.IoT { peerHost, err := GetHost(deletedNode.HostID.String()) if err == nil && host.ID != peerHost.ID { if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {