@@ -103,6 +103,8 @@ type clustersManager struct {
103
103
104
104
// list of watchers to notify of clusters updates
105
105
watchers []* ClustersWatcher
106
+
107
+ usersLock sync.Map
106
108
}
107
109
108
110
// ClusterListUpdate records the changes to the cluster state managed by the factory.
@@ -413,7 +415,11 @@ func (cf *clustersManager) UpdateUserNamespaces(ctx context.Context, user *auth.
413
415
go func (cluster Cluster ) {
414
416
defer wg .Done ()
415
417
416
- clusterNs := cf .clustersNamespaces .Get (cluster .Name )
418
+ clusterNs , found := cf .clustersNamespaces .Get (cluster .Name )
419
+ if ! found {
420
+ cf .log .Error (nil , "failed to get cluster namespaces" , "cluster" , cluster .Name )
421
+ return
422
+ }
417
423
418
424
cfg , err := ClientConfigWithUser (user , cf .kubeConfigOptions ... )(cluster )
419
425
if err != nil {
@@ -434,11 +440,21 @@ func (cf *clustersManager) UpdateUserNamespaces(ctx context.Context, user *auth.
434
440
wg .Wait ()
435
441
}
436
442
443
+ func (cf * clustersManager ) UserLock (userId string ) * sync.Mutex {
444
+ actual , _ := cf .usersLock .LoadOrStore (userId , & sync.Mutex {})
445
+ lock := actual .(* sync.Mutex )
446
+ lock .Lock ()
447
+ return lock
448
+ }
449
+
437
450
func (cf * clustersManager ) GetUserNamespaces (user * auth.UserPrincipal ) map [string ][]v1.Namespace {
438
451
return cf .usersNamespaces .GetAll (user , cf .clusters .Get ())
439
452
}
440
453
441
454
func (cf * clustersManager ) userNsList (ctx context.Context , user * auth.UserPrincipal ) map [string ][]v1.Namespace {
455
+ userLock := cf .UserLock (user .ID )
456
+ defer userLock .Unlock ()
457
+
442
458
userNamespaces := cf .GetUserNamespaces (user )
443
459
if len (userNamespaces ) > 0 {
444
460
return userNamespaces
0 commit comments