Merge pull request #762 from gravitl/feature_v0.10.0_iface_trigger

Feature v0.10.0 iface trigger
This commit is contained in:
Matthew R Kasun 2022-02-14 11:45:11 -05:00 committed by GitHub
commit fbc8182f33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 16 deletions

View file

@ -16,7 +16,10 @@ import (
"github.com/gravitl/netmaker/servercfg"
)
// ALL_NETWORK_ACCESS - represents all networks
const ALL_NETWORK_ACCESS = "THIS_USER_HAS_ALL"
// NO_NETWORKS_PRESENT - represents no networks
const NO_NETWORKS_PRESENT = "THIS_USER_HAS_NONE"
func networkHandlers(r *mux.Router) {
@ -116,7 +119,7 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
logger.Log(2, "failed to get server node")
return
}
runUpdates(&node, false)
runUpdates(&node, false, false)
}
// Update a network
@ -181,7 +184,7 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
return
}
for _, node := range nodes {
runUpdates(&node, true)
runUpdates(&node, true, false)
}
}

View file

@ -412,7 +412,7 @@ func createNode(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, false)
runUpdates(&node, false, false)
}
//Takes node out of pending state
@ -430,7 +430,7 @@ func uncordonNode(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("SUCCESS")
runUpdates(&node, true)
runUpdates(&node, true, false)
}
func createEgressGateway(w http.ResponseWriter, r *http.Request) {
@ -454,7 +454,7 @@ func createEgressGateway(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}
func deleteEgressGateway(w http.ResponseWriter, r *http.Request) {
@ -472,7 +472,7 @@ func deleteEgressGateway(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}
// == INGRESS ==
@ -492,7 +492,7 @@ func createIngressGateway(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}
func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
@ -509,7 +509,7 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}
func updateNode(w http.ResponseWriter, r *http.Request) {
@ -551,6 +551,8 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
newNode.PostUp = node.PostUp
}
ifaceDelta := logic.IfaceDelta(&node, &newNode)
err = logic.UpdateNode(&node, &newNode)
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
@ -579,7 +581,7 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(newNode)
runUpdates(&newNode, true)
runUpdates(&newNode, true, ifaceDelta)
}
func deleteNode(w http.ResponseWriter, r *http.Request) {
@ -613,12 +615,11 @@ func deleteNode(w http.ResponseWriter, r *http.Request) {
}
returnSuccessResponse(w, r, nodeid+" deleted.")
time.Sleep(time.Second << 1)
logger.Log(1, r.Header.Get("user"), "Deleted node", nodeid, "from network", params["network"])
runUpdates(&node, false)
runUpdates(&node, false, true)
}
func runUpdates(node *models.Node, nodeUpdate bool) error {
func runUpdates(node *models.Node, nodeUpdate bool, requiresPause bool) error {
//don't publish to server node
if nodeUpdate && !isServer(node) {
@ -628,6 +629,10 @@ func runUpdates(node *models.Node, nodeUpdate bool) error {
}
}
if requiresPause { // TODO in future, detect when a node has finished iface update
time.Sleep(time.Second * 10)
}
if err := runServerPeerUpdate(node, isServer(node)); err != nil {
logger.Log(1, "internal error when running peer node:", err.Error())
return err

View file

@ -107,7 +107,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
Type: nodepb.NODE_TYPE,
}
runUpdates(&node, false)
runUpdates(&node, false, false)
go func(node *models.Node) {
if node.UDPHolePunch == "yes" {
@ -146,6 +146,8 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object)
return nil, err
}
ifaceDelta := logic.IfaceDelta(&node, &newnode)
if !servercfg.GetRce() {
newnode.PostDown = node.PostDown
newnode.PostUp = node.PostUp
@ -166,7 +168,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object)
return nil, err
}
runUpdates(&newnode, false)
runUpdates(&newnode, false, ifaceDelta)
return &nodepb.Object{
Data: string(nodeData),

View file

@ -36,7 +36,7 @@ func createRelay(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}
func deleteRelay(w http.ResponseWriter, r *http.Request) {
@ -58,5 +58,5 @@ func deleteRelay(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
runUpdates(&node, true)
runUpdates(&node, true, false)
}

View file

@ -46,6 +46,62 @@ func HasPeerConnected(node *models.Node) bool {
return false
}
// IfaceDelta - checks if the new node causes an interface change
func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
// single comparison statements
if newNode.Endpoint != currentNode.Endpoint ||
newNode.LocalAddress != currentNode.LocalAddress ||
newNode.PublicKey != currentNode.PublicKey ||
newNode.Address != currentNode.Address ||
newNode.IsEgressGateway != currentNode.IsEgressGateway ||
newNode.IsIngressGateway != currentNode.IsIngressGateway ||
newNode.IsRelay != currentNode.IsRelay ||
newNode.UDPHolePunch != currentNode.UDPHolePunch ||
newNode.IsPending != currentNode.IsPending ||
newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
newNode.DNSOn != currentNode.DNSOn ||
len(newNode.ExcludedAddrs) != len(currentNode.ExcludedAddrs) ||
len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
return true
}
// multi-comparison statements
if newNode.IsDualStack == "yes" {
if newNode.Address6 != currentNode.Address6 {
return true
}
}
if newNode.IsEgressGateway == "yes" {
if len(currentNode.EgressGatewayRanges) != len(newNode.EgressGatewayRanges) {
return true
}
for _, address := range newNode.EgressGatewayRanges {
if !StringSliceContains(currentNode.EgressGatewayRanges, address) {
return true
}
}
}
if newNode.IsRelay == "yes" {
if len(currentNode.RelayAddrs) != len(newNode.RelayAddrs) {
return true
}
for _, address := range newNode.RelayAddrs {
if !StringSliceContains(currentNode.RelayAddrs, address) {
return true
}
}
}
for _, address := range newNode.AllowedIPs {
if !StringSliceContains(currentNode.AllowedIPs, address) {
return true
}
}
return false
}
// == Private Functions ==
// gets the server peers locally

View file

@ -6,6 +6,7 @@ import (
"github.com/gravitl/netmaker/models"
)
// IfaceDelta - checks if the new node causes an interface change
func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
// single comparison statements
if newNode.Endpoint != currentNode.Endpoint ||