Skip to content

✨ Add k8s:required and k8s:optional markers #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -97,7 +97,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
9 changes: 4 additions & 5 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,15 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
}
// explicitly required - kubebuilder
props.Required = append(props.Required, fieldName)
case field.Markers.Get("optional") != nil:
// explicitly optional - kubernetes
case field.Markers.Get("required") != nil:
case field.Markers.Get("optional") != nil, field.Markers.Get("k8s:optional") != nil:
// explicitly optional
case field.Markers.Get("required") != nil, field.Markers.Get("k8s:required") != nil:
if exactlyOneOf.Has(fieldName) || atMostOneOf.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
}
// explicitly required - kubernetes
// explicitly required
props.Required = append(props.Required, fieldName)

// if this package isn't set to optional default...
case defaultMode == "required":
// ...everything that's not inline / omitempty is required
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 @@ -9137,6 +9143,7 @@ spec:
- defaultedString
- doubleDefaultedString
- embeddedResource
- explicitlyRequiredK8s
- explicitlyRequiredKubebuilder
- explicitlyRequiredKubernetes
- float64WithValidations
Expand Down