Skip to content

Conversation

wazery
Copy link

@wazery wazery commented Aug 11, 2025

Implements +kubebuilder:feature-gate=<gate-name> marker that allows fields to be conditionally included in generated CRDs based on enabled feature gates.

  • Add centralized featuregate package with evaluation and parsing logic
  • Implement CRD feature gate markers for conditional field inclusion
  • Add RBAC feature gate support with multiple gate evaluation
  • Extend Webhook generator with feature gate capabilities
  • Include comprehensive test coverage and golden output files

Addresses #1238, and #600

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 11, 2025
Copy link

linux-foundation-easycla bot commented Aug 11, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wazery
Once this PR has been reviewed and has the lgtm label, please assign joelspeed for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested a review from vincepri August 11, 2025 15:18
@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Aug 11, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @wazery!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 11, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @wazery. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 11, 2025
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keen to seen progress on this issue, thanks @wazery for starting the push forward.

Feature gating is complex when it comes to CRDs. I'd be keen to see if we can also work out a plan for how we will gate specific markers, is that something we can align on as a project before we move forward?

Specifically I've seen needs for feature gated different enum sets, different feature gated XValidations and different values for things like Min/Max items that are feature gated.

But perhaps we actually need to be able to support gating all of the various markers we support?

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 11, 2025
@wazery wazery changed the title ✨ Add feature gate marker support for conditional CRD field inclusion ✨ WIP Add feature gate marker support for conditional CRD field inclusion Aug 11, 2025
@wazery wazery force-pushed the w-feature-gate-support branch from 3001605 to b15c521 Compare August 11, 2025 18:30
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 11, 2025
@wazery wazery force-pushed the w-feature-gate-support branch from b15c521 to de2450b Compare August 11, 2025 18:35
@wazery
Copy link
Author

wazery commented Aug 12, 2025

I'm keen to seen progress on this issue, thanks @wazery for starting the push forward.

Feature gating is complex when it comes to CRDs. I'd be keen to see if we can also work out a plan for how we will gate specific markers, is that something we can align on as a project before we move forward?

Specifically I've seen needs for feature gated different enum sets, different feature gated XValidations and different values for things like Min/Max items that are feature gated.

But perhaps we actually need to be able to support gating all of the various markers we support?

/ok-to-test

Thanks a lot Joel for your review, I truly appreciate it and learn much from it.

As you can see in the current implementation it's very initial, and allows only binary field inclusion so either the entire field is in or out. Yet we can enhance the implementation further to address your cases:

  • Feature-gated enum values
  • Feature-gated validation rules (XValidations)
  • Feature-gated marker values (Min/MaxItems, etc.)
  • Conditional marker application

For example:

Option A: Inline Feature Gate Syntax

// Feature-gated enum values
// +kubebuilder:validation:Enum=stable;production
// +kubebuilder:validation:Enum=alpha;beta;experimental,featureGate=alpha

// Feature-gated validation rules  
// +kubebuilder:validation:XValidation=rule="self.size > 0"
// +kubebuilder:validation:XValidation=rule="self.advanced == true",featureGate=alpha

// Feature-gated min/max values
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MinItems=5,featureGate=strict

Option B: Dedicated Feature Gate Markers

// +kubebuilder:validation:Enum=stable;production
// +kubebuilder:validation:Enum:featureGate=alpha,value=alpha;beta;experimental

// +kubebuilder:validation:XValidation=rule="self.size > 0"  
// +kubebuilder:validation:XValidation:featureGate=alpha,rule="self.advanced == true"

Option C: Block-based Feature Gating

// +kubebuilder:feature-gate:alpha
// +kubebuilder:validation:Enum=alpha;beta;experimental  
// +kubebuilder:validation:XValidation=rule="self.advanced == true"
// +kubebuilder:feature-gate:end

// +kubebuilder:validation:Enum=stable;production  // always applied

I love to hear your thoughts on how do you think we have to move forward on this PR, and truly appreciated your help 🙇.

wazery pushed a commit to wazery/controller-tools that referenced this pull request Aug 12, 2025
Based on feedback from PR kubernetes-sigs#1259 review comments, enhance RBAC feature gates with:

- Support for complex feature gate expressions using OR (|) and AND (&) logic
- Enhanced documentation with clear usage examples
- Validation for feature gate expressions with proper error handling
- Comprehensive test coverage for multiple gate scenarios

New feature gate syntax:
- Single gate: featureGate=alpha
- OR logic: featureGate=alpha|beta (enabled if ANY gate is true)
- AND logic: featureGate=alpha&beta (enabled if ALL gates are true)

Examples:
// +kubebuilder:rbac:featureGate=alpha|beta,groups=apps,resources=deployments,verbs=get;list
// +kubebuilder:rbac:featureGate=alpha&beta,groups="",resources=services,verbs=get;list

Addresses maintainer feedback on expression validation, multiple gate support,
and improved documentation clarity.
@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 12, 2025
wazery added a commit to wazery/controller-tools that referenced this pull request Aug 12, 2025
Based on feedback from PR kubernetes-sigs#1259 review comments, enhance RBAC feature gates with:

- Support for complex feature gate expressions using OR (|) and AND (&) logic
- Enhanced documentation with clear usage examples
- Validation for feature gate expressions with proper error handling
- Comprehensive test coverage for multiple gate scenarios

