From f1b5518bf3bbff2676eb978988fdc72485fa43e0 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Wed, 16 Feb 2022 19:50:10 -0500 Subject: [PATCH] wip --- controllers/node.go | 31 +++++++++++------- controllers/node_grpc.go | 69 ++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/controllers/node.go b/controllers/node.go index 6aadbc7a..b2156a9b 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "errors" "fmt" "net/http" "strings" @@ -585,7 +586,18 @@ func updateNode(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(newNode) - runUpdates(&newNode, true, ifaceDelta) + if isServer(&node) { + if ifaceDelta { + if err := mq.PublishPeerUpdate(&node); err != nil { + logger.Log(1, "failed to publish peer update "+err.Error()) + } + } + if err := logic.UpdateNode(&node, &newNode); err != nil { + logger.Log(1, "error updating server node "+err.Error()) + } + } else { + runUpdates(&newNode) + } } func deleteNode(w http.ResponseWriter, r *http.Request) { @@ -620,23 +632,18 @@ func deleteNode(w http.ResponseWriter, r *http.Request) { returnSuccessResponse(w, r, nodeid+" deleted.") logger.Log(1, r.Header.Get("user"), "Deleted node", nodeid, "from network", params["network"]) + //:w runUpdates(&node, false) } -func runUpdates(node *models.Node, nodeUpdate bool) error { +func runUpdates(node *models.Node) error { //don't publish to server node - - if nodeUpdate && !isServer(node) { - if err := mq.NodeUpdate(node); err != nil { - logger.Log(1, "error publishing node update", err.Error()) - return err - } + if isServer(node) { + return errors.New("update to server node not permited") } - - if err := runServerUpdate(node, isServer(node)); err != nil { - logger.Log(1, "internal error when running peer node:", err.Error()) + if err := mq.NodeUpdate(node); err != nil { + logger.Log(1, "error publishing node update", err.Error()) return err } - return nil } diff --git a/controllers/node_grpc.go b/controllers/node_grpc.go index 3778da99..f0e10e6e 100644 --- a/controllers/node_grpc.go +++ b/controllers/node_grpc.go @@ -13,7 +13,6 @@ import ( "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/mq" - "github.com/gravitl/netmaker/servercfg" "github.com/gravitl/netmaker/serverctl" ) @@ -107,7 +106,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) Type: nodepb.NODE_TYPE, } - runUpdates(&node, false, false) + //runUpdates(&node, false, false) go func(node *models.Node) { if node.UDPHolePunch == "yes" { @@ -134,47 +133,47 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) } // NodeServiceServer.UpdateNode updates a node and responds over gRPC -func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) { +// func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.Object) (*nodepb.Object, error) { - var newnode models.Node - if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil { - return nil, err - } +// var newnode models.Node +// if err := json.Unmarshal([]byte(req.GetData()), &newnode); err != nil { +// return nil, err +// } - node, err := logic.GetNodeByID(newnode.ID) - if err != nil { - return nil, err - } +// node, err := logic.GetNodeByID(newnode.ID) +// if err != nil { +// return nil, err +// } - ifaceDelta := logic.IfaceDelta(&node, &newnode) +// ifaceDelta := logic.IfaceDelta(&node, &newnode) - if !servercfg.GetRce() { - newnode.PostDown = node.PostDown - newnode.PostUp = node.PostUp - } +// if !servercfg.GetRce() { +// newnode.PostDown = node.PostDown +// newnode.PostUp = node.PostUp +// } - err = logic.UpdateNode(&node, &newnode) - if err != nil { - return nil, err - } - newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network) - if err != nil { - return nil, err - } - getServerAddrs(&newnode) +// err = logic.UpdateNode(&node, &newnode) +// if err != nil { +// return nil, err +// } +// newnode.NetworkSettings, err = logic.GetNetworkSettings(node.Network) +// if err != nil { +// return nil, err +// } +// getServerAddrs(&newnode) - nodeData, errN := json.Marshal(&newnode) - if errN != nil { - return nil, err - } +// nodeData, errN := json.Marshal(&newnode) +// if errN != nil { +// return nil, err +// } - runUpdates(&newnode, false, ifaceDelta) +// runUpdates(&newnode, false, ifaceDelta) - return &nodepb.Object{ - Data: string(nodeData), - Type: nodepb.NODE_TYPE, - }, nil -} +// return &nodepb.Object{ +// Data: string(nodeData), +// Type: nodepb.NODE_TYPE, +// }, nil +// } func getServerAddrs(node *models.Node) { serverNodes := logic.GetServerNodes(node.Network)