-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AUTH-8: Move usercache from the oauth-apiserver to lib-go #1223
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package usercache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/client-go/tools/cache" | ||
|
||
userapi "github.com/openshift/api/user/v1" | ||
userinformer "github.com/openshift/client-go/user/informers/externalversions/user/v1" | ||
) | ||
|
||
// GroupCache is a skin on an indexer to provide the reverse index from user to groups. | ||
// Once we work out a cleaner way to extend a lister, this should live there. | ||
type GroupCache struct { | ||
indexer cache.Indexer | ||
groupsSynced cache.InformerSynced | ||
} | ||
|
||
const ByUserIndexName = "ByUser" | ||
|
||
// ByUserIndexKeys is cache.IndexFunc for Groups that will index groups by User, so that a direct cache lookup | ||
// using a User.Name will return all Groups that User is a member of | ||
func ByUserIndexKeys(obj interface{}) ([]string, error) { | ||
group, ok := obj.(*userapi.Group) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected type: %v", obj) | ||
} | ||
|
||
return group.Users, nil | ||
} | ||
|
||
func NewGroupCache(groupInformer userinformer.GroupInformer) *GroupCache { | ||
return &GroupCache{ | ||
indexer: groupInformer.Informer().GetIndexer(), | ||
groupsSynced: groupInformer.Informer().HasSynced, | ||
} | ||
} | ||
|
||
func (c *GroupCache) GroupsFor(username string) ([]*userapi.Group, error) { | ||
objs, err := c.indexer.ByIndex(ByUserIndexName, username) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
groups := make([]*userapi.Group, len(objs)) | ||
for i := range objs { | ||
groups[i] = objs[i].(*userapi.Group) | ||
} | ||
|
||
return groups, nil | ||
} | ||
|
||
func (c *GroupCache) HasSynced() bool { | ||
return c.groupsSynced() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package usercache | ||
|
||
import ( | ||
"testing" | ||
|
||
userv1 "github.com/openshift/api/user/v1" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
func TestGroupCache_GroupsFor(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
username string | ||
wantGroups sets.String | ||
}{ | ||
{ | ||
name: "user with no groups", | ||
username: "user0", | ||
}, | ||
{ | ||
name: "user with some groups", | ||
username: "user1", | ||
wantGroups: sets.NewString("group0", "group2"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
c := &GroupCache{ | ||
indexer: testGroupsIndexer(t), | ||
groupsSynced: func() bool { return true }, | ||
} | ||
got, err := c.GroupsFor(tt.username) | ||
require.NoError(t, err) | ||
|
||
gotGroupNames := sets.NewString() | ||
for _, g := range got { | ||
gotGroupNames.Insert(g.Name) | ||
} | ||
if gotGroupNames.Difference(tt.wantGroups).Len() > 0 { | ||
t.Errorf("wanted groups: %v; but got %v", tt.wantGroups.List(), gotGroupNames.List()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testGroupsIndexer(t *testing.T) cache.Indexer { | ||
testGroups := []*userv1.Group{ | ||
makeGroup("group0", "user1", "user2", "user3"), | ||
makeGroup("group1", "user2", "user3"), | ||
makeGroup("group2", "user1", "user3"), | ||
makeGroup("group12", "user3"), | ||
makeGroup("group123"), | ||
} | ||
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{ | ||
ByUserIndexName: ByUserIndexKeys, | ||
}) | ||
for _, g := range testGroups { | ||
require.NoError(t, indexer.Add(g)) | ||
} | ||
|
||
return indexer | ||
} | ||
|
||
func makeGroup(groupName string, members ...string) *userv1.Group { | ||
return &userv1.Group{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: groupName, | ||
}, | ||
Users: members, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intentionally public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's needed to set up the indexer for the given informer: https://github.com/openshift/oauth-apiserver/blob/7423fd05978658842ca56236d315e28a4fa6d40a/pkg/oauth/apiserver/apiserver.go#L109-L114