@@ -38,13 +38,12 @@ var Running bool
3838var ClientList map [string ]int
3939
4040// ClientListLock Lock
41- var ClientListLock * sync.Mutex
41+ var ClientListLock * sync.RWMutex
4242
4343func init () {
4444 Running = true
4545 ClientList = map [string ]int {}
46- ClientListLock = & sync.Mutex {}
47-
46+ ClientListLock = & sync.RWMutex {}
4847}
4948
5049// ========== //
@@ -706,11 +705,7 @@ func DeleteClientEntry(nodeIP string) {
706705 ClientListLock .Lock ()
707706 defer ClientListLock .Unlock ()
708707
709- _ , exists := ClientList [nodeIP ]
710-
711- if exists {
712- delete (ClientList , nodeIP )
713- }
708+ delete (ClientList , nodeIP )
714709}
715710
716711// =============== //
@@ -722,66 +717,72 @@ func connectToKubeArmor(nodeIP, port string) error {
722717 // create connection info
723718 server := nodeIP + ":" + port
724719
725- defer DeleteClientEntry (nodeIP )
720+ for Running {
721+ ClientListLock .RLock ()
722+ _ , found := ClientList [nodeIP ]
723+ ClientListLock .RUnlock ()
724+ if ! found {
725+ // KubeArmor with this IP is deleted or the IP has changed
726+ // parent function will spawn a new goroutine accordingly
727+ break
728+ }
726729
727- // create a client
728- client := NewClient (server )
729- if client == nil {
730- return nil
731- }
730+ // create a client
731+ client := NewClient (server )
732+ if client == nil {
733+ time .Sleep (5 * time .Second ) // wait for 5 second before retrying
734+ continue
735+ }
732736
733- // do healthcheck
734- if ok := client .DoHealthCheck (); ! ok {
735- kg .Warnf ("Failed to check the liveness of KubeArmor's gRPC service (%s)" , server )
736- return nil
737- }
738- kg .Printf ("Checked the liveness of KubeArmor's gRPC service (%s)" , server )
737+ // do healthcheck
738+ if ok := client .DoHealthCheck (); ! ok {
739+ kg .Warnf ("Failed to check the liveness of KubeArmor's gRPC service (%s)" , server )
740+ time .Sleep (5 * time .Second ) // wait for 5 second before retrying
741+ continue
742+ }
743+ kg .Printf ("Checked the liveness of KubeArmor's gRPC service (%s)" , server )
739744
740- var wg sync.WaitGroup
741- stop := make (chan struct {})
742- errCh := make (chan error , 1 )
745+ var wg sync.WaitGroup
746+ stop := make (chan struct {})
747+ errCh := make (chan error , 1 )
743748
744- // Start watching messages
745- wg .Add (1 )
746- go func () {
747- client .WatchMessages (& wg , stop , errCh )
748- }()
749- kg .Print ("Started to watch messages from " + server )
749+ // Start watching messages
750+ wg .Add (1 )
751+ go client .WatchMessages (& wg , stop , errCh )
752+ kg .Print ("Started to watch messages from " + server )
750753
751- // Start watching alerts
752- wg .Add (1 )
753- go func () {
754- client .WatchAlerts (& wg , stop , errCh )
755- }()
756- kg .Print ("Started to watch alerts from " + server )
754+ // Start watching alerts
755+ wg .Add (1 )
756+ go client .WatchAlerts (& wg , stop , errCh )
757+ kg .Print ("Started to watch alerts from " + server )
757758
758- // Start watching logs
759- wg .Add (1 )
760- go func () {
761- client .WatchLogs (& wg , stop , errCh )
762- }()
763- kg .Print ("Started to watch logs from " + server )
764-
765- // Wait for an error or all goroutines to finish
766- select {
767- case err := <- errCh :
768- close (stop ) // Stop other goroutines
769- kg .Warn (err .Error ())
770- case <- func () chan struct {} {
771- done := make (chan struct {})
772- go func () {
773- wg .Wait ()
774- close (done )
775- }()
776- return done
777- }():
778- // All goroutines finished without error
779- }
759+ // Start watching logs
760+ wg .Add (1 )
761+ go client .WatchLogs (& wg , stop , errCh )
762+ kg .Print ("Started to watch logs from " + server )
780763
781- if err := client .DestroyClient (); err != nil {
782- kg .Warnf ("Failed to destroy the client (%s) %s" , server , err .Error ())
764+ // Wait for an error or all goroutines to finish
765+ select {
766+ case err := <- errCh :
767+ close (stop ) // Stop other goroutines
768+ kg .Warn (err .Error ())
769+ case <- func () chan struct {} {
770+ done := make (chan struct {})
771+ go func () {
772+ wg .Wait ()
773+ close (done )
774+ }()
775+ return done
776+ }():
777+ // All goroutines finished without error
778+ }
779+
780+ if err := client .DestroyClient (); err != nil {
781+ kg .Warnf ("Failed to destroy the client (%s) %s" , server , err .Error ())
782+ }
783+
784+ kg .Printf ("Destroyed the client (%s)" , server )
783785 }
784- kg .Printf ("Destroyed the client (%s)" , server )
785786
786787 return nil
787788}
@@ -810,16 +811,13 @@ func (rs *RelayServer) GetFeedsFromNodes() {
810811 }
811812
812813 for Running {
813- select {
814- case ip := <- ipsChan :
815- ClientListLock .Lock ()
816- if _ , ok := ClientList [ip ]; ! ok {
817- ClientList [ip ] = 1
818- go connectToKubeArmor (ip , rs .Port )
819- }
820- ClientListLock .Unlock ()
814+ ip := <- ipsChan
815+ ClientListLock .Lock ()
816+ if _ , ok := ClientList [ip ]; ! ok {
817+ ClientList [ip ] = 1
818+ go connectToKubeArmor (ip , rs .Port )
821819 }
822- time . Sleep ( 10 * time . Second )
820+ ClientListLock . Unlock ( )
823821 }
824822 }
825823}
0 commit comments