@@ -1110,6 +1110,112 @@ var _ = Describe("Resource creation and cleanup tests", func() {
11101110 })
11111111 })
11121112
1113+ Context ("Verify correct RBAC permissions are assigned while switching between namespace and cluster scoped Rollouts" , func () {
1114+ var (
1115+ ctx context.Context
1116+ a v1alpha1.RolloutManager
1117+ r * RolloutManagerReconciler
1118+ )
1119+
1120+ BeforeEach (func () {
1121+ ctx = context .Background ()
1122+ a = * makeTestRolloutManager ()
1123+ r = makeTestReconciler (& a )
1124+ err := createNamespace (r , a .Namespace )
1125+ Expect (err ).ToNot (HaveOccurred ())
1126+ })
1127+
1128+ It ("Should delete existing Role when ClusterRole is reconciled" , func () {
1129+ By ("Reconcile Role." )
1130+ role , err := r .reconcileRolloutsRole (ctx , a )
1131+ Expect (err ).ToNot (HaveOccurred ())
1132+
1133+ By ("Verify Role is created" )
1134+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (role ), role )).To (Succeed ())
1135+
1136+ By ("Reconcile ClusterRole" )
1137+ clusterRole , err := r .reconcileRolloutsClusterRole (ctx , a )
1138+ Expect (err ).ToNot (HaveOccurred ())
1139+
1140+ By ("Verify ClusterRole is created" )
1141+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRole ), clusterRole )).To (Succeed ())
1142+
1143+ By ("Verify existing Role is deleted" )
1144+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (role ), role )).To (HaveOccurred ())
1145+ })
1146+
1147+ It ("Should delete existing ClusterRole when Role is reconciled" , func () {
1148+
1149+ By ("Reconcile ClusterRole" )
1150+ clusterRole , err := r .reconcileRolloutsClusterRole (ctx , a )
1151+ Expect (err ).ToNot (HaveOccurred ())
1152+
1153+ By ("Verify ClusterRole is created" )
1154+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRole ), clusterRole )).To (Succeed ())
1155+
1156+ By ("Reconcile Role." )
1157+ role , err := r .reconcileRolloutsRole (ctx , a )
1158+ Expect (err ).ToNot (HaveOccurred ())
1159+
1160+ By ("Verify Role is created" )
1161+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (role ), role )).To (Succeed ())
1162+
1163+ By ("Verify existing ClusterRole is deleted" )
1164+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRole ), clusterRole )).To (HaveOccurred ())
1165+ })
1166+
1167+ It ("Should delete existing RoleBinding when ClusterRoleBinding is reconciled" , func () {
1168+
1169+ By ("Reconcile RoleBinding" )
1170+ sa , err := r .reconcileRolloutsServiceAccount (ctx , a )
1171+ Expect (err ).ToNot (HaveOccurred ())
1172+ role , err := r .reconcileRolloutsRole (ctx , a )
1173+ Expect (err ).ToNot (HaveOccurred ())
1174+ Expect (r .reconcileRolloutsRoleBinding (ctx , a , role , sa )).To (Succeed ())
1175+
1176+ By ("Verify RoleBinding is created" )
1177+ roleBinding := & rbacv1.RoleBinding {ObjectMeta : metav1.ObjectMeta {Name : DefaultArgoRolloutsResourceName , Namespace : a .Namespace }}
1178+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (roleBinding ), roleBinding )).To (Succeed ())
1179+
1180+ By ("Reconcile ClusterRoleBinding" )
1181+ clusterRole , err := r .reconcileRolloutsClusterRole (ctx , a )
1182+ Expect (err ).ToNot (HaveOccurred ())
1183+ Expect (r .reconcileRolloutsClusterRoleBinding (ctx , clusterRole , sa , a )).To (Succeed ())
1184+
1185+ By ("Verify ClusterRoleBinding is created" )
1186+ clusterRoleBinding := & rbacv1.ClusterRoleBinding {ObjectMeta : metav1.ObjectMeta {Name : DefaultArgoRolloutsResourceName }}
1187+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRoleBinding ), clusterRoleBinding )).To (Succeed ())
1188+
1189+ By ("Verify RoleBinding is deleted" )
1190+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (roleBinding ), roleBinding )).To (HaveOccurred ())
1191+ })
1192+
1193+ It ("Should delete existing ClusterRoleBinding when RoleBinding is reconciled" , func () {
1194+
1195+ By ("Reconcile ClusterRoleBinding" )
1196+ sa , err := r .reconcileRolloutsServiceAccount (ctx , a )
1197+ Expect (err ).ToNot (HaveOccurred ())
1198+ clusterRole , err := r .reconcileRolloutsClusterRole (ctx , a )
1199+ Expect (err ).ToNot (HaveOccurred ())
1200+ Expect (r .reconcileRolloutsClusterRoleBinding (ctx , clusterRole , sa , a )).To (Succeed ())
1201+
1202+ By ("Verify ClusterRoleBinding is created" )
1203+ clusterRoleBinding := & rbacv1.ClusterRoleBinding {ObjectMeta : metav1.ObjectMeta {Name : DefaultArgoRolloutsResourceName }}
1204+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRoleBinding ), clusterRoleBinding )).To (Succeed ())
1205+
1206+ By ("Reconcile RoleBinding" )
1207+ role , err := r .reconcileRolloutsRole (ctx , a )
1208+ Expect (err ).ToNot (HaveOccurred ())
1209+ Expect (r .reconcileRolloutsRoleBinding (ctx , a , role , sa )).To (Succeed ())
1210+
1211+ By ("Verify RoleBinding is created" )
1212+ roleBinding := & rbacv1.RoleBinding {ObjectMeta : metav1.ObjectMeta {Name : DefaultArgoRolloutsResourceName , Namespace : a .Namespace }}
1213+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (roleBinding ), roleBinding )).To (Succeed ())
1214+
1215+ By ("Verify ClusterRoleBinding is deleted" )
1216+ Expect (r .Client .Get (ctx , client .ObjectKeyFromObject (clusterRole ), clusterRole )).To (HaveOccurred ())
1217+ })
1218+ })
11131219})
11141220
11151221func serviceMonitor () * monitoringv1.ServiceMonitor {
0 commit comments