Forráskód Böngészése

only enable hooks on master pod when deployed on k8s

Abhishek Kondur 1 éve
szülő
commit
7a9c20edda
5 módosított fájl, 38 hozzáadás és 6 törlés
  1. 1 0
      config/config.go
  2. 12 4
      logic/timer.go
  3. 7 0
      logic/util.go
  4. 7 2
      main.go
  5. 11 0
      servercfg/serverconf.go

+ 1 - 0
config/config.go

@@ -48,6 +48,7 @@ type ServerConfig struct {
 	AllowedOrigin              string        `yaml:"allowedorigin"`
 	NodeID                     string        `yaml:"nodeid"`
 	RestBackend                string        `yaml:"restbackend"`
+	K8s                        bool          `yaml:"k8s"`
 	MessageQueueBackend        string        `yaml:"messagequeuebackend"`
 	DNSMode                    string        `yaml:"dnsmode"`
 	DisableRemoteIPCheck       string        `yaml:"disableremoteipcheck"`

+ 12 - 4
logic/timer.go

@@ -3,11 +3,13 @@ package logic
 import (
 	"context"
 	"fmt"
-	"github.com/gravitl/netmaker/logger"
-	"golang.org/x/exp/slog"
 	"sync"
 	"time"
 
+	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/servercfg"
+	"golang.org/x/exp/slog"
+
 	"github.com/gravitl/netmaker/models"
 )
 
@@ -56,8 +58,14 @@ func StartHookManager(ctx context.Context, wg *sync.WaitGroup) {
 			slog.Error("## Stopping Hook Manager")
 			return
 		case newhook := <-HookManagerCh:
-			wg.Add(1)
-			go addHookWithInterval(ctx, wg, newhook.Hook, newhook.Interval)
+			addHook := true
+			if servercfg.IsDeloyedOnK8s() && !K8sMasterPod() {
+				addHook = false
+			}
+			if addHook {
+				wg.Add(1)
+				go addHookWithInterval(ctx, wg, newhook.Hook, newhook.Interval)
+			}
 		}
 	}
 }

+ 7 - 0
logic/util.go

@@ -134,4 +134,11 @@ func RemoveStringSlice(slice []string, i int) []string {
 	return append(slice[:i], slice[i+1:]...)
 }
 
+// K8sMasterPod - check if this statefulset 0th pod
+func K8sMasterPod() bool {
+	podName := os.Getenv("HOSTNAME")
+	nameSlice := strings.Split(podName, "-")
+	return nameSlice[len(nameSlice)-1] == "0"
+}
+
 // == private ==

+ 7 - 2
main.go

@@ -28,7 +28,6 @@ import (
 	"golang.org/x/exp/slog"
 )
 
-
 var version = "v0.21.2"
 
 // Start DB Connection and start API Request Handler
@@ -163,7 +162,13 @@ func runMessageQueue(wg *sync.WaitGroup, ctx context.Context) {
 		logger.FatalLog("error connecting to MQ Broker")
 	}
 	defer mq.CloseClient()
-	go mq.Keepalive(ctx)
+	keepAlive := true
+	if servercfg.IsDeloyedOnK8s() && !logic.K8sMasterPod() {
+		keepAlive = false
+	}
+	if keepAlive {
+		go mq.Keepalive(ctx)
+	}
 	go func() {
 		peerUpdate := make(chan *models.Node)
 		go logic.ManageZombies(ctx, peerUpdate)

+ 11 - 0
servercfg/serverconf.go

@@ -247,6 +247,17 @@ func GetCoreDNSAddr() string {
 	return addr
 }
 
+// IsDeloyedOnK8s - returns true if deployed on k8s
+func IsDeloyedOnK8s() bool {
+	k8s := false
+	if os.Getenv("K8s") == "true" {
+		k8s = true
+	} else if config.Config.Server.K8s {
+		k8s = true
+	}
+	return k8s
+}
+
 // GetPublicBrokerEndpoint - returns the public broker endpoint which shall be used by netclient
 func GetPublicBrokerEndpoint() string {
 	if os.Getenv("BROKER_ENDPOINT") != "" {