-
Notifications
You must be signed in to change notification settings - Fork 453
✨ Add feature gate marker support #1259
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
2
commits into
kubernetes-sigs:main
Choose a base branch
from
wazery:w-feature-gate-support
base: main
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.
+4,823
−154
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
Copyright 2025. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crd_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-tools/pkg/crd" | ||
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers" | ||
"sigs.k8s.io/controller-tools/pkg/genall" | ||
"sigs.k8s.io/controller-tools/pkg/loader" | ||
"sigs.k8s.io/controller-tools/pkg/markers" | ||
) | ||
|
||
var _ = Describe("CRD Feature Gate Generation", func() { | ||
var ( | ||
ctx *genall.GenerationContext | ||
out *featureGateOutputRule | ||
featureGateDir string | ||
originalWorkingDir string | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
originalWorkingDir, err = os.Getwd() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
featureGateDir = filepath.Join(originalWorkingDir, "testdata", "featuregates") | ||
|
||
By("switching into featuregates testdata") | ||
err = os.Chdir(featureGateDir) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("loading the roots") | ||
pkgs, err := loader.LoadRoots(".") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(pkgs).To(HaveLen(1)) | ||
|
||
out = &featureGateOutputRule{buf: &bytes.Buffer{}} | ||
ctx = &genall.GenerationContext{ | ||
Collector: &markers.Collector{Registry: &markers.Registry{}}, | ||
Roots: pkgs, | ||
Checker: &loader.TypeChecker{}, | ||
OutputRule: out, | ||
} | ||
Expect(crdmarkers.Register(ctx.Collector.Registry)).To(Succeed()) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("restoring original working directory") | ||
err := os.Chdir(originalWorkingDir) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should not include feature-gated fields when no gates are enabled", func() { | ||
By("calling the generator") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
// No FeatureGates specified | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_none/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
|
||
It("should include only alpha-gated fields when alpha gate is enabled", func() { | ||
By("calling the generator") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
FeatureGates: "alpha=true", | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_alpha/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
|
||
It("should include only beta-gated fields when beta gate is enabled", func() { | ||
By("calling the generator") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
FeatureGates: "beta=true", | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_beta/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
|
||
It("should include both feature-gated fields when both gates are enabled", func() { | ||
By("calling the generator") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
FeatureGates: "alpha=true,beta=true", | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_both/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
|
||
It("should handle complex precedence: (alpha&beta)|gamma", func() { | ||
By("calling the generator with only gamma enabled") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
FeatureGates: "gamma=true", | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_gamma/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
|
||
It("should include all fields when all gates are enabled", func() { | ||
By("calling the generator with all gates enabled") | ||
gen := &crd.Generator{ | ||
CRDVersions: []string{"v1"}, | ||
FeatureGates: "alpha=true,beta=true,gamma=true", | ||
} | ||
Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) | ||
|
||
By("loading the expected YAML") | ||
expectedFile, err := os.ReadFile("output_all/_featuregatetests.yaml") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("comparing the two") | ||
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) | ||
}) | ||
}) | ||
|
||
// Helper types for testing | ||
type featureGateOutputRule struct { | ||
buf *bytes.Buffer | ||
} | ||
|
||
func (o *featureGateOutputRule) Open(_ *loader.Package, _ string) (io.WriteCloser, error) { | ||
return featureGateNopCloser{o.buf}, nil | ||
} | ||
|
||
type featureGateNopCloser struct { | ||
io.Writer | ||
} | ||
|
||
func (n featureGateNopCloser) Close() error { | ||
return nil | ||
} |
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,36 @@ | ||
/* | ||
Copyright 2025. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package markers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the motivation for this being in a separate file? |
||
|
||
import ( | ||
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
// +controllertools:marker:generateHelp:category="CRD feature gates" | ||
|
||
// FeatureGate marks a field to be conditionally included based on feature gate enablement. | ||
// Fields marked with +kubebuilder:featuregate will only be included in generated CRDs | ||
// when the specified feature gate is enabled via the crd:featureGates parameter. | ||
type FeatureGate string | ||
|
||
// ApplyToSchema does nothing for feature gates - they are processed by the generator | ||
// to conditionally include/exclude fields. | ||
func (FeatureGate) ApplyToSchema(schema *apiext.JSONSchemaProps, field string) error { | ||
// Feature gates don't modify the schema directly. | ||
// They are processed by the generator to conditionally include/exclude fields. | ||
return nil | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you not register the same marker as both a Type and Field level marker without duplicating the marker as we currently have?