Skip to content

Commit e45d0d7

Browse files
committed
Add skipmap flag to skip validation for AdditionlPropperties
1 parent 5ba5473 commit e45d0d7

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

cmd/internal/codegen/parse/crd.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,28 +215,36 @@ func (b *APIs) parsePrimitiveValidation(t *types.Type, found sets.String, commen
215215
return props, buff.String()
216216
}
217217

218+
type mapTempateArgs struct {
219+
Result string
220+
SkipMap bool
221+
}
222+
218223
var mapTemplate = template.Must(template.New("map-template").Parse(
219224
`v1beta1.JSONSchemaProps{
220225
Type: "object",
221-
AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
226+
{{if not .SkipMap}}AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
222227
Allows: true,
223-
//Schema: &{{.}},
224-
},
228+
Schema: &{{.Result}},
229+
},{{end}}
225230
}`))
226231

227232
// parseMapValidation returns a JSONSchemaProps object and its serialization in
228233
// Go that describe the validations for the given map type.
229234
func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
230-
additionalProps, _ := b.typeToJSONSchemaProps(t.Elem, found, comments)
235+
additionalProps, result := b.typeToJSONSchemaProps(t.Elem, found, comments)
231236
props := v1beta1.JSONSchemaProps{
232237
Type: "object",
233-
AdditionalProperties: &v1beta1.JSONSchemaPropsOrBool{
238+
}
239+
parseOption := b.arguments.CustomArgs.(*ParseOptions)
240+
if !parseOption.SkipMap {
241+
props.AdditionalProperties = &v1beta1.JSONSchemaPropsOrBool{
234242
Allows: true,
235-
Schema: &additionalProps},
243+
Schema: &additionalProps}
236244
}
237245

238246
buff := &bytes.Buffer{}
239-
if err := mapTemplate.Execute(buff, ""); err != nil {
247+
if err := mapTemplate.Execute(buff, mapTempateArgs{result, parseOption.SkipMap}); err != nil {
240248
log.Fatalf("%v", err)
241249
}
242250
return props, buff.String()

cmd/internal/codegen/parse/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
"k8s.io/gengo/types"
2727
)
2828

29+
type ParseOptions struct {
30+
SkipMap bool
31+
}
32+
2933
// IsAPIResource returns true if t has a +resource/+kubebuilder:resource comment tag
3034
func IsAPIResource(t *types.Type) bool {
3135
for _, c := range t.CommentLines {

cmd/kubebuilder-gen/codegen/run/generator.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder-gen/codegen"
2323
"k8s.io/gengo/args"
2424
"k8s.io/gengo/generator"
25+
"github.com/spf13/pflag"
2526
)
2627

2728
// CodeGenerator generates code for Kubernetes resources and controllers
@@ -45,12 +46,16 @@ func (g *CodeGenerator) AddResourceGenerator(generator codegen.ResourceGenerator
4546
return g
4647
}
4748

48-
type customArgs struct{}
49-
5049
// Execute parses packages and executes the code generators against the resource and controller packages
5150
func (g *CodeGenerator) Execute() error {
5251
arguments := args.Default()
53-
arguments.CustomArgs = &customArgs{}
52+
53+
// Custom args.
54+
customArgs := &parse.ParseOptions{}
55+
pflag.CommandLine.BoolVar(&customArgs.SkipMap, "skip-map", true,
56+
"If set true, skip map types.")
57+
arguments.CustomArgs = customArgs
58+
5459
arguments.OutputFileBaseName = g.OutputFileBaseName
5560

5661
err := arguments.Execute(parse.NameSystems(), parse.DefaultNameSystem(), g.packages)

cmd/kubebuilder/create/config/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func (g CodeGenerator) Execute() error {
6767
return fmt.Errorf("Failed making a context: %v", err)
6868
}
6969

70+
arguments.CustomArgs = &parse.ParseOptions{SkipMap: true}
71+
7072
p := parse.NewAPIs(c, arguments)
7173
if crds {
7274
util.WriteString(output, strings.Join(getCrds(p), "---\n"))

cmd/kubebuilder/docs/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func (g CodeGenerator) Execute(dir string) error {
3232
return fmt.Errorf("Failed making a context: %v", err)
3333
}
3434

35+
arguments.CustomArgs = &parse.ParseOptions{SkipMap: true}
36+
3537
p := parse.NewAPIs(c, arguments)
3638
path := filepath.Join(dir, outputDir, "config.yaml")
3739

0 commit comments

Comments
 (0)