@@ -24,6 +24,7 @@ import (
24
24
rbacv1 "k8s.io/api/rbac/v1"
25
25
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
26
26
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
27
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
28
"k8s.io/apimachinery/pkg/labels"
28
29
"sigs.k8s.io/controller-runtime/pkg/client"
29
30
@@ -65,6 +66,9 @@ var _ = Describe("apply functions", func() {
65
66
saName1 = "service-account-1"
66
67
roleName1 = "role-1"
67
68
cRoleName1 = "cluster-role-1"
69
+ cRoleName2 = "cluster-role-2"
70
+ cRoleName3 = "cluster-role-3"
71
+ cRoleName4 = "cluster-role-4"
68
72
)
69
73
70
74
BeforeEach (func () {
@@ -79,7 +83,8 @@ var _ = Describe("apply functions", func() {
79
83
rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
80
84
perms := []client.Object {newRole (roleName1 , rules ... )}
81
85
c .RoleBindings = []rbacv1.RoleBinding {newRoleBinding ("role-binding" , newRoleRef (roleName1 ), newServiceAccountSubject (saName1 ))}
82
- applyRoles (c , perms , strategy , nil )
86
+ err := applyRoles (c , perms , strategy , nil )
87
+ Expect (err ).NotTo (HaveOccurred ())
83
88
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
84
89
{ServiceAccountName : saName1 , Rules : rules },
85
90
}))
@@ -90,7 +95,55 @@ var _ = Describe("apply functions", func() {
90
95
rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
91
96
perms := []client.Object {newClusterRole (cRoleName1 , rules ... )}
92
97
c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
93
- applyClusterRoles (c , perms , strategy , nil )
98
+ err := applyClusterRoles (c , perms , strategy , nil )
99
+ Expect (err ).NotTo (HaveOccurred ())
100
+ Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
101
+ {ServiceAccountName : saName1 , Rules : rules },
102
+ }))
103
+ })
104
+ It ("adds rules from aggregated ClusterRoles eliminating duplicates to the CSV deployment strategy" , func () {
105
+ c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
106
+ c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
107
+ rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}, {Verbs : []string {"update" }}}
108
+ var emptyRules []rbacv1.PolicyRule
109
+ perms := []client.Object {
110
+ func () * rbacv1.ClusterRole {
111
+ cr := newClusterRole (cRoleName1 , emptyRules ... )
112
+ cr .AggregationRule = & rbacv1.AggregationRule {
113
+ ClusterRoleSelectors : []metav1.LabelSelector {
114
+ {
115
+ MatchLabels : map [string ]string {
116
+ "aggregate-to-cluster-role-1" : "true" ,
117
+ },
118
+ },
119
+ },
120
+ }
121
+ return cr
122
+ }(),
123
+ func () * rbacv1.ClusterRole {
124
+ cr := newClusterRole (cRoleName2 , rules ... )
125
+ cr .Labels = map [string ]string {
126
+ "aggregate-to-cluster-role-1" : "true" ,
127
+ }
128
+ return cr
129
+ }(),
130
+ func () * rbacv1.ClusterRole {
131
+ cr := newClusterRole (cRoleName3 , rules ... )
132
+ cr .Labels = map [string ]string {
133
+ "aggregate-to-cluster-role-1" : "true" ,
134
+ }
135
+ return cr
136
+ }(),
137
+ // ClusterRole not bound to any ServiceAccount, nor matching any ClusterRule AggregationRule,
138
+ // it shouldn't land in strategy ClusterPermissions.
139
+ newClusterRole (cRoleName4 , []rbacv1.PolicyRule {{Verbs : []string {"delete" }}}... ),
140
+ }
141
+ for _ , cr := range perms {
142
+ c .ClusterRoles = append (c .ClusterRoles , * cr .(* rbacv1.ClusterRole ))
143
+ }
144
+ c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
145
+ err := applyClusterRoles (c , perms , strategy , nil )
146
+ Expect (err ).NotTo (HaveOccurred ())
94
147
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
95
148
{ServiceAccountName : saName1 , Rules : rules },
96
149
}))
@@ -128,8 +181,10 @@ var _ = Describe("apply functions", func() {
128
181
newClusterRoleBinding ("cluster-role-binding-2" , newClusterRoleRef (cRoleName2 ), newServiceAccountSubject (extraSAName )),
129
182
newClusterRoleBinding ("cluster-role-binding-3" , newClusterRoleRef (cRoleName3 ), newServiceAccountSubject (extraSAName )),
130
183
}
131
- applyRoles (c , perms , strategy , []string {extraSAName })
132
- applyClusterRoles (c , cperms , strategy , []string {extraSAName })
184
+ err := applyRoles (c , perms , strategy , []string {extraSAName })
185
+ Expect (err ).NotTo (HaveOccurred ())
186
+ err = applyClusterRoles (c , cperms , strategy , []string {extraSAName })
187
+ Expect (err ).NotTo (HaveOccurred ())
133
188
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
134
189
{ServiceAccountName : saName1 , Rules : rules },
135
190
{ServiceAccountName : extraSAName , Rules : rules },
@@ -146,14 +201,16 @@ var _ = Describe("apply functions", func() {
146
201
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
147
202
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
148
203
c .RoleBindings = []rbacv1.RoleBinding {newRoleBinding ("role-binding" , newRoleRef (roleName1 ), newServiceAccountSubject (saName1 ))}
149
- applyRoles (c , nil , strategy , nil )
204
+ err := applyRoles (c , nil , strategy , nil )
205
+ Expect (err ).NotTo (HaveOccurred ())
150
206
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
151
207
})
152
208
It ("adds no ClusterPermissions to the CSV deployment strategy" , func () {
153
209
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
154
210
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
155
211
c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
156
- applyClusterRoles (c , nil , strategy , nil )
212
+ err := applyClusterRoles (c , nil , strategy , nil )
213
+ Expect (err ).NotTo (HaveOccurred ())
157
214
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
158
215
})
159
216
})
0 commit comments