Merge pull request #132276 from michaelasp/warningemu

Add warnings for use of Alpha features with Emulated Version
This commit is contained in:
Kubernetes Prow Robot
2025-07-15 12:06:23 -07:00
committed by GitHub
3 changed files with 71 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import (
"k8s.io/apiserver/pkg/server/resourceconfig"
serverstore "k8s.io/apiserver/pkg/server/storage"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
)
// APIEnablementOptions contains the options for which resources to turn on and off.
@@ -105,6 +106,14 @@ func (s *APIEnablementOptions) ApplyTo(c *server.Config, defaultResourceConfig *
c.MergedResourceConfig = mergedResourceConfig
if c.EffectiveVersion.BinaryVersion() != c.EffectiveVersion.EmulationVersion() {
for _, version := range registry.PrioritizedVersionsAllGroups() {
if strings.Contains(version.Version, "alpha") {
klog.Warning("alpha api enabled with emulated groupVersion, this is unsupported, proceed at your own risk: api=", version.String())
}
}
}
return err
}

View File

@@ -400,13 +400,32 @@ func (r *componentGlobalsRegistry) Validate() []error {
defer r.mutex.Unlock()
for _, globals := range r.componentGlobals {
errs = append(errs, globals.effectiveVersion.Validate()...)
var features map[featuregate.Feature]featuregate.FeatureSpec
if globals.featureGate != nil {
errs = append(errs, globals.featureGate.Validate()...)
features = globals.featureGate.GetAll()
}
binaryVersion := globals.effectiveVersion.BinaryVersion()
emulatedVersion := globals.effectiveVersion.EmulationVersion()
if binaryVersion.GreaterThan(emulatedVersion) {
if enabled := enabledAlphaFeatures(features, globals); len(enabled) != 0 {
klog.Warningf("component has alpha features enabled in emulated version, this is unsupported: features=%v", enabled)
}
}
}
return errs
}
func enabledAlphaFeatures(features map[featuregate.Feature]featuregate.FeatureSpec, globals *ComponentGlobals) []string {
var enabled []string
for feat, featSpec := range features {
if featSpec.PreRelease == featuregate.Alpha && globals.featureGate.Enabled(feat) {
enabled = append(enabled, string(feat))
}
}
return enabled
}
func (r *componentGlobalsRegistry) SetEmulationVersionMapping(fromComponent, toComponent string, f VersionMapping) error {
if f == nil {
return nil

View File

@@ -18,11 +18,11 @@ package compatibility
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/spf13/pflag"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/component-base/featuregate"
@@ -476,3 +476,45 @@ func assertVersionEqualTo(t *testing.T, ver *version.Version, expectedVer string
}
t.Errorf("expected: %s, got %s", expectedVer, ver.String())
}
func Test_enabledAlphaFeatures(t *testing.T) {
features := map[featuregate.Feature]featuregate.FeatureSpec{
"myFeat": {
PreRelease: featuregate.Alpha,
},
"myOtherFeat": {
PreRelease: featuregate.Beta,
},
"otherFeatDisabled": {
PreRelease: featuregate.Alpha,
},
}
alphaGate := featuregate.NewFeatureGate()
if err := alphaGate.Add(features); err != nil {
t.Fatalf("Unable to add features, %s", err)
}
err := alphaGate.SetFromMap(
map[string]bool{
"myFeat": true,
"myOtherFeat": true,
"otherFeatDisabled": false,
},
)
if err != nil {
t.Fatalf("Unable to set feature gate, %s", err)
}
globals := &ComponentGlobals{
featureGate: alphaGate,
}
want := []string{
"myFeat",
}
if got := enabledAlphaFeatures(features, globals); !reflect.DeepEqual(got, want) {
t.Errorf("enabledAlphaFeatures() = %v, want %v", got, want)
}
}