Skip to content

Commit 950a0e8

Browse files
authored
Merge pull request #118 from sohankunkerkar/description
🐛 Fix description generation to work for all types of fields
2 parents 80e23c9 + b6f0276 commit 950a0e8

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ spec:
6969
format: byte
7070
type: string
7171
knights:
72+
description: This is a comment on an array field.
7273
items:
7374
type: string
7475
maxItems: 500
7576
minItems: 1
7677
type: array
78+
location:
79+
additionalProperties:
80+
type: string
81+
description: This is a comment on a map field.
82+
type: object
7783
name:
7884
maxLength: 15
7985
minLength: 1
@@ -102,14 +108,17 @@ spec:
102108
- type: string
103109
- type: integer
104110
template:
111+
description: This is a comment on an object field.
105112
type: object
106113
winner:
114+
description: This is a comment on a boolean field.
107115
type: boolean
108116
required:
109117
- rank
110118
- template
111119
- replicas
112120
- rook
121+
- location
113122
type: object
114123
status:
115124
properties:

pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,46 @@ type ToySpec struct {
3232
// +kubebuilder:validation:Maximum=100
3333
// +kubebuilder:validation:Minimum=1
3434
// +kubebuilder:validation:ExclusiveMinimum=true
35-
Power float32 `json:"power,omitempty"`
36-
Bricks int32 `json:"bricks,omitempty"`
35+
Power float32 `json:"power,omitempty"`
36+
37+
Bricks int32 `json:"bricks,omitempty"`
38+
3739
// +kubebuilder:validation:MaxLength=15
3840
// +kubebuilder:validation:MinLength=1
3941
Name string `json:"name,omitempty"`
42+
43+
// This is a comment on an array field.
4044
// +kubebuilder:validation:MaxItems=500
4145
// +kubebuilder:validation:MinItems=1
4246
// +kubebuilder:validation:UniqueItems=false
4347
Knights []string `json:"knights,omitempty"`
44-
Winner bool `json:"winner,omitempty"`
48+
49+
// This is a comment on a boolean field.
50+
Winner bool `json:"winner,omitempty"`
51+
4552
// +kubebuilder:validation:Enum=Lion,Wolf,Dragon
4653
Alias string `json:"alias,omitempty"`
54+
4755
// +kubebuilder:validation:Enum=1,2,3
48-
Rank int `json:"rank"`
56+
Rank int `json:"rank"`
57+
4958
Comment []byte `json:"comment,omitempty"`
5059

51-
Template v1.PodTemplateSpec `json:"template"`
52-
Claim v1.PersistentVolumeClaim `json:"claim,omitempty"`
60+
// This is a comment on an object field.
61+
Template v1.PodTemplateSpec `json:"template"`
62+
63+
Claim v1.PersistentVolumeClaim `json:"claim,omitempty"`
64+
5365
//This is a dummy comment.
5466
// Just checking if the multi-line comments are working or not.
5567
Replicas *int32 `json:"replicas"`
5668

5769
// This is a newly added field.
5870
// Using this for testing purpose.
5971
Rook *intstr.IntOrString `json:"rook"`
72+
73+
// This is a comment on a map field.
74+
Location map[string]string `json:"location"`
6075
}
6176

6277
// ToyStatus defines the observed state of Toy

pkg/internal/codegen/parse/crd.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,17 @@ var mapTemplate = template.Must(template.New("map-template").Parse(
311311
// Go that describe the validations for the given map type.
312312
func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
313313
additionalProps, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false)
314+
additionalProps.Description = ""
314315
props := v1beta1.JSONSchemaProps{
315-
Type: "object",
316+
Type: "object",
317+
Description: parseDescription(comments),
316318
}
317319
parseOption := b.arguments.CustomArgs.(*Options)
318320
if !parseOption.SkipMapValidation {
319321
props.AdditionalProperties = &v1beta1.JSONSchemaPropsOrBool{
320322
Allows: true,
321323
Schema: &additionalProps}
322324
}
323-
324325
buff := &bytes.Buffer{}
325326
if err := mapTemplate.Execute(buff, mapTempateArgs{Result: result, SkipMapValidation: parseOption.SkipMapValidation}); err != nil {
326327
log.Fatalf("%v", err)
@@ -359,16 +360,19 @@ type arrayTemplateArgs struct {
359360
// Go that describe the validations for the given array type.
360361
func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
361362
items, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false)
363+
items.Description = ""
362364
props := v1beta1.JSONSchemaProps{
363-
Type: "array",
364-
Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items},
365+
Type: "array",
366+
Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items},
367+
Description: parseDescription(comments),
365368
}
366369
// To represent byte arrays in the generated code, the property of the OpenAPI definition
367370
// should have string as its type and byte as its format.
368371
if t.Name.Name == "[]byte" {
369372
props.Type = "string"
370373
props.Format = "byte"
371374
props.Items = nil
375+
props.Description = parseDescription(comments)
372376
}
373377
for _, l := range comments {
374378
getValidation(l, &props)
@@ -409,7 +413,8 @@ var objectTemplate = template.Must(template.New("object-template").Parse(
409413
func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments []string, isRoot bool) (v1beta1.JSONSchemaProps, string) {
410414
buff := &bytes.Buffer{}
411415
props := v1beta1.JSONSchemaProps{
412-
Type: "object",
416+
Type: "object",
417+
Description: parseDescription(comments),
413418
}
414419

415420
if strings.HasPrefix(t.Name.String(), "k8s.io/api") {

pkg/internal/codegen/parse/util.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,16 @@ func parseByteValue(b []byte) string {
336336

337337
// parseDescription parse comments above each field in the type definition.
338338
func parseDescription(res []string) string {
339-
var temp, data string
339+
var temp strings.Builder
340+
var desc string
340341
for _, comment := range res {
341342
if !(strings.Contains(comment, "+kubebuilder") || strings.Contains(comment, "+optional")) {
342-
temp += comment + " "
343-
data = strings.TrimRight(temp, " ")
343+
temp.WriteString(comment)
344+
temp.WriteString(" ")
345+
desc = strings.TrimRight(temp.String(), " ")
344346
}
345347
}
346-
return data
348+
return desc
347349
}
348350

349351
// parseEnumToString returns a representive validated go format string from JSONSchemaProps schema

0 commit comments

Comments
 (0)