Skip to content

Add method to create rolebinding for user in namespace scope #50

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions support/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,37 @@ func CreateUserClusterRoleBinding(t Test, userName string, roleName string) *rba

return rb
}

func CreateUserRoleBindingWithClusterRole(t Test, userName string, namespace string, roleName string) *rbacv1.RoleBinding {
t.T().Helper()

// Create a RoleBinding to give specified role access to the user for given namespace
roleBinding := &rbacv1.RoleBinding{
TypeMeta: metav1.TypeMeta{
APIVersion: rbacv1.SchemeGroupVersion.String(),
Kind: "RoleBinding",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "rb-",
Namespace: namespace,
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: roleName, // grants specified role access
APIGroup: rbacv1.SchemeGroupVersion.Group,
},
Subjects: []rbacv1.Subject{
{
Kind: "User",
Name: userName,
APIGroup: rbacv1.SchemeGroupVersion.Group,
},
},
}

rb, err := t.Client().Core().RbacV1().RoleBindings(namespace).Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
t.T().Logf("Created User RoleBinding %s in namespace %s successfully", roleBinding.Name, roleBinding.Namespace)

return rb
}
19 changes: 19 additions & 0 deletions support/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ func TestCreateUserClusterRoleBinding(t *testing.T) {
test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User"))
test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1"))
}

func TestCreateUserRoleBindingWithClusterRole(t *testing.T) {

test := NewTest(t)
namespace := test.NewTestNamespace()

rb := CreateUserRoleBindingWithClusterRole(test, "user-1", namespace.Name, "role1")

test.Expect(rb).To(gomega.Not(gomega.BeNil()))
test.Expect(rb.GenerateName).To(gomega.Equal("rb-"))

test.Expect(rb.RoleRef.APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
test.Expect(rb.RoleRef.Kind).To(gomega.Equal("ClusterRole"))
test.Expect(rb.RoleRef.Name).To(gomega.Equal("role1"))

test.Expect(rb.Subjects[0].APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User"))
test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1"))
}