Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support for example marker #774

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var FieldOnlyMarkers = []*definitionWithHelp{
must(markers.MakeAnyTypeDefinition("kubebuilder:default", markers.DescribesField, Default{})).
WithHelp(Default{}.Help()),

must(markers.MakeAnyTypeDefinition("kubebuilder:example", markers.DescribesField, Example{})).
WithHelp(Example{}.Help()),

must(markers.MakeDefinition("kubebuilder:validation:EmbeddedResource", markers.DescribesField, XEmbeddedResource{})).
WithHelp(XEmbeddedResource{}.Help()),

Expand Down Expand Up @@ -222,6 +225,19 @@ type Default struct {
Value interface{}
}

// +controllertools:marker:generateHelp:category="CRD validation"
// Example sets the example value for this field.
//
// An example value will be accepted as any value valid for the
// field. Formatting for common types include: boolean: `true`, string:
// `Cluster`, numerical: `1.24`, array: `{1,2}`, object: `{policy:
// "delete"}`). Examples should be defined in pruned form, and only best-effort
// validation will be performed. Full validation of an example requires
// submission of the containing CRD to an apiserver.
type Example struct {
Value interface{}
}

// +controllertools:marker:generateHelp:category="CRD processing"
// PreserveUnknownFields stops the apiserver from pruning fields which are not specified.
//
Expand Down Expand Up @@ -465,6 +481,15 @@ func (m Default) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
return nil
}

func (m Example) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
marshalledExample, err := json.Marshal(m.Value)
if err != nil {
return err
}
schema.Example = &apiext.JSON{Raw: marshalledExample}
return nil
}

func (m XPreserveUnknownFields) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
defTrue := true
schema.XPreserveUnknownFields = &defTrue
Expand Down
16 changes: 16 additions & 0 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ type CronJobSpec struct {

// This tests that primitive defaulting can be performed.
// +kubebuilder:default=forty-two
// +kubebuilder:example=forty-two
DefaultedString string `json:"defaultedString"`

// This tests that slice defaulting can be performed.
// +kubebuilder:default={a,b}
// +kubebuilder:example={a,b}
DefaultedSlice []string `json:"defaultedSlice"`

// This tests that object defaulting can be performed.
// +kubebuilder:default={{nested: {foo: "baz", bar: true}},{nested: {bar: false}}}
// +kubebuilder:example={{nested: {foo: "baz", bar: true}},{nested: {bar: false}}}
DefaultedObject []RootObject `json:"defaultedObject"`

// This tests that pattern validator is properly applied.
Expand Down
1 change: 1 addition & 0 deletions pkg/crd/testdata/gen/bar.example.com_foos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
default: fooDefaultString
description: This tests that defaulted fields are stripped for v1beta1,
but not for v1
example: fooExampleString
type: string
required:
- defaultedString
Expand Down
1 change: 1 addition & 0 deletions pkg/crd/testdata/gen/foo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FooSpec struct {
// This tests that defaulted fields are stripped for v1beta1,
// but not for v1
// +kubebuilder:default=fooDefaultString
// +kubebuilder:example=fooExampleString
DefaultedString string `json:"defaultedString"`
}
type FooStatus struct{}
Expand Down
1 change: 1 addition & 0 deletions pkg/crd/testdata/gen/zoo/bar.example.com_zooes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
default: zooDefaultString
description: This tests that defaulted fields are stripped for v1beta1,
but not for v1
example: zooExampleString
type: string
required:
- defaultedString
Expand Down
1 change: 1 addition & 0 deletions pkg/crd/testdata/gen/zoo/zoo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ZooSpec struct {
// This tests that defaulted fields are stripped for v1beta1,
// but not for v1
// +kubebuilder:default=zooDefaultString
// +kubebuilder:example=zooExampleString
DefaultedString string `json:"defaultedString"`
}
type ZooStatus struct{}
Expand Down
10 changes: 10 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ spec:
- nested:
bar: false
description: This tests that object defaulting can be performed.
example:
- nested:
bar: true
foo: baz
- nested:
bar: false
items:
properties:
nested:
Expand All @@ -126,12 +132,16 @@ spec:
- a
- b
description: This tests that slice defaulting can be performed.
example:
- a
- b
items:
type: string
type: array
defaultedString:
default: forty-two
description: This tests that primitive defaulting can be performed.
example: forty-two
type: string
embeddedResource:
type: object
Expand Down
8 changes: 8 additions & 0 deletions pkg/crd/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/rbac/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pkg/webhook/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.