Skip to content

Commit c98652c

Browse files
committed
Add missing comments for internal rbac/informer generation
1 parent 9995627 commit c98652c

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

cmd/internal/codegen/parse/rbac.go

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"strings"
2323

2424
rbacv1 "k8s.io/api/rbac/v1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/gengo/types"
26-
"k8s.io/apimachinery/pkg/apis/meta/v1"
2727
)
2828

29+
// parseRBAC populates the RBAC rules for each annotated type.
2930
func (b *APIs) parseRBAC() {
3031
for _, c := range b.context.Order {
3132
if IsRBAC(c) {
@@ -37,85 +38,86 @@ func (b *APIs) parseRBAC() {
3738
}
3839

3940
func (b *APIs) getRBACTag(c *types.Type) []string {
40-
comments := Comments(c.CommentLines)
41-
resource := comments.getTags("rbac", ":")
42-
resource = append(resource, comments.getTags("kubebuilder:rbac", ":")...)
43-
if len(resource) == 0 {
44-
panic(fmt.Errorf("Must specify +kubebuilder:rbac comment for type %v", c.Name))
45-
}
46-
return resource
41+
comments := Comments(c.CommentLines)
42+
resource := comments.getTags("rbac", ":")
43+
resource = append(resource, comments.getTags("kubebuilder:rbac", ":")...)
44+
if len(resource) == 0 {
45+
panic(fmt.Errorf("Must specify +kubebuilder:rbac comment for type %v", c.Name))
46+
}
47+
return resource
4748
}
4849

4950
func parseRBACTag(tag string) rbacv1.PolicyRule {
50-
result := rbacv1.PolicyRule{}
51-
for _, elem := range strings.Split(tag, ",") {
52-
kv := strings.Split(elem, "=")
53-
if len(kv) != 2 {
54-
log.Fatalf("// +kubebuilder:rbac: tags must be key value pairs. Expected "+
55-
"keys [groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>] "+
56-
"Got string: [%s]", tag)
57-
}
58-
value := kv[1]
59-
values := []string{}
60-
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
61-
value = value[1 : len(value)-1]
62-
}
63-
values = strings.Split(value, ";")
64-
switch kv[0] {
65-
case "groups":
66-
result.APIGroups = values
67-
case "resources":
68-
result.Resources = values
69-
case "verbs":
70-
result.Verbs = values
71-
case "urls":
72-
result.NonResourceURLs = values
73-
}
74-
}
75-
return result
51+
result := rbacv1.PolicyRule{}
52+
for _, elem := range strings.Split(tag, ",") {
53+
kv := strings.Split(elem, "=")
54+
if len(kv) != 2 {
55+
log.Fatalf("// +kubebuilder:rbac: tags must be key value pairs. Expected "+
56+
"keys [groups=<group1;group2>,resources=<resource1;resource2>,verbs=<verb1;verb2>] "+
57+
"Got string: [%s]", tag)
58+
}
59+
value := kv[1]
60+
values := []string{}
61+
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
62+
value = value[1 : len(value)-1]
63+
}
64+
values = strings.Split(value, ";")
65+
switch kv[0] {
66+
case "groups":
67+
result.APIGroups = values
68+
case "resources":
69+
result.Resources = values
70+
case "verbs":
71+
result.Verbs = values
72+
case "urls":
73+
result.NonResourceURLs = values
74+
}
75+
}
76+
return result
7677
}
7778

79+
// parseInfomers populates the informers to generate on each annotated type.
7880
func (b *APIs) parseInformers() {
79-
for _, c := range b.context.Order {
80-
if IsInformer(c) {
81-
for _, tag := range b.getInformerTag(c) {
82-
if b.Informers == nil {
83-
b.Informers = map[v1.GroupVersionKind]bool{}
84-
}
85-
b.Informers[parseInformerTag(tag)] = true
86-
}
87-
}
88-
}
81+
for _, c := range b.context.Order {
82+
if IsInformer(c) {
83+
for _, tag := range b.getInformerTag(c) {
84+
if b.Informers == nil {
85+
b.Informers = map[v1.GroupVersionKind]bool{}
86+
}
87+
b.Informers[parseInformerTag(tag)] = true
88+
}
89+
}
90+
}
8991
}
9092

9193
func (b *APIs) getInformerTag(c *types.Type) []string {
92-
comments := Comments(c.CommentLines)
93-
resource := comments.getTags("informers", ":")
94-
resource = append(resource, comments.getTags("kubebuilder:informers", ":")...)
95-
if len(resource) == 0 {
96-
panic(fmt.Errorf("Must specify +kubebuilder:informers comment for type %v", c.Name))
97-
}
98-
return resource
94+
comments := Comments(c.CommentLines)
95+
resource := comments.getTags("informers", ":")
96+
resource = append(resource, comments.getTags("kubebuilder:informers", ":")...)
97+
if len(resource) == 0 {
98+
panic(fmt.Errorf("Must specify +kubebuilder:informers comment for type %v", c.Name))
99+
}
100+
return resource
99101
}
100102

101103
func parseInformerTag(tag string) v1.GroupVersionKind {
102-
result := v1.GroupVersionKind{}
103-
for _, elem := range strings.Split(tag, ",") {
104-
kv := strings.Split(elem, "=")
105-
if len(kv) != 2 {
106-
log.Fatalf("// +kubebuilder:informers: tags must be key value pairs. Expected "+
107-
"keys [group=core,version=v1,kind=Pod] "+
108-
"Got string: [%s]", tag)
109-
}
110-
value := kv[1]
111-
switch kv[0] {
112-
case "group":
113-
result.Group = value
114-
case "version":
115-
result.Version = value
116-
case "kind":
117-
result.Kind = value
118-
}
119-
}
120-
return result
121-
}
104+
result := v1.GroupVersionKind{}
105+
for _, elem := range strings.Split(tag, ",") {
106+
kv := strings.Split(elem, "=")
107+
if len(kv) != 2 {
108+
log.Fatalf("// +kubebuilder:informers: tags must be key value pairs. Expected "+
109+
"keys [group=core,version=v1,kind=Pod] "+
110+
"Got string: [%s]", tag)
111+
}
112+
value := kv[1]
113+
switch kv[0] {
114+
case "group":
115+
result.Group = value
116+
case "version":
117+
result.Version = value
118+
case "kind":
119+
result.Kind = value
120+
}
121+
}
122+
return result
123+
}

0 commit comments

Comments
 (0)