123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- package mq
- import (
- "encoding/json"
- "errors"
- "fmt"
- "time"
- "github.com/gravitl/netmaker/logger"
- "github.com/gravitl/netmaker/logic"
- "github.com/gravitl/netmaker/logic/metrics"
- "github.com/gravitl/netmaker/models"
- "github.com/gravitl/netmaker/nm-proxy/manager"
- "github.com/gravitl/netmaker/servercfg"
- "github.com/gravitl/netmaker/serverctl"
- )
- // PublishPeerUpdate --- deterines and publishes a peer update to all the peers of a node
- func PublishPeerUpdate(newNode *models.Node, publishToSelf bool) error {
- if !servercfg.IsMessageQueueBackend() {
- return nil
- }
- networkNodes, err := logic.GetNetworkNodes(newNode.Network)
- if err != nil {
- logger.Log(1, "err getting Network Nodes", err.Error())
- return err
- }
- for _, node := range networkNodes {
- if node.IsServer == "yes" {
- if servercfg.IsProxyEnabled() {
- err := PublishProxyUpdate(manager.AddInterface, &node)
- if err != nil {
- logger.Log(0, "failed to send proxy update for server: ", err.Error())
- }
- }
- continue
- }
- if !publishToSelf && newNode.ID == node.ID {
- //skip self
- continue
- }
- err = PublishSinglePeerUpdate(&node)
- if err != nil {
- logger.Log(1, "failed to publish peer update to node", node.Name, "on network", node.Network, ":", err.Error())
- }
- }
- return err
- }
- func PublishProxyUpdate(action manager.ProxyAction, node *models.Node) error {
- peerUpdates, err := logic.GetPeersForProxy(node, false)
- if err != nil {
- return err
- }
- err = ProxyUpdate(&manager.ManagerAction{
- Action: action,
- Payload: peerUpdates}, node)
- if err != nil {
- logger.Log(1, "failed to send proxy update: ", err.Error())
- return err
- }
- return nil
- }
- // PublishSinglePeerUpdate --- determines and publishes a peer update to one node
- func PublishSinglePeerUpdate(node *models.Node) error {
- peerUpdate, err := logic.GetPeerUpdate(node)
- if err != nil {
- return err
- }
- if node.Proxy {
- proxyUpdate, err := logic.GetPeersForProxy(node, false)
- if err != nil {
- return err
- }
- peerUpdate.ProxyUpdate = manager.ManagerAction{
- Action: manager.AddInterface,
- Payload: proxyUpdate,
- }
- }
- data, err := json.Marshal(&peerUpdate)
- if err != nil {
- return err
- }
- return publish(node, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data)
- }
- // PublishPeerUpdate --- publishes a peer update to all the peers of a node
- func PublishExtPeerUpdate(node *models.Node) error {
- var err error
- if logic.IsLocalServer(node) {
- if err = logic.ServerUpdate(node, false); err != nil {
- logger.Log(1, "server node:", node.ID, "failed to update peers with ext clients")
- return err
- } else {
- return nil
- }
- }
- if !servercfg.IsMessageQueueBackend() {
- return nil
- }
- peerUpdate, err := logic.GetPeerUpdate(node)
- if err != nil {
- return err
- }
- data, err := json.Marshal(&peerUpdate)
- if err != nil {
- return err
- }
- if err = publish(node, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data); err != nil {
- return err
- }
- go PublishPeerUpdate(node, false)
- return nil
- }
- // NodeUpdate -- publishes a node update
- func NodeUpdate(node *models.Node) error {
- var err error
- if !servercfg.IsMessageQueueBackend() || node.IsServer == "yes" {
- return nil
- }
- logger.Log(3, "publishing node update to "+node.Name)
- if len(node.NetworkSettings.AccessKeys) > 0 {
- node.NetworkSettings.AccessKeys = []models.AccessKey{} // not to be sent (don't need to spread access keys around the network; we need to know how to reach other nodes, not become them)
- }
- if node.Proxy {
- err = PublishProxyUpdate(manager.AddInterface, node)
- if err != nil {
- logger.Log(1, "failed to publish proxy update to node", node.Name, "on network", node.Network, ":", err.Error())
- }
- } else {
- data, err := json.Marshal(node)
- if err != nil {
- logger.Log(2, "error marshalling node update ", err.Error())
- return err
- }
- if err = publish(node, fmt.Sprintf("update/%s/%s", node.Network, node.ID), data); err != nil {
- logger.Log(2, "error publishing node update to peer ", node.ID, err.Error())
- return err
- }
- }
- return nil
- }
- //ProxyUpdate -- publishes updates to peers related to proxy
- func ProxyUpdate(proxyPayload *manager.ManagerAction, node *models.Node) error {
- if !servercfg.IsMessageQueueBackend() {
- return nil
- }
- if node.IsServer == "yes" {
- logic.ProxyMgmChan <- proxyPayload
- return nil
- }
- logger.Log(3, "publishing proxy update to "+node.Name)
- data, err := json.Marshal(proxyPayload)
- if err != nil {
- logger.Log(2, "error marshalling node update ", err.Error())
- return err
- }
- if err = publish(node, fmt.Sprintf("proxy/%s/%s", node.Network, node.ID), data); err != nil {
- logger.Log(2, "error publishing proxy update to peer ", node.ID, err.Error())
- return err
- }
- return nil
- }
- // sendPeers - retrieve networks, send peer ports to all peers
- func sendPeers() {
- networks, err := logic.GetNetworks()
- if err != nil {
- logger.Log(1, "error retrieving networks for keepalive", err.Error())
- }
- var force bool
- peer_force_send++
- if peer_force_send == 5 {
- // run iptables update to ensure gateways work correctly and mq is forwarded if containerized
- if servercfg.ManageIPTables() != "off" {
- serverctl.InitIPTables(false)
- }
- servercfg.SetHost()
- force = true
- peer_force_send = 0
- err := logic.TimerCheckpoint() // run telemetry & log dumps if 24 hours has passed..
- if err != nil {
- logger.Log(3, "error occurred on timer,", err.Error())
- }
- collectServerMetrics(networks[:])
- }
- for _, network := range networks {
- serverNode, errN := logic.GetNetworkServerLocal(network.NetID)
- if errN == nil {
- serverNode.SetLastCheckIn()
- if err := logic.UpdateNode(&serverNode, &serverNode); err != nil {
- logger.Log(0, "failed checkin for server node", serverNode.Name, "on network", network.NetID, err.Error())
- }
- }
- isLeader := logic.IsLeader(&serverNode)
- if errN == nil && isLeader {
- if network.DefaultUDPHolePunch == "yes" {
- if logic.ShouldPublishPeerPorts(&serverNode) || force {
- if force {
- logger.Log(2, "sending scheduled peer update (5 min)")
- }
- err = PublishPeerUpdate(&serverNode, false)
- if err != nil {
- logger.Log(1, "error publishing udp port updates for network", network.NetID)
- logger.Log(1, errN.Error())
- }
- }
- }
- } else {
- if isLeader {
- logger.Log(1, "unable to retrieve leader for network ", network.NetID)
- }
- logger.Log(2, "server checkin complete for server", serverNode.Name, "on network", network.NetID)
- serverctl.SyncServerNetwork(network.NetID)
- if errN != nil {
- logger.Log(1, errN.Error())
- }
- if servercfg.IsProxyEnabled() {
- serverctl.SyncServerNetworkWithProxy()
- }
- }
- }
- }
- // ServerStartNotify - notifies all non server nodes to pull changes after a restart
- func ServerStartNotify() error {
- nodes, err := logic.GetAllNodes()
- if err != nil {
- return err
- }
- for i := range nodes {
- nodes[i].Action = models.NODE_FORCE_UPDATE
- if err = NodeUpdate(&nodes[i]); err != nil {
- logger.Log(1, "error when notifying node", nodes[i].Name, " - ", nodes[i].ID, "of a server startup")
- }
- }
- return nil
- }
- // function to collect and store metrics for server nodes
- func collectServerMetrics(networks []models.Network) {
- if !servercfg.Is_EE {
- return
- }
- if len(networks) > 0 {
- for i := range networks {
- currentNetworkNodes, err := logic.GetNetworkNodes(networks[i].NetID)
- if err != nil {
- continue
- }
- currentServerNodes := logic.GetServerNodes(networks[i].NetID)
- if len(currentServerNodes) > 0 {
- for i := range currentServerNodes {
- if logic.IsLocalServer(¤tServerNodes[i]) {
- serverMetrics := logic.CollectServerMetrics(currentServerNodes[i].ID, currentNetworkNodes)
- if serverMetrics != nil {
- serverMetrics.NodeName = currentServerNodes[i].Name
- serverMetrics.NodeID = currentServerNodes[i].ID
- serverMetrics.IsServer = "yes"
- serverMetrics.Network = currentServerNodes[i].Network
- if err = metrics.GetExchangedBytesForNode(¤tServerNodes[i], serverMetrics); err != nil {
- logger.Log(1, fmt.Sprintf("failed to update exchanged bytes info for server: %s, err: %v",
- currentServerNodes[i].Name, err))
- }
- updateNodeMetrics(¤tServerNodes[i], serverMetrics)
- if err = logic.UpdateMetrics(currentServerNodes[i].ID, serverMetrics); err != nil {
- logger.Log(1, "failed to update metrics for server node", currentServerNodes[i].ID)
- }
- if servercfg.IsMetricsExporter() {
- logger.Log(2, "-------------> SERVER METRICS: ", fmt.Sprintf("%+v", serverMetrics))
- if err := pushMetricsToExporter(*serverMetrics); err != nil {
- logger.Log(2, "failed to push server metrics to exporter: ", err.Error())
- }
- }
- }
- }
- }
- }
- }
- }
- }
- func pushMetricsToExporter(metrics models.Metrics) error {
- logger.Log(2, "----> Pushing metrics to exporter")
- data, err := json.Marshal(metrics)
- if err != nil {
- return errors.New("failed to marshal metrics: " + err.Error())
- }
- if token := mqclient.Publish("metrics_exporter", 2, true, data); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
- var err error
- if token.Error() == nil {
- err = errors.New("connection timeout")
- } else {
- err = token.Error()
- }
- return err
- }
- return nil
- }
|