@@ -10,6 +10,7 @@ import (
10
10
"k8s.io/apiserver/pkg/admission"
11
11
"k8s.io/apiserver/pkg/authentication/user"
12
12
kapi "k8s.io/kubernetes/pkg/api"
13
+ "k8s.io/kubernetes/pkg/apis/rbac"
13
14
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
14
15
//kcache "k8s.io/client-go/tools/cache"
15
16
"k8s.io/apimachinery/pkg/runtime"
@@ -22,6 +23,19 @@ import (
22
23
userapi "github.com/openshift/origin/pkg/user/apis/user"
23
24
)
24
25
26
+ type testCase struct {
27
+ name string
28
+ expectedErr string
29
+
30
+ object runtime.Object
31
+ oldObject runtime.Object
32
+ kind schema.GroupVersionKind
33
+ resource schema.GroupVersionResource
34
+ namespace string
35
+ subresource string
36
+ objects []runtime.Object
37
+ }
38
+
25
39
func TestAdmission (t * testing.T ) {
26
40
var (
27
41
userAlice = userapi.User {
@@ -79,18 +93,7 @@ func TestAdmission(t *testing.T) {
79
93
}
80
94
)
81
95
82
- testCases := []struct {
83
- name string
84
- expectedErr string
85
-
86
- object runtime.Object
87
- oldObject runtime.Object
88
- kind schema.GroupVersionKind
89
- resource schema.GroupVersionResource
90
- namespace string
91
- subresource string
92
- objects []runtime.Object
93
- }{
96
+ testCases := []testCase {
94
97
{
95
98
name : "ignore (allow) if subresource is nonempty" ,
96
99
object : & authorizationapi.RoleBinding {
@@ -127,7 +130,7 @@ func TestAdmission(t *testing.T) {
127
130
Name : "rolebinding" ,
128
131
},
129
132
Subjects : []kapi.ObjectReference {userAliceRef },
130
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
133
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
131
134
},
132
135
oldObject : & authorizationapi.RoleBinding {
133
136
ObjectMeta : metav1.ObjectMeta {
@@ -163,15 +166,15 @@ func TestAdmission(t *testing.T) {
163
166
systemgroupRef ,
164
167
systemuserRef ,
165
168
},
166
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
169
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
167
170
},
168
171
oldObject : & authorizationapi.RoleBinding {
169
172
ObjectMeta : metav1.ObjectMeta {
170
173
Namespace : "namespace" ,
171
174
Name : "rolebinding" ,
172
175
},
173
176
Subjects : []kapi.ObjectReference {},
174
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
177
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
175
178
},
176
179
kind : authorizationapi .Kind ("RoleBinding" ).WithVersion ("version" ),
177
180
resource : authorizationapi .Resource ("rolebindings" ).WithVersion ("version" ),
@@ -197,7 +200,7 @@ func TestAdmission(t *testing.T) {
197
200
groupRef ,
198
201
serviceaccountRef ,
199
202
},
200
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
203
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
201
204
},
202
205
oldObject : & authorizationapi.RoleBinding {
203
206
ObjectMeta : metav1.ObjectMeta {
@@ -209,7 +212,7 @@ func TestAdmission(t *testing.T) {
209
212
groupRef ,
210
213
serviceaccountRef ,
211
214
},
212
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
215
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
213
216
},
214
217
kind : authorizationapi .Kind ("RoleBinding" ).WithVersion ("version" ),
215
218
resource : authorizationapi .Resource ("rolebindings" ).WithVersion ("version" ),
@@ -246,15 +249,15 @@ func TestAdmission(t *testing.T) {
246
249
serviceaccountRef ,
247
250
groupRef ,
248
251
},
249
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
252
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
250
253
},
251
254
oldObject : & authorizationapi.RoleBinding {
252
255
ObjectMeta : metav1.ObjectMeta {
253
256
Namespace : "namespace" ,
254
257
Name : "rolebinding" ,
255
258
},
256
259
Subjects : []kapi.ObjectReference {},
257
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
260
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
258
261
},
259
262
kind : authorizationapi .Kind ("RoleBinding" ).WithVersion ("version" ),
260
263
resource : authorizationapi .Resource ("rolebindings" ).WithVersion ("version" ),
@@ -340,15 +343,15 @@ func TestAdmission(t *testing.T) {
340
343
Subjects : []kapi.ObjectReference {
341
344
userAliceRef ,
342
345
},
343
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
346
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
344
347
},
345
348
oldObject : & authorizationapi.RoleBinding {
346
349
ObjectMeta : metav1.ObjectMeta {
347
350
Namespace : "namespace" ,
348
351
Name : "rolebinding" ,
349
352
},
350
353
Subjects : []kapi.ObjectReference {},
351
- RoleRef : kapi.ObjectReference {Namespace : authorizationapi . PolicyName },
354
+ RoleRef : kapi.ObjectReference {Namespace : "namespace" },
352
355
},
353
356
kind : authorizationapi .Kind ("RoleBinding" ).WithVersion ("version" ),
354
357
resource : authorizationapi .Resource ("rolebindings" ).WithVersion ("version" ),
@@ -569,6 +572,40 @@ func TestAdmission(t *testing.T) {
569
572
stopCh := make (chan struct {})
570
573
defer close (stopCh )
571
574
575
+ // for each testCase that involves an authorizationapi rolebinding also
576
+ // automatically create a rbac rolebinding test case by converting the
577
+ // object
578
+ convTestCases := []testCase {}
579
+ for _ , tc := range testCases {
580
+ switch tc .object .(type ) {
581
+ case * authorizationapi.RoleBinding :
582
+ newObj := rbac.RoleBinding {}
583
+ oldObj := rbac.RoleBinding {}
584
+ if err := authorizationapi .Convert_authorization_RoleBinding_To_rbac_RoleBinding (tc .object .(* authorizationapi.RoleBinding ), & newObj , nil ); err != nil {
585
+ t .Errorf ("unexpected error converting role binding: %v" , err )
586
+ }
587
+ if err := authorizationapi .Convert_authorization_RoleBinding_To_rbac_RoleBinding (tc .oldObject .(* authorizationapi.RoleBinding ), & oldObj , nil ); err != nil {
588
+ t .Errorf ("unexpected error converting old role binding: %v" , err )
589
+ }
590
+
591
+ convtc := testCase {
592
+ name : "rbac: " + tc .name ,
593
+ expectedErr : tc .expectedErr ,
594
+ object : & newObj ,
595
+ oldObject : & oldObj ,
596
+ kind : rbac .Kind ("RoleBinding" ).WithVersion ("version" ),
597
+ resource : rbac .Resource ("rolebindings" ).WithVersion ("version" ),
598
+ namespace : tc .namespace ,
599
+ subresource : tc .subresource ,
600
+ objects : tc .objects ,
601
+ }
602
+ convTestCases = append (convTestCases , convtc )
603
+ default :
604
+ // ignore
605
+ }
606
+ }
607
+ testCases = append (testCases , convTestCases ... )
608
+
572
609
for _ , tc := range testCases {
573
610
kclientset := fake .NewSimpleClientset (otestclient .UpstreamObjects (tc .objects )... )
574
611
oclient := otestclient .NewSimpleFake (otestclient .OriginObjects (tc .objects )... )
@@ -592,7 +629,7 @@ func TestAdmission(t *testing.T) {
592
629
tc .oldObject ,
593
630
tc .kind ,
594
631
tc .namespace ,
595
- " name" ,
632
+ tc . name ,
596
633
tc .resource ,
597
634
tc .subresource ,
598
635
admission .Create ,
0 commit comments