Skip to content

Commit 7e84d47

Browse files
Merge pull request #132276 from michaelasp/warningemu
Add warnings for use of Alpha features with Emulated Version Kubernetes-commit: 06b62dcae7b2e11dbfc4aea2e0b8d5ced2cf26d9
2 parents ef3b5b4 + f9ab22d commit 7e84d47

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

compatibility/registry.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,32 @@ func (r *componentGlobalsRegistry) Validate() []error {
400400
defer r.mutex.Unlock()
401401
for _, globals := range r.componentGlobals {
402402
errs = append(errs, globals.effectiveVersion.Validate()...)
403+
var features map[featuregate.Feature]featuregate.FeatureSpec
403404
if globals.featureGate != nil {
404405
errs = append(errs, globals.featureGate.Validate()...)
406+
features = globals.featureGate.GetAll()
407+
}
408+
binaryVersion := globals.effectiveVersion.BinaryVersion()
409+
emulatedVersion := globals.effectiveVersion.EmulationVersion()
410+
if binaryVersion.GreaterThan(emulatedVersion) {
411+
if enabled := enabledAlphaFeatures(features, globals); len(enabled) != 0 {
412+
klog.Warningf("component has alpha features enabled in emulated version, this is unsupported: features=%v", enabled)
413+
}
405414
}
406415
}
407416
return errs
408417
}
409418

419+
func enabledAlphaFeatures(features map[featuregate.Feature]featuregate.FeatureSpec, globals *ComponentGlobals) []string {
420+
var enabled []string
421+
for feat, featSpec := range features {
422+
if featSpec.PreRelease == featuregate.Alpha && globals.featureGate.Enabled(feat) {
423+
enabled = append(enabled, string(feat))
424+
}
425+
}
426+
return enabled
427+
}
428+
410429
func (r *componentGlobalsRegistry) SetEmulationVersionMapping(fromComponent, toComponent string, f VersionMapping) error {
411430
if f == nil {
412431
return nil

compatibility/registry_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package compatibility
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122
"strings"
2223
"testing"
2324

2425
"github.com/spf13/pflag"
25-
2626
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2727
"k8s.io/apimachinery/pkg/util/version"
2828
"k8s.io/component-base/featuregate"
@@ -476,3 +476,45 @@ func assertVersionEqualTo(t *testing.T, ver *version.Version, expectedVer string
476476
}
477477
t.Errorf("expected: %s, got %s", expectedVer, ver.String())
478478
}
479+
480+
func Test_enabledAlphaFeatures(t *testing.T) {
481+
features := map[featuregate.Feature]featuregate.FeatureSpec{
482+
"myFeat": {
483+
PreRelease: featuregate.Alpha,
484+
},
485+
"myOtherFeat": {
486+
PreRelease: featuregate.Beta,
487+
},
488+
"otherFeatDisabled": {
489+
PreRelease: featuregate.Alpha,
490+
},
491+
}
492+
493+
alphaGate := featuregate.NewFeatureGate()
494+
if err := alphaGate.Add(features); err != nil {
495+
t.Fatalf("Unable to add features, %s", err)
496+
}
497+
498+
err := alphaGate.SetFromMap(
499+
map[string]bool{
500+
"myFeat": true,
501+
"myOtherFeat": true,
502+
"otherFeatDisabled": false,
503+
},
504+
)
505+
if err != nil {
506+
t.Fatalf("Unable to set feature gate, %s", err)
507+
}
508+
509+
globals := &ComponentGlobals{
510+
featureGate: alphaGate,
511+
}
512+
513+
want := []string{
514+
"myFeat",
515+
}
516+
517+
if got := enabledAlphaFeatures(features, globals); !reflect.DeepEqual(got, want) {
518+
t.Errorf("enabledAlphaFeatures() = %v, want %v", got, want)
519+
}
520+
}

0 commit comments

Comments
 (0)