Skip to content

Commit 8c252b3

Browse files
committed
feat(crd): Add k8s:required and k8s:optional markers\n\nThis commit introduces two new markers, k8s:required and\nk8s:optional, to allow marking fields in CRDs as required or\noptional.\n\nThese IDL tags are introduced as part of the KEP for declarative validation:\nhttps://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/5073-declarative-validation-with-validation-gen/README.md\n\nAdding this in controller-gen will allow understanding native types much\nbetter in CRDs.\n\nThis provides a kubernetes-style alternative to the existing\n+required and +optional markers.
1 parent 647f081 commit 8c252b3

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

pkg/crd/markers/validation.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ var FieldOnlyMarkers = []*definitionWithHelp{
9797
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
9898
must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})).
9999
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),
100-
100+
must(markers.MakeDefinition("k8s:required", markers.DescribesField, struct{}{})).
101+
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
102+
must(markers.MakeDefinition("k8s:optional", markers.DescribesField, struct{}{})).
103+
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),
101104
must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
102105
WithHelp(Nullable{}.Help()),
103106

pkg/crd/schema.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,15 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
469469
}
470470
// explicitly required - kubebuilder
471471
props.Required = append(props.Required, fieldName)
472-
case field.Markers.Get("optional") != nil:
473-
// explicitly optional - kubernetes
474-
case field.Markers.Get("required") != nil:
472+
case field.Markers.Get("optional") != nil, field.Markers.Get("k8s:optional") != nil:
473+
// explicitly optional
474+
case field.Markers.Get("required") != nil, field.Markers.Get("k8s:required") != nil:
475475
if exactlyOneOf.Has(fieldName) || atMostOneOf.Has(fieldName) {
476476
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("field %s is part of OneOf constraint and cannot be marked as required", fieldName), structType))
477477
return props
478478
}
479-
// explicitly required - kubernetes
479+
// explicitly required
480480
props.Required = append(props.Required, fieldName)
481-
482481
// if this package isn't set to optional default...
483482
case defaultMode == "required":
484483
// ...everything that's not inline / omitempty is required

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
@@ -9137,6 +9143,7 @@ spec:
91379143
- defaultedString
91389144
- doubleDefaultedString
91399145
- embeddedResource
9146+
- explicitlyRequiredK8s
91409147
- explicitlyRequiredKubebuilder
91419148
- explicitlyRequiredKubernetes
91429149
- float64WithValidations

0 commit comments

Comments
 (0)