@@ -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,52 @@ 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
+ c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
142
+ err := applyClusterRoles (c , perms , strategy , nil )
143
+ Expect (err ).NotTo (HaveOccurred ())
94
144
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
95
145
{ServiceAccountName : saName1 , Rules : rules },
96
146
}))
@@ -128,8 +178,10 @@ var _ = Describe("apply functions", func() {
128
178
newClusterRoleBinding ("cluster-role-binding-2" , newClusterRoleRef (cRoleName2 ), newServiceAccountSubject (extraSAName )),
129
179
newClusterRoleBinding ("cluster-role-binding-3" , newClusterRoleRef (cRoleName3 ), newServiceAccountSubject (extraSAName )),
130
180
}
131
- applyRoles (c , perms , strategy , []string {extraSAName })
132
- applyClusterRoles (c , cperms , strategy , []string {extraSAName })
181
+ err := applyRoles (c , perms , strategy , []string {extraSAName })
182
+ Expect (err ).NotTo (HaveOccurred ())
183
+ err = applyClusterRoles (c , cperms , strategy , []string {extraSAName })
184
+ Expect (err ).NotTo (HaveOccurred ())
133
185
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
134
186
{ServiceAccountName : saName1 , Rules : rules },
135
187
{ServiceAccountName : extraSAName , Rules : rules },
@@ -146,14 +198,16 @@ var _ = Describe("apply functions", func() {
146
198
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
147
199
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
148
200
c .RoleBindings = []rbacv1.RoleBinding {newRoleBinding ("role-binding" , newRoleRef (roleName1 ), newServiceAccountSubject (saName1 ))}
149
- applyRoles (c , nil , strategy , nil )
201
+ err := applyRoles (c , nil , strategy , nil )
202
+ Expect (err ).NotTo (HaveOccurred ())
150
203
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
151
204
})
152
205
It ("adds no ClusterPermissions to the CSV deployment strategy" , func () {
153
206
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
154
207
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
155
208
c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
156
- applyClusterRoles (c , nil , strategy , nil )
209
+ err := applyClusterRoles (c , nil , strategy , nil )
210
+ Expect (err ).NotTo (HaveOccurred ())
157
211
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
158
212
})
159
213
})
0 commit comments