Skip to content

Commit cf8f8c9

Browse files
authored
Merge pull request #228 from damemi/fix-tags
🐛Add FieldOnlyMarkers to AllDefinitions and update Nullable
2 parents abcf7d8 + 81a6fa6 commit cf8f8c9

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

pkg/crd/markers/validation.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.
5858
Enum(nil),
5959
Format(""),
6060
Type(""),
61-
Nullable(false),
6261
)
6362

6463
// FieldOnlyMarkers list field-specific validation markers (i.e. those markers that don't make
@@ -67,6 +66,7 @@ var FieldOnlyMarkers = []*markers.Definition{
6766
markers.Must(markers.MakeDefinition("kubebuilder:validation:Required", markers.DescribesField, struct{}{})),
6867
markers.Must(markers.MakeDefinition("kubebuilder:validation:Optional", markers.DescribesField, struct{}{})),
6968
markers.Must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})),
69+
markers.Must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})),
7070
}
7171

7272
func init() {
@@ -77,6 +77,8 @@ func init() {
7777
typDef.Target = markers.DescribesType
7878
AllDefinitions = append(AllDefinitions, &typDef)
7979
}
80+
81+
AllDefinitions = append(AllDefinitions, FieldOnlyMarkers...)
8082
}
8183

8284
type Maximum int
@@ -96,7 +98,7 @@ type UniqueItems bool
9698
type Enum []interface{}
9799
type Format string
98100
type Type string
99-
type Nullable bool
101+
type Nullable struct{}
100102
type Required struct{}
101103
type Optional struct{}
102104

@@ -222,6 +224,6 @@ func (m Type) ApplyToSchema(schema *v1beta1.JSONSchemaProps) error {
222224
func (m Type) ApplyFirst() {}
223225

224226
func (m Nullable) ApplyToSchema(schema *v1beta1.JSONSchemaProps) error {
225-
schema.Nullable = bool(m)
227+
schema.Nullable = true
226228
return nil
227229
}

pkg/crd/parser_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
8080
Expect(yaml.Unmarshal(expectedFile, &crd)).To(Succeed())
8181

8282
By("comparing the two")
83-
Expect(parser.CustomResourceDefinitions[groupKind]).To(Equal(crd), "%s", cmp.Diff(parser.CustomResourceDefinitions[groupKind], crd))
83+
Expect(parser.CustomResourceDefinitions[groupKind]).To(Equal(crd), "type not as expected, check pkg/crd/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(parser.CustomResourceDefinitions[groupKind], crd))
8484
})
8585
})

pkg/crd/testdata/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# CRD Integration Test testdata
2+
3+
This contains a tiny module used for testdata for the CRD integration
4+
test. The directory should always be called testdata, so Go treats it
5+
specially.
6+
7+
The `cronjob_types.go` file contains the input types, and is loosely based
8+
on the CronJob tutorial from the [KubeBuilder
9+
Book](https://book.kubebuilder.io/cronjob-tutorial.html), but with added
10+
fields to test additional markers and generation behavior.
11+
12+
If you add a new marker, re-generate the golden output file,
13+
`testdata.kubebuilder.io_cronjobs.yaml`, with:
14+
15+
```bash
16+
$ /path/to/current/build/of/controller-gen crd paths=. output:dir=.
17+
```
18+
19+
Make sure you review the diff to ensure that it only contains the desired
20+
changes!
21+
22+
If you didn't add a new marker and this output changes, make sure you have
23+
a good explanation for why generated output needs to change!

pkg/crd/testdata/cronjob_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type CronJobSpec struct {
6262
// This tests byte slice schema generation.
6363
BinaryName []byte `json:"binaryName"`
6464

65+
// This tests that nullable works correctly
66+
// +nullable
67+
CanBeNull string `json:"canBeNull"`
68+
6569
// Specifies the job that will be created when executing a CronJob.
6670
JobTemplate batchv1beta1.JobTemplateSpec `json:"jobTemplate"`
6771

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ spec:
427427
description: This tests byte slice schema generation.
428428
format: byte
429429
type: string
430+
canBeNull:
431+
description: This tests that nullable works correctly
432+
nullable: true
433+
type: string
430434
concurrencyPolicy:
431435
description: 'Specifies how to treat concurrent executions of a Job.
432436
Valid values are: - "Allow" (default): allows CronJobs to run concurrently;
@@ -6197,6 +6201,7 @@ spec:
61976201
type: boolean
61986202
required:
61996203
- binaryName
6204+
- canBeNull
62006205
- jobTemplate
62016206
- schedule
62026207
type: object

0 commit comments

Comments
 (0)