@@ -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,8 @@ 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"
68
71
)
69
72
70
73
BeforeEach (func () {
@@ -79,7 +82,8 @@ var _ = Describe("apply functions", func() {
79
82
rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
80
83
perms := []client.Object {newRole (roleName1 , rules ... )}
81
84
c .RoleBindings = []rbacv1.RoleBinding {newRoleBinding ("role-binding" , newRoleRef (roleName1 ), newServiceAccountSubject (saName1 ))}
82
- applyRoles (c , perms , strategy , nil )
85
+ err := applyRoles (c , perms , strategy , nil )
86
+ Expect (err ).NotTo (HaveOccurred ())
83
87
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
84
88
{ServiceAccountName : saName1 , Rules : rules },
85
89
}))
@@ -90,7 +94,49 @@ var _ = Describe("apply functions", func() {
90
94
rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
91
95
perms := []client.Object {newClusterRole (cRoleName1 , rules ... )}
92
96
c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
93
- applyClusterRoles (c , perms , strategy , nil )
97
+ err := applyClusterRoles (c , perms , strategy , nil )
98
+ Expect (err ).NotTo (HaveOccurred ())
99
+ Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
100
+ {ServiceAccountName : saName1 , Rules : rules },
101
+ }))
102
+ })
103
+ It ("adds rules from aggregated ClusterRoles eliminating duplicates to the CSV deployment strategy" , func () {
104
+ c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
105
+ c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
106
+ rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
107
+ var emptyRules []rbacv1.PolicyRule
108
+ perms := []client.Object {
109
+ func () * rbacv1.ClusterRole {
110
+ cr := newClusterRole (cRoleName1 , emptyRules ... )
111
+ cr .AggregationRule = & rbacv1.AggregationRule {
112
+ ClusterRoleSelectors : []metav1.LabelSelector {
113
+ {
114
+ MatchLabels : map [string ]string {
115
+ "aggregate-to-cluster-role-1" : "true" ,
116
+ },
117
+ },
118
+ },
119
+ }
120
+ return cr
121
+ }(),
122
+ func () * rbacv1.ClusterRole {
123
+ cr := newClusterRole (cRoleName2 , rules ... )
124
+ cr .Labels = map [string ]string {
125
+ "aggregate-to-cluster-role-1" : "true" ,
126
+ }
127
+ return cr
128
+ }(),
129
+ func () * rbacv1.ClusterRole {
130
+ cr := newClusterRole (cRoleName3 , rules ... )
131
+ cr .Labels = map [string ]string {
132
+ "aggregate-to-cluster-role-1" : "true" ,
133
+ }
134
+ return cr
135
+ }(),
136
+ }
137
+ c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
138
+ err := applyClusterRoles (c , perms , strategy , nil )
139
+ Expect (err ).NotTo (HaveOccurred ())
94
140
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
95
141
{ServiceAccountName : saName1 , Rules : rules },
96
142
}))
@@ -128,8 +174,10 @@ var _ = Describe("apply functions", func() {
128
174
newClusterRoleBinding ("cluster-role-binding-2" , newClusterRoleRef (cRoleName2 ), newServiceAccountSubject (extraSAName )),
129
175
newClusterRoleBinding ("cluster-role-binding-3" , newClusterRoleRef (cRoleName3 ), newServiceAccountSubject (extraSAName )),
130
176
}
131
- applyRoles (c , perms , strategy , []string {extraSAName })
132
- applyClusterRoles (c , cperms , strategy , []string {extraSAName })
177
+ err := applyRoles (c , perms , strategy , []string {extraSAName })
178
+ Expect (err ).NotTo (HaveOccurred ())
179
+ err = applyClusterRoles (c , cperms , strategy , []string {extraSAName })
180
+ Expect (err ).NotTo (HaveOccurred ())
133
181
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
134
182
{ServiceAccountName : saName1 , Rules : rules },
135
183
{ServiceAccountName : extraSAName , Rules : rules },
@@ -146,14 +194,16 @@ var _ = Describe("apply functions", func() {
146
194
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
147
195
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
148
196
c .RoleBindings = []rbacv1.RoleBinding {newRoleBinding ("role-binding" , newRoleRef (roleName1 ), newServiceAccountSubject (saName1 ))}
149
- applyRoles (c , nil , strategy , nil )
197
+ err := applyRoles (c , nil , strategy , nil )
198
+ Expect (err ).NotTo (HaveOccurred ())
150
199
Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
151
200
})
152
201
It ("adds no ClusterPermissions to the CSV deployment strategy" , func () {
153
202
c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
154
203
c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
155
204
c .ClusterRoleBindings = []rbacv1.ClusterRoleBinding {newClusterRoleBinding ("cluster-role-binding" , newClusterRoleRef (cRoleName1 ), newServiceAccountSubject (saName1 ))}
156
- applyClusterRoles (c , nil , strategy , nil )
205
+ err := applyClusterRoles (c , nil , strategy , nil )
206
+ Expect (err ).NotTo (HaveOccurred ())
157
207
Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
158
208
})
159
209
})
0 commit comments