@@ -139,11 +139,11 @@ var ValidationIshMarkers = []*definitionWithHelp{
139139 must (markers .MakeAnyTypeDefinition ("kubebuilder:title" , markers .DescribesType , Title {})).
140140 WithHelp (Title {}.Help ()),
141141
142+ // Feature gate markers for both fields and types (separate registrations)
142143 must (markers .MakeDefinition ("kubebuilder:featuregate" , markers .DescribesField , FeatureGate ("" ))).
143144 WithHelp (FeatureGate ("" ).Help ()),
144-
145- must (markers .MakeDefinition ("kubebuilder:validation:featureGate" , markers .DescribesField , FeatureGateValidation {})).
146- WithHelp (markers .SimpleHelp ("CRD validation feature gates" , "applies validation rules conditionally based on feature gate enablement." )),
145+ must (markers .MakeDefinition ("kubebuilder:featuregate" , markers .DescribesType , FeatureGate ("" ))).
146+ WithHelp (FeatureGate ("" ).Help ()),
147147}
148148
149149func init () {
@@ -399,6 +399,31 @@ type ExactlyOneOf []string
399399// +controllertools:marker:generateHelp:category="CRD validation"
400400type AtLeastOneOf []string
401401
402+ // FeatureGate marks a field or type to be conditionally included based on feature gate enablement.
403+ //
404+ // Fields or types marked with +kubebuilder:featuregate will only be included in generated CRDs
405+ // when the specified feature gate expression evaluates to true via the crd:featureGates parameter.
406+ //
407+ // Supported formats:
408+ // - Single gate: +kubebuilder:featuregate=alpha
409+ // - OR expression: +kubebuilder:featuregate=alpha|beta (true if ANY gate is enabled)
410+ // - AND expression: +kubebuilder:featuregate=alpha&beta (true if ALL gates are enabled)
411+ // - Mixed with precedence: +kubebuilder:featuregate=alpha&beta|gamma (equivalent to (alpha&beta)|gamma)
412+ // - Explicit precedence: +kubebuilder:featuregate=(alpha|beta)&gamma
413+ //
414+ // Operator precedence follows standard conventions: & (AND) has higher precedence than | (OR).
415+ // Use parentheses for explicit grouping when needed.
416+ // +controllertools:marker:generateHelp:category="CRD feature gates"
417+ type FeatureGate string
418+
419+ // ApplyToSchema does nothing for feature gates - they are processed by the generator
420+ // to conditionally include/exclude fields and types.
421+ func (FeatureGate ) ApplyToSchema (schema * apiext.JSONSchemaProps ) error {
422+ // Feature gates don't modify the schema directly.
423+ // They are processed by the generator to conditionally include/exclude fields.
424+ return nil
425+ }
426+
402427func (m Maximum ) ApplyToSchema (schema * apiext.JSONSchemaProps ) error {
403428 if ! hasNumericType (schema ) {
404429 return fmt .Errorf ("must apply maximum to a numeric value, found %s" , schema .Type )
@@ -756,44 +781,3 @@ func fieldsToOneOfCelRuleStr(fields []string) string {
756781}
757782
758783// +controllertools:marker:generateHelp:category="CRD validation feature gates"
759-
760- // FeatureGateValidation marks a validation constraint to be conditionally applied based on feature gate enablement.
761- // This allows validation rules to be enabled/disabled based on feature gates.
762- // The validation parameter accepts the same values as standard kubebuilder:validation markers.
763- //
764- // Examples:
765- // - +kubebuilder:validation:featureGate=alpha,rule="Maximum=100"
766- // - +kubebuilder:validation:featureGate=alpha|beta,rule="MinLength=5"
767- // - +kubebuilder:validation:featureGate=(alpha&beta)|gamma,rule="Pattern=^[a-z]+$"
768- type FeatureGateValidation struct {
769- // FeatureGate specifies the feature gate expression that must be satisfied
770- // for this validation rule to be applied. Supports complex expressions with
771- // AND (&), OR (|) operators and parentheses for precedence.
772- FeatureGate string `marker:"featureGate"`
773-
774- // Rule specifies the validation rule to apply when the feature gate is enabled.
775- // This should be a valid validation marker rule (e.g., "Maximum=100", "MinLength=5").
776- Rule string `marker:"rule"`
777- }
778-
779- // ApplyToSchema applies the validation rule to the schema if the feature gate is enabled.
780- func (f FeatureGateValidation ) ApplyToSchema (schema * apiext.JSONSchemaProps ) error {
781- // This will be called by the schema generation with access to the context
782- // The actual feature gate evaluation will be handled by the caller
783- return fmt .Errorf ("FeatureGateValidation cannot be applied directly - use feature gate context" )
784- }
785-
786- // SupportsFeatureGate indicates this marker supports feature gates
787- func (f FeatureGateValidation ) SupportsFeatureGate () bool {
788- return true
789- }
790-
791- // GetFeatureGate returns the feature gate expression
792- func (f FeatureGateValidation ) GetFeatureGate () string {
793- return f .FeatureGate
794- }
795-
796- // GetValidationRule returns the validation rule to apply
797- func (f FeatureGateValidation ) GetValidationRule () string {
798- return f .Rule
799- }
0 commit comments