diff --git a/pkg/crd/markers/validation.go b/pkg/crd/markers/validation.go index 839b07f6e..8c27e3ac4 100644 --- a/pkg/crd/markers/validation.go +++ b/pkg/crd/markers/validation.go @@ -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()), diff --git a/pkg/crd/schema.go b/pkg/crd/schema.go index efb09b7c9..7ad488bbe 100644 --- a/pkg/crd/schema.go +++ b/pkg/crd/schema.go @@ -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 diff --git a/pkg/crd/testdata/cronjob_types.go b/pkg/crd/testdata/cronjob_types.go index e496407b2..54d93d704 100644 --- a/pkg/crd/testdata/cronjob_types.go +++ b/pkg/crd/testdata/cronjob_types.go @@ -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"` @@ -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"` diff --git a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml index 8bbbaf1a1..96c5d8595 100644 --- a/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml +++ b/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml @@ -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 @@ -9137,6 +9143,7 @@ spec: - defaultedString - doubleDefaultedString - embeddedResource + - explicitlyRequiredK8s - explicitlyRequiredKubebuilder - explicitlyRequiredKubernetes - float64WithValidations