diff --git a/pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml b/pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml index e60ae7338..9b8d235de 100644 --- a/pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml +++ b/pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml @@ -69,11 +69,17 @@ spec: format: byte type: string knights: + description: This is a comment on an array field. items: type: string maxItems: 500 minItems: 1 type: array + location: + additionalProperties: + type: string + description: This is a comment on a map field. + type: object name: maxLength: 15 minLength: 1 @@ -102,14 +108,17 @@ spec: - type: string - type: integer template: + description: This is a comment on an object field. type: object winner: + description: This is a comment on a boolean field. type: boolean required: - rank - template - replicas - rook + - location type: object status: properties: diff --git a/pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go b/pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go index 26d2d4867..391c73a19 100644 --- a/pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go +++ b/pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go @@ -32,24 +32,36 @@ type ToySpec struct { // +kubebuilder:validation:Maximum=100 // +kubebuilder:validation:Minimum=1 // +kubebuilder:validation:ExclusiveMinimum=true - Power float32 `json:"power,omitempty"` - Bricks int32 `json:"bricks,omitempty"` + Power float32 `json:"power,omitempty"` + + Bricks int32 `json:"bricks,omitempty"` + // +kubebuilder:validation:MaxLength=15 // +kubebuilder:validation:MinLength=1 Name string `json:"name,omitempty"` + + // This is a comment on an array field. // +kubebuilder:validation:MaxItems=500 // +kubebuilder:validation:MinItems=1 // +kubebuilder:validation:UniqueItems=false Knights []string `json:"knights,omitempty"` - Winner bool `json:"winner,omitempty"` + + // This is a comment on a boolean field. + Winner bool `json:"winner,omitempty"` + // +kubebuilder:validation:Enum=Lion,Wolf,Dragon Alias string `json:"alias,omitempty"` + // +kubebuilder:validation:Enum=1,2,3 - Rank int `json:"rank"` + Rank int `json:"rank"` + Comment []byte `json:"comment,omitempty"` - Template v1.PodTemplateSpec `json:"template"` - Claim v1.PersistentVolumeClaim `json:"claim,omitempty"` + // This is a comment on an object field. + Template v1.PodTemplateSpec `json:"template"` + + Claim v1.PersistentVolumeClaim `json:"claim,omitempty"` + //This is a dummy comment. // Just checking if the multi-line comments are working or not. Replicas *int32 `json:"replicas"` @@ -57,6 +69,9 @@ type ToySpec struct { // This is a newly added field. // Using this for testing purpose. Rook *intstr.IntOrString `json:"rook"` + + // This is a comment on a map field. + Location map[string]string `json:"location"` } // ToyStatus defines the observed state of Toy diff --git a/pkg/internal/codegen/parse/crd.go b/pkg/internal/codegen/parse/crd.go index 8bd8f9afd..0e079e209 100644 --- a/pkg/internal/codegen/parse/crd.go +++ b/pkg/internal/codegen/parse/crd.go @@ -311,8 +311,10 @@ var mapTemplate = template.Must(template.New("map-template").Parse( // Go that describe the validations for the given map type. func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) { additionalProps, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false) + additionalProps.Description = "" props := v1beta1.JSONSchemaProps{ - Type: "object", + Type: "object", + Description: parseDescription(comments), } parseOption := b.arguments.CustomArgs.(*Options) if !parseOption.SkipMapValidation { @@ -320,7 +322,6 @@ func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []s Allows: true, Schema: &additionalProps} } - buff := &bytes.Buffer{} if err := mapTemplate.Execute(buff, mapTempateArgs{Result: result, SkipMapValidation: parseOption.SkipMapValidation}); err != nil { log.Fatalf("%v", err) @@ -359,9 +360,11 @@ type arrayTemplateArgs struct { // Go that describe the validations for the given array type. func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) { items, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false) + items.Description = "" props := v1beta1.JSONSchemaProps{ - Type: "array", - Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items}, + Type: "array", + Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items}, + Description: parseDescription(comments), } // To represent byte arrays in the generated code, the property of the OpenAPI definition // should have string as its type and byte as its format. @@ -369,6 +372,7 @@ func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments [ props.Type = "string" props.Format = "byte" props.Items = nil + props.Description = parseDescription(comments) } for _, l := range comments { getValidation(l, &props) @@ -409,7 +413,8 @@ var objectTemplate = template.Must(template.New("object-template").Parse( func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments []string, isRoot bool) (v1beta1.JSONSchemaProps, string) { buff := &bytes.Buffer{} props := v1beta1.JSONSchemaProps{ - Type: "object", + Type: "object", + Description: parseDescription(comments), } if strings.HasPrefix(t.Name.String(), "k8s.io/api") { diff --git a/pkg/internal/codegen/parse/util.go b/pkg/internal/codegen/parse/util.go index 3ca9d3829..eb9dfa35a 100644 --- a/pkg/internal/codegen/parse/util.go +++ b/pkg/internal/codegen/parse/util.go @@ -336,14 +336,16 @@ func parseByteValue(b []byte) string { // parseDescription parse comments above each field in the type definition. func parseDescription(res []string) string { - var temp, data string + var temp strings.Builder + var desc string for _, comment := range res { if !(strings.Contains(comment, "+kubebuilder") || strings.Contains(comment, "+optional")) { - temp += comment + " " - data = strings.TrimRight(temp, " ") + temp.WriteString(comment) + temp.WriteString(" ") + desc = strings.TrimRight(temp.String(), " ") } } - return data + return desc } // parseEnumToString returns a representive validated go format string from JSONSchemaProps schema