Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix getting updated user namespaces
Browse files Browse the repository at this point in the history
alichaddad committed Sep 22, 2022
1 parent 6210404 commit 82d96ca
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion core/clustersmngr/factory.go
Original file line number Diff line number Diff line change
@@ -103,6 +103,8 @@ type clustersManager struct {

// list of watchers to notify of clusters updates
watchers []*ClustersWatcher

usersLock sync.Map
}

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

clusterNs := cf.clustersNamespaces.Get(cluster.Name)
clusterNs, found := cf.clustersNamespaces.Get(cluster.Name)
if !found {
cf.log.Error(nil, "failed to get cluster namespaces", "cluster", cluster.Name)
return
}

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

func (cf *clustersManager) UserLock(userId string) *sync.Mutex {
actual, _ := cf.usersLock.LoadOrStore(userId, &sync.Mutex{})
lock := actual.(*sync.Mutex)
lock.Lock()
return lock
}

func (cf *clustersManager) GetUserNamespaces(user *auth.UserPrincipal) map[string][]v1.Namespace {
return cf.usersNamespaces.GetAll(user, cf.clusters.Get())
}

func (cf *clustersManager) userNsList(ctx context.Context, user *auth.UserPrincipal) map[string][]v1.Namespace {
userLock := cf.UserLock(user.ID)
defer userLock.Unlock()

userNamespaces := cf.GetUserNamespaces(user)
if len(userNamespaces) > 0 {
return userNamespaces
8 changes: 6 additions & 2 deletions core/clustersmngr/factory_caches.go
Original file line number Diff line number Diff line change
@@ -104,11 +104,15 @@ func (cn *ClustersNamespaces) Clear() {
cn.namespaces = make(map[string][]v1.Namespace)
}

func (cn *ClustersNamespaces) Get(cluster string) []v1.Namespace {
func (cn *ClustersNamespaces) Get(cluster string) ([]v1.Namespace, bool) {
cn.Lock()
defer cn.Unlock()

return cn.namespaces[cluster]
clusterObj, ok := cn.namespaces[cluster]
if !ok {
return nil, false
}
return clusterObj, true
}

type UsersNamespaces struct {

0 comments on commit 82d96ca

Please sign in to comment.