New feature gate syntax:
- Single gate: featureGate=alpha
- OR logic: featureGate=alpha|beta (enabled if ANY gate is true)
- AND logic: featureGate=alpha&beta (enabled if ALL gates are true)

Examples:
// +kubebuilder:rbac:featureGate=alpha|beta,groups=apps,resources=deployments,verbs=get;list
// +kubebuilder:rbac:featureGate=alpha&beta,groups="",resources=services,verbs=get;list

Addresses maintainer feedback on expression validation, multiple gate support,
and improved documentation clarity.
@wazery wazery force-pushed the w-feature-gate-support branch from ff80782 to cc41ffa Compare August 12, 2025 12:58
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 12, 2025
@JoelSpeed
Copy link
Contributor

From your suggestions, I'd be interested to see if we can move towards Option A, though I don't know how simple that is for markers where we currently don't have keyed values.

So for example, in XValidation, it's easy to add another value as it's already a multi-value marker. For the single value markers, adding an optional additional field isn't something I've looked into (not recently at least), but from the UX perspective sounds like the best IMO

Interested to see if other maintainers have opinions on this project @alvaroaleman @sbueringer (and @everettraven who is a colleague who helps me with API review at RH)

@wazery wazery force-pushed the w-feature-gate-support branch 5 times, most recently from 101f809 to 2e1498d Compare August 26, 2025 20:37
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 26, 2025
@wazery wazery force-pushed the w-feature-gate-support branch from 2e1498d to e53dccf Compare August 26, 2025 20:46
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 26, 2025
@wazery wazery force-pushed the w-feature-gate-support branch from e53dccf to 53df8d4 Compare August 26, 2025 20:51
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 26, 2025
@wazery wazery force-pushed the w-feature-gate-support branch 4 times, most recently from 73f7c9b to e3a9853 Compare August 27, 2025 20:34
@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Aug 28, 2025
@wazery wazery changed the title ✨ WIP Add feature gate marker support for conditional CRD field inclusion ✨ Add feature gate marker support Aug 28, 2025
@wazery wazery marked this pull request as ready for review August 28, 2025 16:04
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 28, 2025
@wazery wazery force-pushed the w-feature-gate-support branch from 0414f0a to 98e49af Compare August 28, 2025 16:05
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Aug 28, 2025
wazery added 2 commits August 28, 2025 18:25
…ok generators

- Add centralized featuregate package with evaluation and parsing logic
- Implement CRD feature gate markers for conditional field inclusion
- Add RBAC feature gate support with multiple gate evaluation
- Extend Webhook generator with feature gate capabilities
- Include comprehensive test coverage and golden output files
Add +kubebuilder:featuregate marker support for conditional CRD type
generation with OR/AND expression support.
@wazery wazery force-pushed the w-feature-gate-support branch from 98e49af to 48dcf3c Compare August 28, 2025 16:25
@wazery
Copy link
Author

wazery commented Aug 28, 2025

PR Summary

Scope: This PR implements comprehensive feature gate support across all controller-tools generators:

What's Supported:

  • CRD generator: Field-level (+kubebuilder:featuregate) and type-level (+kubebuilder:featuregate) feature gates
  • RBAC generator: Feature gates on RBAC markers for conditional permission generation
  • Webhook generator: Feature gates for conditional webhook registration
  • Centralized feature gate utility: Shared featuregate package with expression evaluation, parsing, and validation
  • Addresses issue Support for a feature gate flag/marker when generating CRDs #600 by completing the missing type-level feature gate functionality while providing a unified feature gate system across all generators.

What's NOT Supported (Future Work):

While rewriting the PR to have a centralised utility for supporting feature gates, I found it would require modifying each validation marker definition and the schema generation logic - a significant undertaking that should be its own focused effort. IMHO we can do that in another follow up PR.

Feature-gated validation markers:

// These are NOT supported yet:
// +kubebuilder:validation:Enum=value1;value2,featureGate=alpha
// +kubebuilder:validation:MinItems=5,featureGate=strict  
// +kubebuilder:validation:XValidation=rule="...",featureGate=beta

Also in a follow up PR I can include the following features (a work in progress now)

  • Output Segregation: Directory-based separation
  • Advanced CLI Workflows: Multi-gate, multi-output generation patterns
  • Validation Gate Support: Feature gates on validation markers

These feature requests are mentioned in the issue #1238

Looking forward to your reviews 🙇

@JoelSpeed
Copy link
Contributor

While rewriting the PR to have a centralised utility for supporting feature gates, I found it would require modifying each validation marker definition and the schema generation logic - a significant undertaking that should be its own focused effort. IMHO we can do that in another follow up PR.

I'm currently working with the declarative validation folks and the API review folks upstream to work out what feature gating might look like in terms of markers there.

I think we may want to copy their pattern downstream which is slightly more general, and uses chaining. For example, the +k8s:ifEnabled(<option>) marker must take a "payload". The payload in this case is another marker. So, rather than changing our existing markers, we would need to work out how to support +k8s:ifEnabled(<option>):=+kubebuilder:validation:MinItems:=5

The syntax and behaviour upstream is not quite cemented yet but I'm hoping that during the 1.35 cycle it will be, and that will allow us to start thinking about how this might work in the world of custom resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants