Skip to content
Open
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
5 changes: 4 additions & 1 deletion pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ var FieldOnlyMarkers = []*definitionWithHelp{
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})).
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),

must(markers.MakeDefinition("k8s:required", markers.DescribesField, struct{}{})).
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required.")),
must(markers.MakeDefinition("k8s:optional", markers.DescribesField, struct{}{})).
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),
must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
WithHelp(Nullable{}.Help()),

Expand Down
19 changes: 15 additions & 4 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.

4 changes: 2 additions & 2 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
}
// explicitly required - kubebuilder
props.Required = append(props.Required, fieldName)
case field.Markers.Get("optional") != nil:
case field.Markers.Get("optional") != nil, field.Markers.Get("k8s:optional") != nil:
// explicitly optional - kubernetes
case field.Markers.Get("required") != nil:
case field.Markers.Get("required") != nil, field.Markers.Get("k8s:required") != nil:
if exactlyOneOf.Has(fieldName) || atMostOneOf.Has(fieldName) || atLeastOneOf.Has(fieldName) {
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("field %s is part of OneOf constraint and cannot be marked as required", fieldName), structType))
return props
Expand Down
8 changes: 8 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ type CronJobSpec struct {
// +optional
ExplicitlyOptionalKubernetes string `json:"explicitlyOptionalKubernetes"`

// This tests explicitly optional k8s fields
// +k8s:optional
ExplicitlyOptionalK8s string `json:"explicitlyOptionalK8s"`

// This tests explicitly required kubebuilder fields
// +kubebuilder:validation:Required
ExplicitlyRequiredKubebuilder string `json:"explicitlyRequiredKubebuilder,omitempty"`
Expand All @@ -246,6 +250,10 @@ type CronJobSpec struct {
// +required
ExplicitlyRequiredKubernetes string `json:"explicitlyRequiredKubernetes,omitempty"`

// This tests explicitly required k8s fields
// +k8s:required
ExplicitlyRequiredK8s string `json:"explicitlyRequiredK8s,omitempty"`

// This tests that min/max properties work
MinMaxProperties MinMaxObject `json:"minMaxProperties,omitempty"`

Expand Down
7 changes: 7 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,18 @@ spec:
- 3
type: integer
type: array
explicitlyOptionalK8s:
description: This tests explicitly optional k8s fields
type: string
explicitlyOptionalKubebuilder:
description: This tests explicitly optional kubebuilder fields
type: string
explicitlyOptionalKubernetes:
description: This tests explicitly optional kubernetes fields
type: string
explicitlyRequiredK8s:
description: This tests explicitly required k8s fields
type: string
explicitlyRequiredKubebuilder:
description: This tests explicitly required kubebuilder fields
type: string
Expand Down Expand Up @@ -9174,6 +9180,7 @@ spec:
- defaultedString
- doubleDefaultedString
- embeddedResource
- explicitlyRequiredK8s
- explicitlyRequiredKubebuilder
- explicitlyRequiredKubernetes
- float64WithValidations
Expand Down