Skip to content

Commit 4c27c10

Browse files
committed
rbac: fix deduplication of core group and add test coverage
1 parent b00261b commit 4c27c10

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

pkg/rbac/parser.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,6 @@ func removeDupAndSort(strs []string) []string {
146146

147147
// ToRule converts this rule to its Kubernetes API form.
148148
func (r *Rule) ToRule() rbacv1.PolicyRule {
149-
// fix the group names first, since letting people type "core" is nice
150-
for i, group := range r.Groups {
151-
if group == "core" {
152-
r.Groups[i] = ""
153-
}
154-
}
155149
return rbacv1.PolicyRule{
156150
APIGroups: r.Groups,
157151
Verbs: r.Verbs,
@@ -230,6 +224,13 @@ func GenerateRoles(ctx *genall.GenerationContext, roleName string) ([]interface{
230224
ruleMap := make(map[ruleKey]*Rule)
231225
// all the Rules having the same ruleKey will be merged into the first Rule
232226
for _, rule := range rules {
227+
// fix the group name first, since letting people type "core" is nice
228+
for i, name := range rule.Groups {
229+
if name == "core" {
230+
rule.Groups[i] = ""
231+
}
232+
}
233+
233234
key := rule.key()
234235
if _, ok := ruleMap[key]; !ok {
235236
ruleMap[key] = rule

pkg/rbac/testdata/controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ package controller
3030
// +kubebuilder:rbac:groups=not-deduplicate-groups2,resources=some,verbs=list
3131
// +kubebuilder:rbac:urls=/url-to-duplicate,verbs=get
3232
// +kubebuilder:rbac:urls=/another/url-to-duplicate,verbs=get
33+
// +kubebuilder:rbac:groups=core,resources=deduplicate,verbs=list
34+
// +kubebuilder:rbac:groups="",resources=me,verbs=list
35+
// +kubebuilder:rbac:groups=core;"";some-other-to-deduplicate-with-core,resources=me,verbs=list;get

pkg/rbac/testdata/role.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ rules:
99
- /url-to-duplicate
1010
verbs:
1111
- get
12+
- apiGroups:
13+
- ""
14+
resources:
15+
- deduplicate
16+
- me
17+
verbs:
18+
- list
19+
- apiGroups:
20+
- ""
21+
- some-other-to-deduplicate-with-core
22+
resources:
23+
- me
24+
verbs:
25+
- get
26+
- list
1227
- apiGroups:
1328
- art
1429
resources:

0 commit comments

Comments
 (0)