4
4
"context"
5
5
"fmt"
6
6
"hash/fnv"
7
+ "k8s.io/apiserver/pkg/storage/names"
7
8
"reflect"
8
9
"strings"
9
10
@@ -978,38 +979,34 @@ func (a *Operator) updateNamespaceList(op *operatorsv1.OperatorGroup) ([]string,
978
979
}
979
980
980
981
func (a * Operator ) ensureOpGroupClusterRole (op * operatorsv1.OperatorGroup , suffix string , apis cache.APISet ) error {
982
+ // create target cluster role spec
981
983
clusterRole := & rbacv1.ClusterRole {
982
984
ObjectMeta : metav1.ObjectMeta {
983
- Name : strings .Join ([]string {op .GetName (), suffix }, "-" ),
984
- },
985
- }
986
- var selectors []metav1.LabelSelector
987
- for api := range apis {
988
- aggregationLabel , err := aggregationLabelFromAPIKey (api , suffix )
989
- if err != nil {
990
- return err
991
- }
992
- selectors = append (selectors , metav1.LabelSelector {
993
- MatchLabels : map [string ]string {
994
- aggregationLabel : "true" ,
985
+ Name : names .SimpleNameGenerator .GenerateName (fmt .Sprintf ("olm.operatorgroup.%s-" , suffix )),
986
+ Labels : map [string ]string {
987
+ ownerutil .OlmOperatorGroupClusterRoleLevel : suffix ,
995
988
},
996
- })
997
- }
998
- if len (selectors ) > 0 {
999
- clusterRole .AggregationRule = & rbacv1.AggregationRule {
1000
- ClusterRoleSelectors : selectors ,
1001
- }
989
+ },
1002
990
}
1003
- err := ownerutil .AddOwnerLabels (clusterRole , op )
991
+
992
+ var err error
993
+ clusterRole .AggregationRule , err = a .getClusterRoleAggregationRule (apis , suffix )
1004
994
if err != nil {
1005
995
return err
1006
996
}
1007
997
1008
- existingRole , err := a .lister .RbacV1 ().ClusterRoleLister ().Get (clusterRole .Name )
1009
- if err != nil && ! apierrors .IsNotFound (err ) {
998
+ if err := ownerutil .AddOwnerLabels (clusterRole , op ); err != nil {
1010
999
return err
1011
1000
}
1012
- if apierrors .IsNotFound (err ) {
1001
+
1002
+ // get existing cluster role for this level (suffix: admin, edit, view))
1003
+ existingClusterRoleList , err := a .lister .RbacV1 ().ClusterRoleLister ().List (labels .SelectorFromSet (ownerutil .ClusterRoleByOwnerAndLevel (op , suffix )))
1004
+ if err != nil {
1005
+ return err
1006
+ }
1007
+
1008
+ var existingRole * rbacv1.ClusterRole
1009
+ if len (existingClusterRoleList ) == 0 {
1013
1010
existingRole , err = a .opClient .KubernetesInterface ().RbacV1 ().ClusterRoles ().Create (context .TODO (), clusterRole , metav1.CreateOptions {})
1014
1011
if err == nil {
1015
1012
return nil
@@ -1018,6 +1015,10 @@ func (a *Operator) ensureOpGroupClusterRole(op *operatorsv1.OperatorGroup, suffi
1018
1015
a .logger .WithError (err ).Errorf ("Create cluster role failed: %v" , clusterRole )
1019
1016
return err
1020
1017
}
1018
+ } else if len (existingClusterRoleList ) == 1 {
1019
+ existingRole = existingClusterRoleList [0 ].DeepCopy ()
1020
+ } else {
1021
+ return fmt .Errorf ("found multiple cluster roles at level '%s' for operator group: '%s'" , suffix , op .Name )
1021
1022
}
1022
1023
1023
1024
if existingRole != nil && labels .Equals (existingRole .Labels , clusterRole .Labels ) && reflect .DeepEqual (existingRole .AggregationRule , clusterRole .AggregationRule ) {
@@ -1031,6 +1032,38 @@ func (a *Operator) ensureOpGroupClusterRole(op *operatorsv1.OperatorGroup, suffi
1031
1032
return nil
1032
1033
}
1033
1034
1035
+ func (a * Operator ) getClusterRoleAggregationRule (apis cache.APISet , suffix string ) (* rbacv1.AggregationRule , error ) {
1036
+ var selectors []metav1.LabelSelector
1037
+ for api := range apis {
1038
+ aggregationLabel , err := aggregationLabelFromAPIKey (api , suffix )
1039
+ if err != nil {
1040
+ return nil , err
1041
+ }
1042
+ selectors = append (selectors , metav1.LabelSelector {
1043
+ MatchLabels : map [string ]string {
1044
+ aggregationLabel : "true" ,
1045
+ },
1046
+ })
1047
+ }
1048
+ if len (selectors ) > 0 {
1049
+ return & rbacv1.AggregationRule {
1050
+ ClusterRoleSelectors : selectors ,
1051
+ }, nil
1052
+ }
1053
+ return nil , nil
1054
+ }
1055
+
1056
+ func (a * Operator ) clusterRoleExistsAndIsOwnedBy (roleName string , owner ownerutil.Owner ) (bool , error ) {
1057
+ role , err := a .lister .RbacV1 ().ClusterRoleLister ().Get (roleName )
1058
+ if err != nil && ! apierrors .IsNotFound (err ) {
1059
+ return false , err
1060
+ }
1061
+ if apierrors .IsNotFound (err ) {
1062
+ return false , nil
1063
+ }
1064
+ return ownerutil .IsOwnedBy (role , owner ), nil
1065
+ }
1066
+
1034
1067
func (a * Operator ) ensureOpGroupClusterRoles (op * operatorsv1.OperatorGroup , apis cache.APISet ) error {
1035
1068
for _ , suffix := range Suffices {
1036
1069
if err := a .ensureOpGroupClusterRole (op , suffix , apis ); err != nil {
0 commit comments