@@ -181,7 +181,7 @@ type ClusterService interface {
181
181
FindAllNamespacesByUserIdAndClusterId (userId int32 , clusterId int , isActionUserSuperAdmin bool ) ([]string , error )
182
182
FindAllForClusterByUserId (userId int32 , isActionUserSuperAdmin bool ) ([]ClusterBean , error )
183
183
FetchRolesFromGroup (userId int32 ) ([]* repository3.RoleModel , error )
184
- HandleErrorInClusterConnections (clusters []* ClusterBean , respMap map [ int ] error , clusterExistInDb bool )
184
+ HandleErrorInClusterConnections (clusters []* ClusterBean , respMap * sync. Map , clusterExistInDb bool )
185
185
ConnectClustersInBatch (clusters []* ClusterBean , clusterExistInDb bool )
186
186
ConvertClusterBeanToCluster (clusterBean * ClusterBean , userId int32 ) * repository.Cluster
187
187
ConvertClusterBeanObjectToCluster (bean * ClusterBean ) * v1alpha1.Cluster
@@ -259,11 +259,14 @@ func (impl *ClusterServiceImpl) ConvertClusterBeanToCluster(clusterBean *Cluster
259
259
260
260
// getAndUpdateClusterConnectionStatus is a cron function to update the connection status of all clusters
261
261
func (impl * ClusterServiceImpl ) getAndUpdateClusterConnectionStatus () {
262
- impl .logger .Debug ("starting cluster connection status fetch thread" )
263
- defer impl .logger .Debug ("stopped cluster connection status fetch thread" )
262
+ impl .logger .Info ("starting cluster connection status fetch thread" )
263
+ startTime := time .Now ()
264
+ defer func () {
265
+ impl .logger .Debugw ("cluster connection status fetch thread completed" , "timeTaken" , time .Since (startTime ))
266
+ }()
264
267
265
268
//getting all clusters
266
- clusters , err := impl .FindAllExceptVirtual ()
269
+ clusters , err := impl .FindAll ()
267
270
if err != nil {
268
271
impl .logger .Errorw ("error in getting all clusters" , "err" , err )
269
272
return
@@ -845,38 +848,54 @@ func (impl *ClusterServiceImpl) FetchRolesFromGroup(userId int32) ([]*repository
845
848
return roles , nil
846
849
}
847
850
851
+ func (impl * ClusterServiceImpl ) updateConnectionStatusForVirtualCluster (respMap * sync.Map , clusterId int , clusterName string ) {
852
+ connErr := fmt .Errorf ("Get virtual cluster '%s' error: connection not setup for isolated clusters" , clusterName )
853
+ respMap .Store (clusterId , connErr )
854
+ }
855
+
848
856
func (impl * ClusterServiceImpl ) ConnectClustersInBatch (clusters []* ClusterBean , clusterExistInDb bool ) {
849
857
var wg sync.WaitGroup
850
- respMap := make (map [int ]error )
851
- mutex := & sync.Mutex {}
852
-
858
+ respMap := & sync.Map {}
853
859
for idx , cluster := range clusters {
860
+ if cluster .IsVirtualCluster {
861
+ impl .updateConnectionStatusForVirtualCluster (respMap , cluster .Id , cluster .ClusterName )
862
+ continue
863
+ }
854
864
wg .Add (1 )
855
865
go func (idx int , cluster * ClusterBean ) {
856
866
defer wg .Done ()
857
867
clusterConfig := cluster .GetClusterConfig ()
858
868
_ , _ , k8sClientSet , err := impl .K8sUtil .GetK8sConfigAndClients (clusterConfig )
859
869
if err != nil {
860
- mutex .Lock ()
861
- respMap [cluster .Id ] = err
862
- mutex .Unlock ()
870
+ respMap .Store (cluster .Id , err )
863
871
return
864
872
}
865
873
866
874
id := cluster .Id
867
875
if ! clusterExistInDb {
868
876
id = idx
869
877
}
870
- impl .GetAndUpdateConnectionStatusForOneCluster (k8sClientSet , id , respMap , mutex )
878
+ impl .GetAndUpdateConnectionStatusForOneCluster (k8sClientSet , id , respMap )
871
879
}(idx , cluster )
872
880
}
873
881
874
882
wg .Wait ()
875
883
impl .HandleErrorInClusterConnections (clusters , respMap , clusterExistInDb )
876
884
}
877
885
878
- func (impl * ClusterServiceImpl ) HandleErrorInClusterConnections (clusters []* ClusterBean , respMap map [int ]error , clusterExistInDb bool ) {
879
- for id , err := range respMap {
886
+ func (impl * ClusterServiceImpl ) HandleErrorInClusterConnections (clusters []* ClusterBean , respMap * sync.Map , clusterExistInDb bool ) {
887
+ respMap .Range (func (key , value any ) bool {
888
+ defer func () {
889
+ // defer to handle panic on type assertion
890
+ if r := recover (); r != nil {
891
+ impl .logger .Errorw ("error in handling error in cluster connections" , "key" , key , "value" , value , "err" , r )
892
+ }
893
+ }()
894
+ id := key .(int )
895
+ var err error
896
+ if connectionError , ok := value .(error ); ok {
897
+ err = connectionError
898
+ }
880
899
errorInConnecting := ""
881
900
if err != nil {
882
901
errorInConnecting = err .Error ()
@@ -896,7 +915,8 @@ func (impl *ClusterServiceImpl) HandleErrorInClusterConnections(clusters []*Clus
896
915
//id is index of the cluster in clusters array
897
916
clusters [id ].ErrorInConnecting = errorInConnecting
898
917
}
899
- }
918
+ return true
919
+ })
900
920
}
901
921
902
922
func (impl * ClusterServiceImpl ) ValidateKubeconfig (kubeConfig string ) (map [string ]* ValidateClusterBean , error ) {
@@ -1066,7 +1086,7 @@ func (impl *ClusterServiceImpl) ValidateKubeconfig(kubeConfig string) (map[strin
1066
1086
1067
1087
}
1068
1088
1069
- func (impl * ClusterServiceImpl ) GetAndUpdateConnectionStatusForOneCluster (k8sClientSet * kubernetes.Clientset , clusterId int , respMap map [ int ] error , mutex * sync.Mutex ) {
1089
+ func (impl * ClusterServiceImpl ) GetAndUpdateConnectionStatusForOneCluster (k8sClientSet * kubernetes.Clientset , clusterId int , respMap * sync.Map ) {
1070
1090
response , err := impl .K8sUtil .GetLiveZCall (k8s .LiveZ , k8sClientSet )
1071
1091
log .Println ("received response for cluster livez status" , "response" , string (response ), "err" , err , "clusterId" , clusterId )
1072
1092
@@ -1092,9 +1112,8 @@ func (impl *ClusterServiceImpl) GetAndUpdateConnectionStatusForOneCluster(k8sCli
1092
1112
} else if err == nil && string (response ) != "ok" {
1093
1113
err = fmt .Errorf ("Validation failed with response : %s" , string (response ))
1094
1114
}
1095
- mutex .Lock ()
1096
- respMap [clusterId ] = err
1097
- mutex .Unlock ()
1115
+
1116
+ respMap .Store (clusterId , err )
1098
1117
}
1099
1118
1100
1119
func (impl * ClusterServiceImpl ) ConvertClusterBeanObjectToCluster (bean * ClusterBean ) * v1alpha1.Cluster {
0 commit comments