@@ -7,10 +7,10 @@ import (
7
7
8
8
rbacv1 "k8s.io/api/rbac/v1"
9
9
"k8s.io/apimachinery/pkg/runtime/schema"
10
- "k8s.io/utils/ptr"
11
10
"sigs.k8s.io/controller-runtime/pkg/client"
12
11
13
- slicesutil "github.com/operator-framework/operator-controller/internal/shared/util/filter"
12
+ "github.com/operator-framework/operator-controller/internal/shared/util/filter"
13
+ slicesutil "github.com/operator-framework/operator-controller/internal/shared/util/slices"
14
14
)
15
15
16
16
var (
@@ -45,21 +45,16 @@ var (
45
45
}
46
46
)
47
47
48
- // GenerateResourceManagerClusterRole generates a ClusterRole with permissions to manage objs resources. The
48
+ // GenerateResourceManagerClusterRolePerms generates a ClusterRole permissions to manage objs resources. The
49
49
// permissions also aggregate any permissions from any ClusterRoles in objs allowing the holder to also assign
50
50
// the RBAC therein to another service account. Note: assumes objs have been created by convert.Convert.
51
- // The returned ClusterRole will not have set .metadata.name
52
- func GenerateResourceManagerClusterRole (objs []client.Object ) * rbacv1.ClusterRole {
53
- rules := slices .Concat (
51
+ func GenerateResourceManagerClusterRolePerms (objs []client.Object ) []rbacv1.PolicyRule {
52
+ return slices .Concat (
54
53
// cluster scoped resource creation and management rules
55
- generatePolicyRules (slicesutil .Filter (objs , isClusterScopedResource )),
54
+ generatePolicyRules (filter .Filter (objs , isClusterScopedResource )),
56
55
// controller rbac scope
57
- collectRBACResourcePolicyRules (slicesutil .Filter (objs , slicesutil .And (isGeneratedResource , isOfKind ("ClusterRole" )))),
56
+ collectRBACResourcePolicyRules (filter .Filter (objs , filter .And (isGeneratedResource , isOfKind ("ClusterRole" )))),
58
57
)
59
- if len (rules ) == 0 {
60
- return nil
61
- }
62
- return ptr .To (newClusterRole ("" , rules ))
63
58
}
64
59
65
60
// GenerateClusterExtensionFinalizerPolicyRule generates a policy rule that allows the holder to update
@@ -73,26 +68,27 @@ func GenerateClusterExtensionFinalizerPolicyRule(clusterExtensionName string) rb
73
68
}
74
69
}
75
70
76
- // GenerateResourceManagerRoles generates one or more Roles with permissions to manage objs resources in their
71
+ // GenerateResourceManagerRolePerms generates role permissions to manage objs resources in their
77
72
// namespaces. The permissions also include any permissions defined in any Roles in objs within the namespace, allowing
78
73
// the holder to also assign the RBAC therein to another service account.
79
74
// Note: currently assumes objs have been created by convert.Convert.
80
75
// The returned Roles will not have set .metadata.name
81
- func GenerateResourceManagerRoles (objs []client.Object ) []* rbacv1.Role {
82
- return mapToSlice (slicesutil .GroupBy (slicesutil .Filter (objs , isNamespaceScopedResource ), namespaceName ), generateRole )
83
- }
84
-
85
- func generateRole (namespace string , namespaceObjs []client.Object ) * rbacv1.Role {
86
- return ptr .To (newRole (
87
- namespace ,
88
- "" ,
89
- slices .Concat (
90
- // namespace scoped resource creation and management rules
91
- generatePolicyRules (namespaceObjs ),
92
- // controller rbac scope
93
- collectRBACResourcePolicyRules (slicesutil .Filter (namespaceObjs , slicesutil .And (isOfKind ("Role" ), isGeneratedResource ))),
94
- ),
95
- ))
76
+ func GenerateResourceManagerRolePerms (objs []client.Object ) map [string ][]rbacv1.PolicyRule {
77
+ out := map [string ][]rbacv1.PolicyRule {}
78
+ namespaceScopedObjs := filter .Filter (objs , isNamespaceScopedResource )
79
+ for _ , obj := range namespaceScopedObjs {
80
+ namespace := obj .GetNamespace ()
81
+ if _ , ok := out [namespace ]; ! ok {
82
+ objsInNamespace := filter .Filter (namespaceScopedObjs , isInNamespace (namespace ))
83
+ out [namespace ] = slices .Concat (
84
+ // namespace scoped resource creation and management rules
85
+ generatePolicyRules (objsInNamespace ),
86
+ // controller rbac scope
87
+ collectRBACResourcePolicyRules (filter .Filter (objsInNamespace , filter .And (isOfKind ("Role" ), isGeneratedResource ))),
88
+ )
89
+ }
90
+ }
91
+ return out
96
92
}
97
93
98
94
func generatePolicyRules (objs []client.Object ) []rbacv1.PolicyRule {
@@ -143,7 +139,7 @@ func isNamespaceScopedResource(o client.Object) bool {
143
139
return slices .Contains (namespaceScopedResources , o .GetObjectKind ().GroupVersionKind ().Kind )
144
140
}
145
141
146
- func isOfKind (kind string ) slicesutil .Predicate [client.Object ] {
142
+ func isOfKind (kind string ) filter .Predicate [client.Object ] {
147
143
return func (o client.Object ) bool {
148
144
return o .GetObjectKind ().GroupVersionKind ().Kind == kind
149
145
}
@@ -155,12 +151,14 @@ func isGeneratedResource(o client.Object) bool {
155
151
return ok
156
152
}
157
153
158
- func groupKind (obj client.Object ) schema.GroupKind {
159
- return obj .GetObjectKind ().GroupVersionKind ().GroupKind ()
154
+ func isInNamespace (namespace string ) filter.Predicate [client.Object ] {
155
+ return func (o client.Object ) bool {
156
+ return o .GetNamespace () == namespace
157
+ }
160
158
}
161
159
162
- func namespaceName (obj client.Object ) string {
163
- return obj .GetNamespace ()
160
+ func groupKind (obj client.Object ) schema. GroupKind {
161
+ return obj .GetObjectKind (). GroupVersionKind (). GroupKind ()
164
162
}
165
163
166
164
func toResourceName (o client.Object ) string {
0 commit comments