Skip to content

Commit 561e4da

Browse files
authored
Merge pull request #170 from Liujingfang1/additionalProperties
Add skipmap flag to skip validation for AdditionlPropperties
2 parents a81a1cc + 8059b9b commit 561e4da

File tree

8 files changed

+50
-13
lines changed

8 files changed

+50
-13
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+
SkipMapValidation 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 .SkipMapValidation}}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.SkipMapValidation {
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: result, SkipMapValidation: parseOption.SkipMapValidation}); 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+
SkipMapValidation 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.SkipMapValidation, "skip-map-validation", true,
56+
"if set to true, skip generating validation schema for map type in CRD.")
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/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ kubebuilder create config --crds --output myextensionname.yaml
5757
fmt.Printf("Must either specify the name of the extension with --name or set --crds.\n")
5858
return
5959
}
60-
CodeGenerator{}.Execute()
60+
CodeGenerator{SkipMapValidation: skipMapValidation}.Execute()
6161
log.Printf("Config written to %s", output)
6262
},
6363
}
6464

6565
var (
6666
controllerType, controllerImage, name, output, crdNamespace string
67-
crds bool
67+
crds, skipMapValidation bool
6868
)
6969

7070
func AddCreateConfig(cmd *cobra.Command) {
@@ -75,4 +75,5 @@ func AddCreateConfig(cmd *cobra.Command) {
7575
configCmd.Flags().StringVar(&controllerImage, "controller-image", "", "name of the controller container to run.")
7676
configCmd.Flags().StringVar(&name, "name", "", "name of the installation. used to generate the namespace and resource names.")
7777
configCmd.Flags().StringVar(&output, "output", filepath.Join("hack", "install.yaml"), "location to write yaml to")
78+
configCmd.Flags().BoolVar(&skipMapValidation, "skip-map-validation", true, "if set to true, skip generating validation schema for map type in CRD.")
7879
}

cmd/kubebuilder/create/config/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import (
3636
)
3737

3838
// CodeGenerator generates code for Kubernetes resources and controllers
39-
type CodeGenerator struct{}
39+
type CodeGenerator struct{
40+
SkipMapValidation bool
41+
}
4042

4143
var kblabels = map[string]string{
4244
"kubebuilder.k8s.io": version.GetVersion().KubeBuilderVersion,
@@ -67,6 +69,8 @@ func (g CodeGenerator) Execute() error {
6769
return fmt.Errorf("Failed making a context: %v", err)
6870
}
6971

72+
arguments.CustomArgs = &parse.ParseOptions{SkipMapValidation: g.SkipMapValidation}
73+
7074
p := parse.NewAPIs(c, arguments)
7175
if crds {
7276
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{SkipMapValidation: true}
36+
3537
p := parse.NewAPIs(c, arguments)
3638
path := filepath.Join(dir, outputDir, "config.yaml")
3739

test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ function generate_crd_resources {
150150
sed -i -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|" pkg/apis/insect/v1beta1/bee_types.go
151151
sed -i -e "s|type BeeController struct {|// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list\ntype BeeController struct {|" pkg/controller/bee/controller.go
152152

153+
header_text "adding a map type to resource"
154+
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
155+
153156
header_text "generating and testing CRD definition"
154157
kubebuilder create config --crds --output crd.yaml
155158
kubebuilder create config --controller-image myimage:v1 --name myextensionname --output install.yaml
@@ -187,6 +190,9 @@ spec:
187190
metadata:
188191
type: object
189192
spec:
193+
properties:
194+
request:
195+
type: object
190196
type: object
191197
status:
192198
type: object
@@ -281,6 +287,9 @@ spec:
281287
metadata:
282288
type: object
283289
spec:
290+
properties:
291+
request:
292+
type: object
284293
type: object
285294
status:
286295
type: object
@@ -429,6 +438,9 @@ spec:
429438
metadata:
430439
type: object
431440
spec:
441+
properties:
442+
request:
443+
type: object
432444
type: object
433445
status:
434446
type: object

test/docs/expected/includes/_generated_bee_v1beta1_insect_concept.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Appears In:
3939

4040
Field | Description
4141
------------ | -----------
42+
`request`<br /> *object* |
4243

4344
### BeeStatus v1beta1
4445

0 commit comments

Comments
 (0)