Skip to content

Use role name as string for role bindings #47

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
merged 1 commit into from
May 29, 2024
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
12 changes: 6 additions & 6 deletions support/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func CreateRoleBinding(t Test, namespace string, serviceAccount *corev1.ServiceA
return rb
}

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

roleBinding := &rbacv1.RoleBinding{
Expand All @@ -115,7 +115,7 @@ func CreateUserRoleBinding(t Test, namespace string, userName string, role *rbac
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "Role",
Name: role.Name,
Name: roleName,
},
Subjects: []rbacv1.Subject{
{
Expand All @@ -127,7 +127,7 @@ func CreateUserRoleBinding(t Test, namespace string, userName string, role *rbac
}
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/%s successfully", role.Namespace, role.Name)
t.T().Logf("Created User RoleBinding %s/%s successfully", roleBinding.Namespace, roleBinding.Name)

return rb
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func CreateClusterRoleBinding(t Test, serviceAccount *corev1.ServiceAccount, rol
return rb
}

func CreateUserClusterRoleBinding(t Test, userName string, role *rbacv1.ClusterRole) *rbacv1.ClusterRoleBinding {
func CreateUserClusterRoleBinding(t Test, userName string, roleName string) *rbacv1.ClusterRoleBinding {
t.T().Helper()

roleBinding := &rbacv1.ClusterRoleBinding{
Expand All @@ -182,7 +182,7 @@ func CreateUserClusterRoleBinding(t Test, userName string, role *rbacv1.ClusterR
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: role.Name,
Name: roleName,
},
Subjects: []rbacv1.Subject{
{
Expand All @@ -194,7 +194,7 @@ func CreateUserClusterRoleBinding(t Test, userName string, role *rbacv1.ClusterR
}
rb, err := t.Client().Core().RbacV1().ClusterRoleBindings().Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
t.T().Logf("Created User ClusterRoleBinding %s/%s successfully", role.Namespace, role.Name)
t.T().Logf("Created User ClusterRoleBinding %s successfully", roleBinding.Name)

t.T().Cleanup(func() {
t.Client().Core().RbacV1().ClusterRoleBindings().Delete(t.Ctx(), rb.Name, metav1.DeleteOptions{})
Expand Down
26 changes: 2 additions & 24 deletions support/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,13 @@ import (
"github.com/onsi/gomega"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestCreateUserRoleBinding(t *testing.T) {

test := NewTest(t)

role := &rbacv1.Role{
TypeMeta: metav1.TypeMeta{
APIVersion: rbacv1.SchemeGroupVersion.String(),
Kind: "Role",
},
ObjectMeta: metav1.ObjectMeta{
Name: "role1",
Namespace: "ns-1",
},
}

rb := CreateUserRoleBinding(test, "ns-1", "user-1", role)
rb := CreateUserRoleBinding(test, "ns-1", "user-1", "role1")

test.Expect(rb).To(gomega.Not(gomega.BeNil()))
test.Expect(rb.GenerateName).To(gomega.Equal("rb-"))
Expand All @@ -59,17 +47,7 @@ func TestCreateUserClusterRoleBinding(t *testing.T) {

test := NewTest(t)

crole := &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
APIVersion: rbacv1.SchemeGroupVersion.String(),
Kind: "ClusterRole",
},
ObjectMeta: metav1.ObjectMeta{
Name: "role1",
},
}

rb := CreateUserClusterRoleBinding(test, "user-1", crole)
rb := CreateUserClusterRoleBinding(test, "user-1", "role1")

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