Skip to content

Commit 9084e41

Browse files
authored
Merge pull request #774 from sbueringer/pr-add-example-marker
✨ Add support for example marker
2 parents 5dd653b + 671cc95 commit 9084e41

11 files changed

+84
-1
lines changed

pkg/crd/markers/validation.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ var FieldOnlyMarkers = []*definitionWithHelp{
8686
must(markers.MakeAnyTypeDefinition("kubebuilder:default", markers.DescribesField, Default{})).
8787
WithHelp(Default{}.Help()),
8888

89+
must(markers.MakeAnyTypeDefinition("kubebuilder:example", markers.DescribesField, Example{})).
90+
WithHelp(Example{}.Help()),
91+
8992
must(markers.MakeDefinition("kubebuilder:validation:EmbeddedResource", markers.DescribesField, XEmbeddedResource{})).
9093
WithHelp(XEmbeddedResource{}.Help()),
9194

@@ -222,6 +225,19 @@ type Default struct {
222225
Value interface{}
223226
}
224227

228+
// +controllertools:marker:generateHelp:category="CRD validation"
229+
// Example sets the example value for this field.
230+
//
231+
// An example value will be accepted as any value valid for the
232+
// field. Formatting for common types include: boolean: `true`, string:
233+
// `Cluster`, numerical: `1.24`, array: `{1,2}`, object: `{policy:
234+
// "delete"}`). Examples should be defined in pruned form, and only best-effort
235+
// validation will be performed. Full validation of an example requires
236+
// submission of the containing CRD to an apiserver.
237+
type Example struct {
238+
Value interface{}
239+
}
240+
225241
// +controllertools:marker:generateHelp:category="CRD processing"
226242
// PreserveUnknownFields stops the apiserver from pruning fields which are not specified.
227243
//
@@ -465,6 +481,15 @@ func (m Default) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
465481
return nil
466482
}
467483

484+
func (m Example) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
485+
marshalledExample, err := json.Marshal(m.Value)
486+
if err != nil {
487+
return err
488+
}
489+
schema.Example = &apiext.JSON{Raw: marshalledExample}
490+
return nil
491+
}
492+
468493
func (m XPreserveUnknownFields) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
469494
defTrue := true
470495
schema.XPreserveUnknownFields = &defTrue

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/testdata/cronjob_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,17 @@ type CronJobSpec struct {
107107

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

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

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

120123
// This tests that pattern validator is properly applied.

pkg/crd/testdata/gen/bar.example.com_foos.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ spec:
3838
default: fooDefaultString
3939
description: This tests that defaulted fields are stripped for v1beta1,
4040
but not for v1
41+
example: fooExampleString
4142
type: string
4243
required:
4344
- defaultedString

pkg/crd/testdata/gen/foo_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type FooSpec struct {
2828
// This tests that defaulted fields are stripped for v1beta1,
2929
// but not for v1
3030
// +kubebuilder:default=fooDefaultString
31+
// +kubebuilder:example=fooExampleString
3132
DefaultedString string `json:"defaultedString"`
3233
}
3334
type FooStatus struct{}

pkg/crd/testdata/gen/zoo/bar.example.com_zooes.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ spec:
3838
default: zooDefaultString
3939
description: This tests that defaulted fields are stripped for v1beta1,
4040
but not for v1
41+
example: zooExampleString
4142
type: string
4243
required:
4344
- defaultedString

pkg/crd/testdata/gen/zoo/zoo_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ZooSpec struct {
2828
// This tests that defaulted fields are stripped for v1beta1,
2929
// but not for v1
3030
// +kubebuilder:default=zooDefaultString
31+
// +kubebuilder:example=zooExampleString
3132
DefaultedString string `json:"defaultedString"`
3233
}
3334
type ZooStatus struct{}

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ spec:
105105
- nested:
106106
bar: false
107107
description: This tests that object defaulting can be performed.
108+
example:
109+
- nested:
110+
bar: true
111+
foo: baz
112+
- nested:
113+
bar: false
108114
items:
109115
properties:
110116
nested:
@@ -126,12 +132,16 @@ spec:
126132
- a
127133
- b
128134
description: This tests that slice defaulting can be performed.
135+
example:
136+
- a
137+
- b
129138
items:
130139
type: string
131140
type: array
132141
defaultedString:
133142
default: forty-two
134143
description: This tests that primitive defaulting can be performed.
144+
example: forty-two
135145
type: string
136146
embeddedResource:
137147
type: object

pkg/crd/zz_generated.markerhelp.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/rbac/zz_generated.markerhelp.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/webhook/zz_generated.markerhelp.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)