Skip to content

Commit

Permalink
fix getting updated user namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alichaddad committed Sep 22, 2022
1 parent bcaea22 commit e7cd75e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
18 changes: 17 additions & 1 deletion core/clustersmngr/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions core/clustersmngr/factory_caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ 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 {
Expand Down
7 changes: 5 additions & 2 deletions core/clustersmngr/factory_caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ func TestClustersNamespaces(t *testing.T) {

cs.Set(clusterName, []v1.Namespace{ns})

g.Expect(cs.Get(clusterName)).To(Equal([]v1.Namespace{ns}))
got, found := cs.Get(clusterName)
g.Expect(found).To(BeTrue())
g.Expect(got).To(Equal([]v1.Namespace{ns}))

cs.Clear()

g.Expect(cs.Get(clusterName)).To(HaveLen(0))
_, found = cs.Get(clusterName)
g.Expect(found).To(BeFalse())
}

func TestClusterSet_Set(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions core/clustersmngr/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func TestUpdateUsers(t *testing.T) {
clustersFetcher.FetchReturns([]clustersmngr.Cluster{c1, c2}, nil)

g.Expect(clustersManager.UpdateClusters(ctx)).To(Succeed())
g.Expect(clustersManager.UpdateNamespaces(ctx))
clustersManager.UpdateUserNamespaces(ctx, u1)

contents := clustersManager.GetUserNamespaces(u1)
Expand Down

0 comments on commit e7cd75e

Please sign in to comment.