Skip to content

Commit

Permalink
merge conflicts resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek9686 committed Sep 30, 2022
2 parents f0a3e2c + 514b69b commit 1827f8c
Show file tree
Hide file tree
Showing 15 changed files with 440 additions and 113 deletions.
46 changes: 44 additions & 2 deletions controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func authorize(nodesAllowed, networkCheck bool, authNetwork string, next http.Ha
if nodesAllowed {
// TODO --- should ensure that node is only operating on itself
if _, _, _, err := logic.VerifyToken(authToken); err == nil {

// this indicates request is from a node
// used for failover - if a getNode comes from node, this will trigger a metrics wipe
r.Header.Set("requestfrom", "node")
next.ServeHTTP(w, r)
return
}
Expand Down Expand Up @@ -452,6 +456,8 @@ func getNode(w http.ResponseWriter, r *http.Request) {
// set header.
w.Header().Set("Content-Type", "application/json")

nodeRequest := r.Header.Get("requestfrom") == "node"

var params = mux.Vars(r)
nodeid := params["nodeid"]
node, err := logic.GetNodeByID(nodeid)
Expand Down Expand Up @@ -481,6 +487,12 @@ func getNode(w http.ResponseWriter, r *http.Request) {
PeerIDs: peerUpdate.PeerIDs,
}

if servercfg.Is_EE && nodeRequest {
if err = logic.EnterpriseResetAllPeersFailovers(node.ID, node.Network); err != nil {
logger.Log(1, "failed to reset failover list during node config pull", node.Name, node.Network)
}
}

logger.Log(2, r.Header.Get("user"), "fetched node", params["nodeid"])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
Expand Down Expand Up @@ -841,7 +853,13 @@ func createIngressGateway(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
nodeid := params["nodeid"]
netid := params["network"]
node, err := logic.CreateIngressGateway(netid, nodeid)
type failoverData struct {
Failover bool `json:"failover"`
}
var failoverReqBody failoverData
json.NewDecoder(r.Body).Decode(&failoverReqBody)

node, err := logic.CreateIngressGateway(netid, nodeid, failoverReqBody.Failover)
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("failed to create ingress gateway on node [%s] on network [%s]: %v",
Expand All @@ -850,6 +868,12 @@ func createIngressGateway(w http.ResponseWriter, r *http.Request) {
return
}

if servercfg.Is_EE && failoverReqBody.Failover {
if err = logic.EnterpriseResetFailoverFunc(node.Network); err != nil {
logger.Log(1, "failed to reset failover list during failover create", node.Name, node.Network)
}
}

logger.Log(1, r.Header.Get("user"), "created ingress gateway on node", nodeid, "on network", netid)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
Expand All @@ -873,7 +897,7 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
var params = mux.Vars(r)
nodeid := params["nodeid"]
netid := params["network"]
node, err := logic.DeleteIngressGateway(netid, nodeid)
node, wasFailover, err := logic.DeleteIngressGateway(netid, nodeid)
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("failed to delete ingress gateway on node [%s] on network [%s]: %v",
Expand All @@ -882,6 +906,12 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
return
}

if servercfg.Is_EE && wasFailover {
if err = logic.EnterpriseResetFailoverFunc(node.Network); err != nil {
logger.Log(1, "failed to reset failover list during failover create", node.Name, node.Network)
}
}

logger.Log(1, r.Header.Get("user"), "deleted ingress gateway", nodeid)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
Expand Down Expand Up @@ -965,6 +995,12 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
}
}

if ifaceDelta && servercfg.Is_EE {
if err = logic.EnterpriseResetAllPeersFailovers(node.ID, node.Network); err != nil {
logger.Log(0, "failed to reset failover lists during node update for node", node.Name, node.Network)
}
}

err = logic.UpdateNode(&node, &newNode)
if err != nil {
logger.Log(0, r.Header.Get("user"),
Expand Down Expand Up @@ -1059,6 +1095,12 @@ func deleteNode(w http.ResponseWriter, r *http.Request) {
logger.Log(0, fmt.Sprintf("failed to send DynSec command [%v]: %v",
event.Commands, err.Error()))
}
if servercfg.Is_EE {
if err = logic.EnterpriseResetAllPeersFailovers(node.ID, node.Network); err != nil {
logger.Log(0, "failed to reset failover lists during node delete for node", node.Name, node.Network)
}
}

logic.ReturnSuccessResponse(w, r, nodeid+" deleted.")
logger.Log(1, r.Header.Get("user"), "Deleted node", nodeid, "from network", params["network"])
runUpdates(&node, false)
Expand Down
17 changes: 17 additions & 0 deletions ee/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ee
import (
controller "github.com/gravitl/netmaker/controllers"
"github.com/gravitl/netmaker/ee/ee_controllers"
eelogic "github.com/gravitl/netmaker/ee/logic"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
Expand All @@ -27,7 +28,11 @@ func InitEE() {
}
// == End License Handling ==
AddLicenseHooks()
resetFailover()
})
logic.EnterpriseFailoverFunc = eelogic.SetFailover
logic.EnterpriseResetFailoverFunc = eelogic.ResetFailover
logic.EnterpriseResetAllPeersFailovers = eelogic.WipeAffectedFailoversOnly
}

func setControllerLimits() {
Expand All @@ -38,6 +43,18 @@ func setControllerLimits() {
servercfg.Is_EE = true
}

func resetFailover() {
nets, err := logic.GetNetworks()
if err == nil {
for _, net := range nets {
err = eelogic.ResetFailover(net.NetID)
if err != nil {
logger.Log(0, "failed to reset failover on network", net.NetID, ":", err.Error())
}
}
}
}

func retrieveEELogo() string {
return `
__ __ ______ ______ __ __ ______ __ __ ______ ______
Expand Down
121 changes: 121 additions & 0 deletions ee/logic/failover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package logic

import (
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
)

// SetFailover - finds a suitable failover candidate and sets it
func SetFailover(node *models.Node) error {
failoverNode := determineFailoverCandidate(node)
if failoverNode != nil {
return setFailoverNode(failoverNode, node)
}
return nil
}

// ResetFailover - sets the failover node and wipes disconnected status
func ResetFailover(network string) error {
nodes, err := logic.GetNetworkNodes(network)
if err != nil {
return err
}
for _, node := range nodes {
err = SetFailover(&node)
if err != nil {
logger.Log(2, "error setting failover for node", node.Name, ":", err.Error())
}
err = WipeFailover(node.ID)
if err != nil {
logger.Log(2, "error wiping failover for node", node.Name, ":", err.Error())
}
}
return nil
}

// determineFailoverCandidate - returns a list of nodes that
// are suitable for relaying a given node
func determineFailoverCandidate(nodeToBeRelayed *models.Node) *models.Node {

currentNetworkNodes, err := logic.GetNetworkNodes(nodeToBeRelayed.Network)
if err != nil {
return nil
}

currentMetrics, err := logic.GetMetrics(nodeToBeRelayed.ID)
if err != nil || currentMetrics == nil || currentMetrics.Connectivity == nil {
return nil
}

minLatency := int64(9223372036854775807) // max signed int64 value
var fastestCandidate *models.Node
for i := range currentNetworkNodes {
if currentNetworkNodes[i].ID == nodeToBeRelayed.ID {
continue
}

if currentMetrics.Connectivity[currentNetworkNodes[i].ID].Connected && (currentNetworkNodes[i].Failover == "yes") {
if currentMetrics.Connectivity[currentNetworkNodes[i].ID].Latency < int64(minLatency) {
fastestCandidate = &currentNetworkNodes[i]
minLatency = currentMetrics.Connectivity[currentNetworkNodes[i].ID].Latency
}
}
}

return fastestCandidate
}

// setFailoverNode - changes node's failover node
func setFailoverNode(failoverNode, node *models.Node) error {

node.FailoverNode = failoverNode.ID
nodeToUpdate, err := logic.GetNodeByID(node.ID)
if err != nil {
return err
}
if nodeToUpdate.FailoverNode == failoverNode.ID {
return nil
}
return logic.UpdateNode(&nodeToUpdate, node)
}

// WipeFailover - removes the failover peers of given node (ID)
func WipeFailover(nodeid string) error {
metrics, err := logic.GetMetrics(nodeid)
if err != nil {
return err
}
if metrics != nil {
metrics.FailoverPeers = make(map[string]string)
return logic.UpdateMetrics(nodeid, metrics)
}
return nil
}

// WipeAffectedFailoversOnly - wipes failovers for nodes that have given node (ID)
// in their respective failover lists
func WipeAffectedFailoversOnly(nodeid, network string) error {
currentNetworkNodes, err := logic.GetNetworkNodes(network)
if err != nil {
return nil
}
WipeFailover(nodeid)

for i := range currentNetworkNodes {
currNodeID := currentNetworkNodes[i].ID
if currNodeID == nodeid {
continue
}
currMetrics, err := logic.GetMetrics(currNodeID)
if err != nil || currMetrics == nil {
continue
}
if currMetrics.FailoverPeers != nil {
if len(currMetrics.FailoverPeers[nodeid]) > 0 {
WipeFailover(currNodeID)
}
}
}
return nil
}
24 changes: 14 additions & 10 deletions logic/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
)

// CreateEgressGateway - creates an egress gateway
Expand Down Expand Up @@ -172,7 +173,7 @@ func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
}

// CreateIngressGateway - creates an ingress gateway
func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
func CreateIngressGateway(netid string, nodeid string, failover bool) (models.Node, error) {

var postUpCmd, postDownCmd string
node, err := GetNodeByID(nodeid)
Expand Down Expand Up @@ -224,7 +225,9 @@ func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
node.PostUp = postUpCmd
node.PostDown = postDownCmd
node.UDPHolePunch = "no"

if failover && servercfg.Is_EE {
node.Failover = "yes"
}
data, err := json.Marshal(&node)
if err != nil {
return models.Node{}, err
Expand All @@ -238,26 +241,27 @@ func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
}

// DeleteIngressGateway - deletes an ingress gateway
func DeleteIngressGateway(networkName string, nodeid string) (models.Node, error) {
func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, error) {

node, err := GetNodeByID(nodeid)
if err != nil {
return models.Node{}, err
return models.Node{}, false, err
}
network, err := GetParentNetwork(networkName)
if err != nil {
return models.Node{}, err
return models.Node{}, false, err
}
// delete ext clients belonging to ingress gateway
if err = DeleteGatewayExtClients(node.ID, networkName); err != nil {
return models.Node{}, err
return models.Node{}, false, err
}
logger.Log(3, "deleting ingress gateway")

wasFailover := node.Failover == "yes"
node.UDPHolePunch = network.DefaultUDPHolePunch
node.LastModified = time.Now().Unix()
node.IsIngressGateway = "no"
node.IngressGatewayRange = ""
node.Failover = "no"

// default to removing postup and postdown
node.PostUp = ""
Expand All @@ -274,14 +278,14 @@ func DeleteIngressGateway(networkName string, nodeid string) (models.Node, error

data, err := json.Marshal(&node)
if err != nil {
return models.Node{}, err
return models.Node{}, false, err
}
err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
if err != nil {
return models.Node{}, err
return models.Node{}, wasFailover, err
}
err = SetNetworkNodesLastModified(networkName)
return node, err
return node, wasFailover, err
}

// DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
Expand Down
17 changes: 15 additions & 2 deletions logic/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,24 @@ func Collect(iface string, peerMap models.PeerMap) (*models.Metrics, error) {
newMetric.Latency = 999
} else {
pingStats := pinger.Statistics()
newMetric.Uptime = 1
if pingStats.PacketsRecv > 0 {
newMetric.Uptime = 1
newMetric.Connected = true
newMetric.Latency = pingStats.AvgRtt.Milliseconds()
}
}
}

// check device peer to see if WG is working if ping failed
if !newMetric.Connected {
if currPeer.ReceiveBytes > 0 &&
currPeer.TransmitBytes > 0 &&
time.Now().Before(currPeer.LastHandshakeTime.Add(time.Minute<<1)) {
newMetric.Connected = true
newMetric.Latency = pingStats.AvgRtt.Milliseconds()
newMetric.Uptime = 1
}
}

newMetric.TotalTime = 1
metrics.Connectivity[id] = newMetric
}
Expand Down
Loading

0 comments on commit 1827f8c

Please sign in to comment.