From ea8fb09fab81736e188d64c421fcf89623cb06bc Mon Sep 17 00:00:00 2001 From: Max Ma Date: Thu, 3 Oct 2024 11:21:38 +0200 Subject: [PATCH 1/4] reset failover if endpoint changed --- logic/hosts.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/logic/hosts.go b/logic/hosts.go index 0fa8887e9..48482e045 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -10,6 +10,7 @@ import ( "github.com/google/uuid" "golang.org/x/crypto/bcrypt" + "golang.org/x/exp/slog" "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/logger" @@ -254,14 +255,31 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) currHost.WgPublicListenPort = newHost.WgPublicListenPort sendPeerUpdate = true } + isEndpointChanged := false if currHost.EndpointIP.String() != newHost.EndpointIP.String() { currHost.EndpointIP = newHost.EndpointIP sendPeerUpdate = true + isEndpointChanged = true } if currHost.EndpointIPv6.String() != newHost.EndpointIPv6.String() { currHost.EndpointIPv6 = newHost.EndpointIPv6 sendPeerUpdate = true + isEndpointChanged = true } + + if isEndpointChanged { + for _, nodeID := range currHost.Nodes { + node, err := GetNodeByID(nodeID) + if err != nil { + slog.Error("failed to get node:", "id", node.ID, "error", err) + continue + } + if node.FailedOverBy != uuid.Nil { + ResetFailedOverPeer(&node) + } + } + } + currHost.DaemonInstalled = newHost.DaemonInstalled currHost.Debug = newHost.Debug currHost.Verbosity = newHost.Verbosity From 5fb6034f6d2b1a42957657f283c25bf4a5984e0a Mon Sep 17 00:00:00 2001 From: Max Ma Date: Mon, 7 Oct 2024 16:39:51 +0200 Subject: [PATCH 2/4] add peerUpdate in pull when resetfailover --- controllers/hosts.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 349894331..bae8eb3e1 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -167,6 +167,7 @@ func pull(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return } + for _, nodeID := range host.Nodes { node, err := logic.GetNodeByID(nodeID) if err != nil { @@ -174,7 +175,12 @@ func pull(w http.ResponseWriter, r *http.Request) { continue } if node.FailedOverBy != uuid.Nil { - go logic.ResetFailedOverPeer(&node) + logic.ResetFailedOverPeer(&node) + go func() { + if err := mq.PublishPeerUpdate(false); err != nil { + logger.Log(0, "fail to publish peer update: ", err.Error()) + } + }() } } allNodes, err := logic.GetAllNodes() From 385f1573548444d85d0546c2edcb9b0703dc08a0 Mon Sep 17 00:00:00 2001 From: Max Ma Date: Tue, 8 Oct 2024 10:37:32 +0200 Subject: [PATCH 3/4] remove go routine for peerUpdate in pull --- controllers/hosts.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index bae8eb3e1..d1b22facb 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -176,11 +176,9 @@ func pull(w http.ResponseWriter, r *http.Request) { } if node.FailedOverBy != uuid.Nil { logic.ResetFailedOverPeer(&node) - go func() { - if err := mq.PublishPeerUpdate(false); err != nil { - logger.Log(0, "fail to publish peer update: ", err.Error()) - } - }() + if err := mq.PublishPeerUpdate(true); err != nil { + logger.Log(0, "fail to publish peer update: ", err.Error()) + } } } allNodes, err := logic.GetAllNodes() From c8b2eeb873da655e129742d078abf99681e7cdf6 Mon Sep 17 00:00:00 2001 From: Max Ma Date: Tue, 8 Oct 2024 12:17:17 +0200 Subject: [PATCH 4/4] update peerUpdate in pull --- controllers/hosts.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index d1b22facb..dbfbc6d51 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -168,6 +168,7 @@ func pull(w http.ResponseWriter, r *http.Request) { return } + sendPeerUpdate := false for _, nodeID := range host.Nodes { node, err := logic.GetNodeByID(nodeID) if err != nil { @@ -176,9 +177,12 @@ func pull(w http.ResponseWriter, r *http.Request) { } if node.FailedOverBy != uuid.Nil { logic.ResetFailedOverPeer(&node) - if err := mq.PublishPeerUpdate(true); err != nil { - logger.Log(0, "fail to publish peer update: ", err.Error()) - } + sendPeerUpdate = true + } + } + if sendPeerUpdate { + if err := mq.PublishPeerUpdate(true); err != nil { + logger.Log(0, "fail to publish peer update: ", err.Error()) } } allNodes, err := logic.GetAllNodes()