Skip to content

Commit bf7d6b7

Browse files
authored
Merge pull request #1247 from lalitc375/k8s-required
✨ Add k8s:required and k8s:optional markers
2 parents 8ef4174 + 8093c7e commit bf7d6b7

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

pkg/crd/markers/validation.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ var FieldOnlyMarkers = []*definitionWithHelp{
103103
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
104104
must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})).
105105
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),
106-
106+
must(markers.MakeDefinition("k8s:required", markers.DescribesField, struct{}{})).
107+
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
108+
must(markers.MakeDefinition("k8s:optional", markers.DescribesField, struct{}{})).
109+
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),
107110
must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
108111
WithHelp(Nullable{}.Help()),
109112

pkg/crd/markers/zz_generated.markerhelp.go

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

pkg/crd/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
472472
}
473473
// explicitly required - kubebuilder
474474
props.Required = append(props.Required, fieldName)
475-
case field.Markers.Get("optional") != nil:
475+
case field.Markers.Get("optional") != nil, field.Markers.Get("k8s:optional") != nil:
476476
// explicitly optional - kubernetes
477-
case field.Markers.Get("required") != nil:
477+
case field.Markers.Get("required") != nil, field.Markers.Get("k8s:required") != nil:
478478
if exactlyOneOf.Has(fieldName) || atMostOneOf.Has(fieldName) || atLeastOneOf.Has(fieldName) {
479479
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("field %s is part of OneOf constraint and cannot be marked as required", fieldName), structType))
480480
return props

pkg/crd/testdata/cronjob_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ type CronJobSpec struct {
238238
// +optional
239239
ExplicitlyOptionalKubernetes string `json:"explicitlyOptionalKubernetes"`
240240

241+
// This tests explicitly optional k8s fields
242+
// +k8s:optional
243+
ExplicitlyOptionalK8s string `json:"explicitlyOptionalK8s"`
244+
241245
// This tests explicitly required kubebuilder fields
242246
// +kubebuilder:validation:Required
243247
ExplicitlyRequiredKubebuilder string `json:"explicitlyRequiredKubebuilder,omitempty"`
@@ -246,6 +250,10 @@ type CronJobSpec struct {
246250
// +required
247251
ExplicitlyRequiredKubernetes string `json:"explicitlyRequiredKubernetes,omitempty"`
248252

253+
// This tests explicitly required k8s fields
254+
// +k8s:required
255+
ExplicitlyRequiredK8s string `json:"explicitlyRequiredK8s,omitempty"`
256+
249257
// This tests that min/max properties work
250258
MinMaxProperties MinMaxObject `json:"minMaxProperties,omitempty"`
251259

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,18 @@ spec:
201201
- 3
202202
type: integer
203203
type: array
204+
explicitlyOptionalK8s:
205+
description: This tests explicitly optional k8s fields
206+
type: string
204207
explicitlyOptionalKubebuilder:
205208
description: This tests explicitly optional kubebuilder fields
206209
type: string
207210
explicitlyOptionalKubernetes:
208211
description: This tests explicitly optional kubernetes fields
209212
type: string
213+
explicitlyRequiredK8s:
214+
description: This tests explicitly required k8s fields
215+
type: string
210216
explicitlyRequiredKubebuilder:
211217
description: This tests explicitly required kubebuilder fields
212218
type: string
@@ -9174,6 +9180,7 @@ spec:
91749180
- defaultedString
91759181
- doubleDefaultedString
91769182
- embeddedResource
9183+
- explicitlyRequiredK8s
91779184
- explicitlyRequiredKubebuilder
91789185
- explicitlyRequiredKubernetes
91799186
- float64WithValidations

0 commit comments

Comments
 (0)