Skip to content

Commit 1666763

Browse files
committed
fix getting updated user namespaces
1 parent bcaea22 commit 1666763

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

core/clustersmngr/factory.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ type clustersManager struct {
103103

104104
// list of watchers to notify of clusters updates
105105
watchers []*ClustersWatcher
106+
107+
usersLock sync.Map
106108
}
107109

108110
// 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.
413415
go func(cluster Cluster) {
414416
defer wg.Done()
415417

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+
}
417423

418424
cfg, err := ClientConfigWithUser(user, cf.kubeConfigOptions...)(cluster)
419425
if err != nil {
@@ -434,11 +440,21 @@ func (cf *clustersManager) UpdateUserNamespaces(ctx context.Context, user *auth.
434440
wg.Wait()
435441
}
436442

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+
437450
func (cf *clustersManager) GetUserNamespaces(user *auth.UserPrincipal) map[string][]v1.Namespace {
438451
return cf.usersNamespaces.GetAll(user, cf.clusters.Get())
439452
}
440453

441454
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+
442458
userNamespaces := cf.GetUserNamespaces(user)
443459
if len(userNamespaces) > 0 {
444460
return userNamespaces

core/clustersmngr/factory_caches.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,15 @@ func (cn *ClustersNamespaces) Clear() {
104104
cn.namespaces = make(map[string][]v1.Namespace)
105105
}
106106

107-
func (cn *ClustersNamespaces) Get(cluster string) []v1.Namespace {
107+
func (cn *ClustersNamespaces) Get(cluster string) ([]v1.Namespace, bool) {
108108
cn.Lock()
109109
defer cn.Unlock()
110110

111-
return cn.namespaces[cluster]
111+
clusterObj, ok := cn.namespaces[cluster]
112+
if !ok {
113+
return nil, false
114+
}
115+
return clusterObj, true
112116
}
113117

114118
type UsersNamespaces struct {

0 commit comments

Comments
 (0)