@@ -24,6 +24,7 @@ import (
2424 rbacv1 "k8s.io/api/rbac/v1"
2525 apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2626 apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
27+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728 "k8s.io/apimachinery/pkg/labels"
2829 "sigs.k8s.io/controller-runtime/pkg/client"
2930
@@ -65,6 +66,8 @@ var _ = Describe("apply functions", func() {
6566 saName1 = "service-account-1"
6667 roleName1 = "role-1"
6768 cRoleName1 = "cluster-role-1"
69+ cRoleName2 = "cluster-role-2"
70+ cRoleName3 = "cluster-role-3"
6871 )
6972
7073 BeforeEach (func () {
@@ -79,7 +82,8 @@ var _ = Describe("apply functions", func() {
7982 rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
8083 perms := []client.Object {newRole (roleName1 , rules ... )}
8184 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 ())
8387 Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
8488 {ServiceAccountName : saName1 , Rules : rules },
8589 }))
@@ -90,7 +94,49 @@ var _ = Describe("apply functions", func() {
9094 rules := []rbacv1.PolicyRule {{Verbs : []string {"create" }}}
9195 perms := []client.Object {newClusterRole (cRoleName1 , rules ... )}
9296 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 ())
94140 Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
95141 {ServiceAccountName : saName1 , Rules : rules },
96142 }))
@@ -128,8 +174,10 @@ var _ = Describe("apply functions", func() {
128174 newClusterRoleBinding ("cluster-role-binding-2" , newClusterRoleRef (cRoleName2 ), newServiceAccountSubject (extraSAName )),
129175 newClusterRoleBinding ("cluster-role-binding-3" , newClusterRoleRef (cRoleName3 ), newServiceAccountSubject (extraSAName )),
130176 }
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 ())
133181 Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {
134182 {ServiceAccountName : saName1 , Rules : rules },
135183 {ServiceAccountName : extraSAName , Rules : rules },
@@ -146,14 +194,16 @@ var _ = Describe("apply functions", func() {
146194 c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
147195 c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
148196 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 ())
150199 Expect (strategy .Permissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
151200 })
152201 It ("adds no ClusterPermissions to the CSV deployment strategy" , func () {
153202 c .Deployments = []appsv1.Deployment {newDeploymentWithServiceAccount (depName1 , saName1 )}
154203 c .ServiceAccounts = []corev1.ServiceAccount {newServiceAccount (saName1 )}
155204 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 ())
157207 Expect (strategy .ClusterPermissions ).To (Equal ([]operatorsv1alpha1.StrategyDeploymentPermissions {}))
158208 })
159209 })
0 commit comments