Skip to content

Commit

Permalink
Merge pull request #118 from sohankunkerkar/description
Browse files Browse the repository at this point in the history
🐛 Fix description generation to work for all types of fields
  • Loading branch information
k8s-ci-robot authored Dec 17, 2018
2 parents 80e23c9 + b6f0276 commit 950a0e8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
9 changes: 9 additions & 0 deletions pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 21 additions & 6 deletions pkg/crd/generator/testData/pkg/apis/fun/v1alpha1/toy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,46 @@ 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"`

// 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
Expand Down
15 changes: 10 additions & 5 deletions pkg/internal/codegen/parse/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,17 @@ 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 {
props.AdditionalProperties = &v1beta1.JSONSchemaPropsOrBool{
Allows: true,
Schema: &additionalProps}
}

buff := &bytes.Buffer{}
if err := mapTemplate.Execute(buff, mapTempateArgs{Result: result, SkipMapValidation: parseOption.SkipMapValidation}); err != nil {
log.Fatalf("%v", err)
Expand Down Expand Up @@ -359,16 +360,19 @@ 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.
if t.Name.Name == "[]byte" {
props.Type = "string"
props.Format = "byte"
props.Items = nil
props.Description = parseDescription(comments)
}
for _, l := range comments {
getValidation(l, &props)
Expand Down Expand Up @@ -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") {
Expand Down
10 changes: 6 additions & 4 deletions pkg/internal/codegen/parse/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 950a0e8

Please sign in to comment.