Skip to content

Commit

Permalink
Address comments: change flag name and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed May 18, 2018
1 parent 5906e25 commit 8059b9b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd/internal/codegen/parse/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ func (b *APIs) parsePrimitiveValidation(t *types.Type, found sets.String, commen

type mapTempateArgs struct {
Result string
SkipMap bool
SkipMapValidation bool
}

var mapTemplate = template.Must(template.New("map-template").Parse(
`v1beta1.JSONSchemaProps{
Type: "object",
{{if not .SkipMap}}AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
{{if not .SkipMapValidation}}AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
Allows: true,
Schema: &{{.Result}},
},{{end}}
Expand All @@ -237,14 +237,14 @@ func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []s
Type: "object",
}
parseOption := b.arguments.CustomArgs.(*ParseOptions)
if !parseOption.SkipMap {
if !parseOption.SkipMapValidation {
props.AdditionalProperties = &v1beta1.JSONSchemaPropsOrBool{
Allows: true,
Schema: &additionalProps}
}

buff := &bytes.Buffer{}
if err := mapTemplate.Execute(buff, mapTempateArgs{result, parseOption.SkipMap}); err != nil {
if err := mapTemplate.Execute(buff, mapTempateArgs{Result: result, SkipMapValidation: parseOption.SkipMapValidation}); err != nil {
log.Fatalf("%v", err)
}
return props, buff.String()
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/codegen/parse/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

type ParseOptions struct {
SkipMap bool
SkipMapValidation bool
}

// IsAPIResource returns true if t has a +resource/+kubebuilder:resource comment tag
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubebuilder-gen/codegen/run/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (g *CodeGenerator) Execute() error {

// Custom args.
customArgs := &parse.ParseOptions{}
pflag.CommandLine.BoolVar(&customArgs.SkipMap, "skip-map", true,
"If set true, skip map types.")
pflag.CommandLine.BoolVar(&customArgs.SkipMapValidation, "skip-map-validation", true,
"if set to true, skip generating validation schema for map type in CRD.")
arguments.CustomArgs = customArgs

arguments.OutputFileBaseName = g.OutputFileBaseName
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubebuilder/create/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ kubebuilder create config --crds --output myextensionname.yaml
fmt.Printf("Must either specify the name of the extension with --name or set --crds.\n")
return
}
CodeGenerator{SkipMap: skipmap}.Execute()
CodeGenerator{SkipMapValidation: skipMapValidation}.Execute()
log.Printf("Config written to %s", output)
},
}

var (
controllerType, controllerImage, name, output, crdNamespace string
crds, skipmap bool
crds, skipMapValidation bool
)

func AddCreateConfig(cmd *cobra.Command) {
Expand All @@ -75,5 +75,5 @@ func AddCreateConfig(cmd *cobra.Command) {
configCmd.Flags().StringVar(&controllerImage, "controller-image", "", "name of the controller container to run.")
configCmd.Flags().StringVar(&name, "name", "", "name of the installation. used to generate the namespace and resource names.")
configCmd.Flags().StringVar(&output, "output", filepath.Join("hack", "install.yaml"), "location to write yaml to")
configCmd.Flags().BoolVar(&skipmap, "skip-map", true, "if set to true, skip validation of map in CRD")
configCmd.Flags().BoolVar(&skipMapValidation, "skip-map-validation", true, "if set to true, skip generating validation schema for map type in CRD.")
}
4 changes: 2 additions & 2 deletions cmd/kubebuilder/create/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

// CodeGenerator generates code for Kubernetes resources and controllers
type CodeGenerator struct{
SkipMap bool
SkipMapValidation bool
}

var kblabels = map[string]string{
Expand Down Expand Up @@ -69,7 +69,7 @@ func (g CodeGenerator) Execute() error {
return fmt.Errorf("Failed making a context: %v", err)
}

arguments.CustomArgs = &parse.ParseOptions{SkipMap: g.SkipMap}
arguments.CustomArgs = &parse.ParseOptions{SkipMapValidation: g.SkipMapValidation}

p := parse.NewAPIs(c, arguments)
if crds {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubebuilder/docs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g CodeGenerator) Execute(dir string) error {
return fmt.Errorf("Failed making a context: %v", err)
}

arguments.CustomArgs = &parse.ParseOptions{SkipMap: true}
arguments.CustomArgs = &parse.ParseOptions{SkipMapValidation: true}

p := parse.NewAPIs(c, arguments)
path := filepath.Join(dir, outputDir, "config.yaml")
Expand Down
12 changes: 12 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ function generate_crd_resources {
sed -i -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|" pkg/apis/insect/v1beta1/bee_types.go
sed -i -e "s|type BeeController struct {|// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list\ntype BeeController struct {|" pkg/controller/bee/controller.go

header_text "adding a map type to resource"
sed -i -e "s|type BeeSpec struct {|type BeeSpec struct {\n Request map[string]string \`json:\"request,omitempty\"\`|" pkg/apis/insect/v1beta1/bee_types.go

header_text "generating and testing CRD definition"
kubebuilder create config --crds --output crd.yaml
kubebuilder create config --controller-image myimage:v1 --name myextensionname --output install.yaml
Expand Down Expand Up @@ -187,6 +190,9 @@ spec:
metadata:
type: object
spec:
properties:
request:
type: object
type: object
status:
type: object
Expand Down Expand Up @@ -281,6 +287,9 @@ spec:
metadata:
type: object
spec:
properties:
request:
type: object
type: object
status:
type: object
Expand Down Expand Up @@ -429,6 +438,9 @@ spec:
metadata:
type: object
spec:
properties:
request:
type: object
type: object
status:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Appears In:

Field | Description
------------ | -----------
`request`<br /> *object* |

### BeeStatus v1beta1

Expand Down

0 comments on commit 8059b9b

Please sign in to comment.