-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨ Add feature gates support for experimental API fields #4973
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
wazery
wants to merge
7
commits into
kubernetes-sigs:master
Choose a base branch
from
wazery:w-feature-gates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,937
−8
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba64bd7
feat(feature-gates) implement feature gates for #849
wazery f6b0247
Add feature gate discovery tests and improve parser
wazery 270d561
Enhance docs book integration with feature gates documentation
wazery b52ac9a
change the tests to use gomega to adhere to project standard
wazery 066bee6
rewrite the e2e test to adhere to project style
wazery 1e2aca0
fix tests for feature gates
wazery a1ae5ed
handle feature gates in docs scaffolds
wazery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# Feature Gates | ||
|
||
Feature gates allow you to enable or disable experimental features in your Kubebuilder controllers. This is similar to how Kubernetes core uses feature gates to manage experimental functionality. | ||
|
||
## Quick Start | ||
|
||
### Marking Fields | ||
|
||
```go | ||
type MyResourceSpec struct { | ||
// Standard field | ||
Name string `json:"name"` | ||
|
||
// Experimental field | ||
// +feature-gate experimental-bar | ||
Bar *string `json:"bar,omitempty"` | ||
} | ||
``` | ||
|
||
### Running with Feature Gates | ||
|
||
```bash | ||
# Enable feature gates | ||
./manager --feature-gates=experimental-bar | ||
|
||
# Multiple gates | ||
./manager --feature-gates=experimental-bar,advanced-features | ||
|
||
# Mixed states | ||
./manager --feature-gates=experimental-bar=true,advanced-features=false | ||
``` | ||
|
||
## Overview | ||
|
||
Feature gates provide a mechanism to: | ||
- Control the availability of experimental features | ||
- Enable gradual rollout of new functionality | ||
- Maintain backward compatibility during API evolution | ||
|
||
## Usage | ||
|
||
### Marking Fields with Feature Gates | ||
|
||
Use the `+feature-gate` marker to mark experimental fields in your API types: | ||
|
||
```go | ||
type MyResourceSpec struct { | ||
// Standard field | ||
Name string `json:"name"` | ||
|
||
// Experimental field that requires the "experimental-bar" feature gate | ||
// +feature-gate experimental-bar | ||
Bar *string `json:"bar,omitempty"` | ||
} | ||
``` | ||
|
||
### Running with Feature Gates | ||
|
||
Enable feature gates when running your controller: | ||
|
||
```bash | ||
# Enable a single feature gate | ||
./manager --feature-gates=experimental-bar | ||
|
||
# Enable multiple feature gates | ||
./manager --feature-gates=experimental-bar,advanced-features | ||
|
||
# Mixed enabled/disabled states | ||
./manager --feature-gates=experimental-bar=true,advanced-features=false | ||
``` | ||
|
||
### Feature Gate Formats | ||
|
||
The `--feature-gates` flag accepts: | ||
- `feature1` - Enables feature1 (defaults to true) | ||
- `feature1=true` - Explicitly enables feature1 | ||
- `feature1=false` - Explicitly disables feature1 | ||
- `feature1,feature2` - Enables both features | ||
- `feature1=true,feature2=false` - Mixed states | ||
|
||
## Best Practices | ||
|
||
### Naming Conventions | ||
|
||
Use descriptive, lowercase names with hyphens: | ||
- `experimental-bar` | ||
- `advanced-features` | ||
- `ExperimentalBar` | ||
- `advanced_features` | ||
|
||
### Documentation | ||
|
||
Always document feature-gated fields: | ||
|
||
```go | ||
// Bar is an experimental field that requires the "experimental-bar" feature gate | ||
// +feature-gate experimental-bar | ||
// +optional | ||
Bar *string `json:"bar,omitempty"` | ||
``` | ||
|
||
### Gradual Rollout Strategy | ||
|
||
1. **Alpha**: Feature behind feature gate | ||
2. **Beta**: Feature enabled by default, gate for opt-out | ||
3. **Stable**: Remove feature gate, feature always available | ||
|
||
## Future Integration | ||
|
||
When [controller-tools supports feature gates](https://github.com/kubernetes-sigs/controller-tools/issues/1238), you'll be able to use: | ||
|
||
```go | ||
// +kubebuilder:feature-gate=experimental-bar | ||
// +optional | ||
Bar *string `json:"bar,omitempty"` | ||
``` | ||
|
||
This will automatically exclude the field from CRD schemas when the feature gate is disabled. | ||
|
||
## Examples | ||
|
||
### Basic Example | ||
|
||
```go | ||
type CronJobSpec struct { | ||
// Standard fields | ||
Schedule string `json:"schedule"` | ||
|
||
// Experimental timezone support | ||
// +feature-gate timezone-support | ||
Timezone *string `json:"timezone,omitempty"` | ||
} | ||
``` | ||
|
||
### Controller Logic | ||
|
||
```go | ||
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
// Check if timezone feature is enabled | ||
if featureGates.IsEnabled("timezone-support") { | ||
// Use timezone-aware scheduling | ||
return r.reconcileWithTimezone(ctx, req) | ||
} | ||
|
||
// Fall back to standard scheduling | ||
return r.reconcileStandard(ctx, req) | ||
} | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
### Common Issues | ||
|
||
1. **Feature gate not discovered**: Ensure the `+feature-gate` marker is on the line before the field | ||
2. **Invalid feature gate name**: Use lowercase with hyphens only | ||
3. **Validation errors**: Check that all specified gates are available | ||
|
||
### Debugging | ||
|
||
Enable debug logging to see feature gate discovery: | ||
|
||
```bash | ||
./manager --feature-gates=experimental-bar --zap-log-level=debug | ||
``` | ||
|
||
## Implementation Status | ||
|
||
### Production Ready | ||
|
||
- Feature gate discovery and validation | ||
- Controller integration with `--feature-gates` flag | ||
- Scaffolding integration for new projects | ||
|
||
### Future Enhancement | ||
|
||
- CRD schema modification (requires [controller-tools support](https://github.com/kubernetes-sigs/controller-tools/issues/1238)) | ||
|
||
When [controller-tools supports feature gates](https://github.com/kubernetes-sigs/controller-tools/issues/1238), you'll be able to use: | ||
|
||
```go | ||
// +kubebuilder:feature-gate=experimental-bar | ||
// +optional | ||
Bar *string `json:"bar,omitempty"` | ||
``` | ||
|
||
This will automatically exclude the field from CRD schemas when the feature gate is disabled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.