Skip to content

Commit 7dd2a2a

Browse files
author
fanzhangio
committed
Add annotation support for generating doc config.
- parse doc annotations with +kubebuilder:doc:note and +kubebuilder:doc:warning - support description_warning and description_note in config.yaml - add e2e test in test.sh
1 parent 5ba5473 commit 7dd2a2a

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

cmd/internal/codegen/parse/apis.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (b *APIs) parseAPIs() {
7272
NonNamespaced: resource.NonNamespaced,
7373
ShortName: resource.ShortName,
7474
}
75+
parseDoc(resource, apiResource)
7576
apiVersion.Resources[kind] = apiResource
7677
// Set the package for the api version
7778
apiVersion.Pkg = b.context.Universe[resource.Type.Name.Package]
@@ -275,3 +276,10 @@ func (b *APIs) genDeepCopy(c *types.Type) bool {
275276
comments := Comments(c.CommentLines)
276277
return comments.hasTag("subresource-request")
277278
}
279+
280+
func parseDoc(resource, apiResource *codegen.APIResource) {
281+
if HasDocAnnotation(resource.Type) {
282+
resource.DocAnnotation = getDocAnnotation(resource.Type, "warning", "note")
283+
apiResource.DocAnnotation = resource.DocAnnotation
284+
}
285+
}

cmd/internal/codegen/parse/util.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ func HasCategories(t *types.Type) bool {
125125
return false
126126
}
127127

128+
// HasDocAnnotation returns true if t is an APIResource with doc annotation
129+
// +kubebuilder:doc
130+
func HasDocAnnotation(t *types.Type) bool {
131+
if !IsAPIResource(t) {
132+
return false
133+
}
134+
for _, c := range t.CommentLines {
135+
if strings.Contains(c, "+kubebuilder:doc") {
136+
return true
137+
}
138+
}
139+
return false
140+
}
141+
128142
func IsUnversioned(t *types.Type, group string) bool {
129143
return IsApisDir(filepath.Base(filepath.Dir(t.Name.Package))) && GetGroup(t) == group
130144
}
@@ -202,3 +216,18 @@ func (c Comments) getTags(name, sep string) []string {
202216
}
203217
return tags
204218
}
219+
220+
// getDocAnnotation parse annotations of "+kubebuilder:doc:" with tags of "warning" or "doc" for control generating doc config.
221+
// E.g. +kubebuilder:doc:warning=foo +kubebuilder:doc:note=bar
222+
func getDocAnnotation(t *types.Type, tags ...string) map[string]string {
223+
annotation := make(map[string]string)
224+
for _, tag := range tags {
225+
for _, c := range t.CommentLines {
226+
prefix := fmt.Sprintf("+kubebuilder:doc:%s=", tag)
227+
if strings.HasPrefix(c, prefix) {
228+
annotation[tag] = strings.Replace(c, prefix, "", 1)
229+
}
230+
}
231+
}
232+
return annotation
233+
}

cmd/internal/codegen/types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ limitations under the License.
1717
package codegen
1818

1919
import (
20+
"sort"
21+
2022
rbacv1 "k8s.io/api/rbac/v1"
2123
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2224
"k8s.io/apimachinery/pkg/apis/meta/v1"
2325
"k8s.io/apimachinery/pkg/runtime/schema"
2426
"k8s.io/apimachinery/pkg/util/sets"
2527
"k8s.io/gengo/types"
26-
"sort"
2728
)
2829

2930
type APIs struct {
@@ -161,6 +162,8 @@ type APIResource struct {
161162
CRD v1beta1.CustomResourceDefinition
162163
Validation string
163164
ValidationComments string
165+
// DocAnnotation is a map of annotations by name for doc. e.g. warning, notes message
166+
DocAnnotation map[string]string
164167
}
165168

166169
type APISubresource struct {

cmd/kubebuilder/docs/gen.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (g CodeGenerator) Execute(dir string) error {
6161
}
6262
sort.Strings(vs)
6363
r.Version = vs[0]
64+
if version[vs[0]] != nil {
65+
annotations := version[vs[0]].DocAnnotation
66+
r.Warning = annotations["warning"]
67+
r.Note = annotations["note"]
68+
}
6469
m[r.Kind] = r
6570
s = append(s, r.Kind)
6671
}
@@ -90,7 +95,7 @@ type Category struct {
9095
}
9196

9297
type Resource struct {
93-
Kind, Version, Group string
98+
Kind, Version, Group, Warning, Note string
9499
}
95100

96101
var docsConfigTemplate = `example_location: "examples"
@@ -103,5 +108,8 @@ resource_categories: {{ range $category := .Categories }}
103108
resources: {{ range $resource := $category.Resources }}
104109
- name: "{{ $resource.Kind }}"
105110
version: "{{ $resource.Version }}"
106-
group: "{{ $resource.Group }}"{{ end }}
111+
group: "{{ $resource.Group }}"{{ if $resource.Warning }}
112+
description_warning: "{{ $resource.Warning }}"{{ end -}}{{ if $resource.Note }}
113+
description_note: "{{ $resource.Note }}"{{ end -}}
114+
{{ end }}
107115
{{ end }}`

test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,11 @@ function test_docs {
500500
diff docs/reference/includes $kb_orig/test/docs/expected/includes
501501
diff docs/reference/manifest.json $kb_orig/test/docs/expected/manifest.json
502502
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config.yaml
503+
504+
header_text "testing doc annotations"
505+
sed -i -e "s|// +kubebuilder:categories=foo,bar|// +kubebuilder:categories=foo,bar\n// +kubebuilder:doc:note=test notes message annotations\n// +kubebuilder:doc:warning=test warnings message annotations|" pkg/apis/insect/v1beta1/bee_types.go
506+
kubebuilder docs --brodocs=false --cleanup=false
507+
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config-annotated.yaml
503508
}
504509

505510
function generate_controller {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
example_location: "examples"
2+
api_groups:
3+
- "Ant"
4+
5+
- "Insect"
6+
resource_categories:
7+
- name: "Ant"
8+
include: "ant"
9+
resources:
10+
- name: "Ant"
11+
version: "v1beta1"
12+
group: "ant"
13+
14+
- name: "Insect"
15+
include: "insect"
16+
resources:
17+
- name: "Bee"
18+
version: "v1beta1"
19+
group: "insect"
20+
description_warning: "test warnings message annotations"
21+
description_note: "test notes message annotations"
22+
- name: "Wasp"
23+
version: "v1beta1"
24+
group: "insect"

0 commit comments

Comments
 (0)