Skip to content

Commit 69f4dc6

Browse files
sutaakaropenshift-merge-bot[bot]
authored andcommitted
Add user rolebindings
1 parent 69362af commit 69f4dc6

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

support/rbac.go

+66
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ func CreateRoleBinding(t Test, namespace string, serviceAccount *corev1.ServiceA
101101
return rb
102102
}
103103

104+
func CreateUserRoleBinding(t Test, namespace string, userName string, role *rbacv1.Role) *rbacv1.RoleBinding {
105+
t.T().Helper()
106+
107+
roleBinding := &rbacv1.RoleBinding{
108+
TypeMeta: metav1.TypeMeta{
109+
APIVersion: rbacv1.SchemeGroupVersion.String(),
110+
Kind: "RoleBinding",
111+
},
112+
ObjectMeta: metav1.ObjectMeta{
113+
GenerateName: "rb-",
114+
},
115+
RoleRef: rbacv1.RoleRef{
116+
APIGroup: rbacv1.SchemeGroupVersion.Group,
117+
Kind: "Role",
118+
Name: role.Name,
119+
},
120+
Subjects: []rbacv1.Subject{
121+
{
122+
Kind: "User",
123+
APIGroup: rbacv1.SchemeGroupVersion.Group,
124+
Name: userName,
125+
},
126+
},
127+
}
128+
rb, err := t.Client().Core().RbacV1().RoleBindings(namespace).Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
129+
t.Expect(err).NotTo(gomega.HaveOccurred())
130+
t.T().Logf("Created User RoleBinding %s/%s successfully", role.Namespace, role.Name)
131+
132+
return rb
133+
}
134+
104135
func CreateClusterRoleBinding(t Test, serviceAccount *corev1.ServiceAccount, role *rbacv1.ClusterRole) *rbacv1.ClusterRoleBinding {
105136
t.T().Helper()
106137

@@ -136,3 +167,38 @@ func CreateClusterRoleBinding(t Test, serviceAccount *corev1.ServiceAccount, rol
136167

137168
return rb
138169
}
170+
171+
func CreateUserClusterRoleBinding(t Test, userName string, role *rbacv1.ClusterRole) *rbacv1.ClusterRoleBinding {
172+
t.T().Helper()
173+
174+
roleBinding := &rbacv1.ClusterRoleBinding{
175+
TypeMeta: metav1.TypeMeta{
176+
APIVersion: rbacv1.SchemeGroupVersion.String(),
177+
Kind: "ClusterRoleBinding",
178+
},
179+
ObjectMeta: metav1.ObjectMeta{
180+
GenerateName: "crb-",
181+
},
182+
RoleRef: rbacv1.RoleRef{
183+
APIGroup: rbacv1.SchemeGroupVersion.Group,
184+
Kind: "ClusterRole",
185+
Name: role.Name,
186+
},
187+
Subjects: []rbacv1.Subject{
188+
{
189+
Kind: "User",
190+
APIGroup: rbacv1.SchemeGroupVersion.Group,
191+
Name: userName,
192+
},
193+
},
194+
}
195+
rb, err := t.Client().Core().RbacV1().ClusterRoleBindings().Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
196+
t.Expect(err).NotTo(gomega.HaveOccurred())
197+
t.T().Logf("Created User ClusterRoleBinding %s/%s successfully", role.Namespace, role.Name)
198+
199+
t.T().Cleanup(func() {
200+
t.Client().Core().RbacV1().ClusterRoleBindings().Delete(t.Ctx(), rb.Name, metav1.DeleteOptions{})
201+
})
202+
203+
return rb
204+
}

support/rbac_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package support
18+
19+
import (
20+
"testing"
21+
22+
"github.com/onsi/gomega"
23+
24+
rbacv1 "k8s.io/api/rbac/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
)
27+
28+
func TestCreateUserRoleBinding(t *testing.T) {
29+
30+
test := NewTest(t)
31+
32+
role := &rbacv1.Role{
33+
TypeMeta: metav1.TypeMeta{
34+
APIVersion: rbacv1.SchemeGroupVersion.String(),
35+
Kind: "Role",
36+
},
37+
ObjectMeta: metav1.ObjectMeta{
38+
Name: "role1",
39+
Namespace: "ns-1",
40+
},
41+
}
42+
43+
rb := CreateUserRoleBinding(test, "ns-1", "user-1", role)
44+
45+
test.Expect(rb).To(gomega.Not(gomega.BeNil()))
46+
test.Expect(rb.GenerateName).To(gomega.Equal("rb-"))
47+
test.Expect(rb.Namespace).To(gomega.Equal("ns-1"))
48+
49+
test.Expect(rb.RoleRef.APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
50+
test.Expect(rb.RoleRef.Kind).To(gomega.Equal("Role"))
51+
test.Expect(rb.RoleRef.Name).To(gomega.Equal("role1"))
52+
53+
test.Expect(rb.Subjects[0].APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
54+
test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User"))
55+
test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1"))
56+
}
57+
58+
func TestCreateUserClusterRoleBinding(t *testing.T) {
59+
60+
test := NewTest(t)
61+
62+
crole := &rbacv1.ClusterRole{
63+
TypeMeta: metav1.TypeMeta{
64+
APIVersion: rbacv1.SchemeGroupVersion.String(),
65+
Kind: "ClusterRole",
66+
},
67+
ObjectMeta: metav1.ObjectMeta{
68+
Name: "role1",
69+
},
70+
}
71+
72+
rb := CreateUserClusterRoleBinding(test, "user-1", crole)
73+
74+
test.Expect(rb).To(gomega.Not(gomega.BeNil()))
75+
test.Expect(rb.GenerateName).To(gomega.Equal("crb-"))
76+
77+
test.Expect(rb.RoleRef.APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
78+
test.Expect(rb.RoleRef.Kind).To(gomega.Equal("ClusterRole"))
79+
test.Expect(rb.RoleRef.Name).To(gomega.Equal("role1"))
80+
81+
test.Expect(rb.Subjects[0].APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group))
82+
test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User"))
83+
test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1"))
84+
}

0 commit comments

Comments
 (0)