@@ -435,7 +435,7 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
435
435
return
436
436
}
437
437
438
- _ , err = c .ensureRole (cmRef )
438
+ _ , err = c .ensureRole (cmRef , c . getRolePolicyRules ( cmRef ) )
439
439
if err != nil {
440
440
return
441
441
}
@@ -610,27 +610,13 @@ func (c *ConfigMapUnpacker) ensureJob(cmRef *corev1.ObjectReference, bundlePath
610
610
return
611
611
}
612
612
613
- func (c * ConfigMapUnpacker ) ensureRole (cmRef * corev1.ObjectReference ) (role * rbacv1.Role , err error ) {
613
+ func (c * ConfigMapUnpacker ) ensureRole (cmRef * corev1.ObjectReference , policyRules []rbacv1. PolicyRule ) (role * rbacv1.Role , err error ) {
614
614
if cmRef == nil {
615
615
return nil , fmt .Errorf ("configmap reference is nil" )
616
616
}
617
617
618
- rule := rbacv1.PolicyRule {
619
- APIGroups : []string {
620
- "" ,
621
- },
622
- Verbs : []string {
623
- "create" , "get" , "update" ,
624
- },
625
- Resources : []string {
626
- "configmaps" ,
627
- },
628
- ResourceNames : []string {
629
- cmRef .Name ,
630
- },
631
- }
632
618
fresh := & rbacv1.Role {
633
- Rules : []rbacv1. PolicyRule { rule } ,
619
+ Rules : policyRules ,
634
620
}
635
621
fresh .SetNamespace (cmRef .Namespace )
636
622
fresh .SetName (cmRef .Name )
@@ -646,19 +632,43 @@ func (c *ConfigMapUnpacker) ensureRole(cmRef *corev1.ObjectReference) (role *rba
646
632
}
647
633
648
634
// Add the policy rule if necessary
649
- for _ , existing := range role .Rules {
650
- if equality .Semantic .DeepDerivative (rule , existing ) {
651
- return
635
+ var ruleDiff []rbacv1.PolicyRule
636
+ for _ , proposed := range policyRules {
637
+ if ! containsRule (role .Rules , proposed ) {
638
+ ruleDiff = append (ruleDiff , proposed )
652
639
}
653
640
}
641
+
654
642
role = role .DeepCopy ()
655
- role .Rules = append (role .Rules , rule )
643
+ role .Rules = append (role .Rules , ruleDiff ... )
656
644
657
645
role , err = c .client .RbacV1 ().Roles (role .GetNamespace ()).Update (context .TODO (), role , metav1.UpdateOptions {})
658
646
659
647
return
660
648
}
661
649
650
+ // getRolePolicyRules returns the set of policy rules used by the role attached to the
651
+ // bundle unpacker service account. This method lends itself to easier downstream patching when additional
652
+ // policy rules are required, e.g. for Openshift SCC
653
+ func (c * ConfigMapUnpacker ) getRolePolicyRules (cmRef * corev1.ObjectReference ) []rbacv1.PolicyRule {
654
+ return []rbacv1.PolicyRule {
655
+ {
656
+ APIGroups : []string {
657
+ "" ,
658
+ },
659
+ Verbs : []string {
660
+ "get" , "update" ,
661
+ },
662
+ Resources : []string {
663
+ "configmaps" ,
664
+ },
665
+ ResourceNames : []string {
666
+ cmRef .Name ,
667
+ },
668
+ },
669
+ }
670
+ }
671
+
662
672
func (c * ConfigMapUnpacker ) ensureRoleBinding (cmRef * corev1.ObjectReference ) (roleBinding * rbacv1.RoleBinding , err error ) {
663
673
fresh := & rbacv1.RoleBinding {
664
674
Subjects : []rbacv1.Subject {
@@ -738,3 +748,12 @@ func getCondition(job *batchv1.Job, conditionType batchv1.JobConditionType) (con
738
748
}
739
749
return
740
750
}
751
+
752
+ func containsRule (rules []rbacv1.PolicyRule , rule rbacv1.PolicyRule ) bool {
753
+ for _ , r := range rules {
754
+ if equality .Semantic .DeepDerivative (r , rule ) {
755
+ return true
756
+ }
757
+ }
758
+ return false
759
+ }
0 commit comments