@@ -165,6 +165,8 @@ type MutableVersionedFeatureGate interface {
165
165
// Otherwise, the emulationVersion will be the same as the binary version.
166
166
// If set, the feature defaults and availability will be as if the binary is at the emulated version.
167
167
SetEmulationVersion (emulationVersion * version.Version ) error
168
+ // SetEmulationVersionWithContext is like SetEmulationVersion but accepts a context for controlling behavior.
169
+ SetEmulationVersionWithContext (ctx context.Context , emulationVersion * version.Version ) error
168
170
// GetAll returns a copy of the map of known feature names to versioned feature specs.
169
171
GetAllVersioned () map [Feature ]VersionedSpecs
170
172
// AddVersioned adds versioned feature specs to the featureGate.
@@ -543,6 +545,10 @@ func (f *featureGate) GetAllVersioned() map[Feature]VersionedSpecs {
543
545
}
544
546
545
547
func (f * featureGate ) SetEmulationVersion (emulationVersion * version.Version ) error {
548
+ return f .SetEmulationVersionWithContext (context .Background (), emulationVersion )
549
+ }
550
+
551
+ func (f * featureGate ) SetEmulationVersionWithContext (ctx context.Context , emulationVersion * version.Version ) error {
546
552
if emulationVersion .EqualTo (f .EmulationVersion ()) {
547
553
return nil
548
554
}
@@ -565,7 +571,16 @@ func (f *featureGate) SetEmulationVersion(emulationVersion *version.Version) err
565
571
newVal := featureEnabled (feature , enabled , known , emulationVersion )
566
572
oldVal := featureEnabled (feature , f .enabled .Load ().(map [Feature ]bool ), known , f .EmulationVersion ())
567
573
if newVal != oldVal {
568
- klog .Warningf ("SetEmulationVersion will change already queried feature:%s from %v to %v" , feature , oldVal , newVal )
574
+ // Suppress warning during test restoration to avoid noise in test output
575
+ if ! shouldSuppressWarning (ctx ) {
576
+ warningMsg := fmt .Sprintf ("SetEmulationVersion will change already queried feature:%s from %v to %v" , feature , oldVal , newVal )
577
+ // Try to log to test output first, fallback to global klog
578
+ if logger := getTestLogger (ctx ); logger != nil {
579
+ logger .Logf ("WARNING: %s" , warningMsg )
580
+ } else {
581
+ klog .Warningf ("%s" , warningMsg )
582
+ }
583
+ }
569
584
}
570
585
}
571
586
@@ -669,6 +684,49 @@ func (f *featureGate) AddMetrics() {
669
684
}
670
685
}
671
686
687
+ // contextKey is used to store warning suppression flag in context
688
+ type contextKey struct {}
689
+
690
+ // testLoggerKey is used to store test logger in context
691
+ type testLoggerKey struct {}
692
+
693
+ // WithWarningSuppressionContext returns a context with warning suppression flag
694
+ func WithWarningSuppressionContext (ctx context.Context ) context.Context {
695
+ return context .WithValue (ctx , contextKey {}, true )
696
+ }
697
+
698
+ // WithTestLoggerContext returns a context with test logger for redirecting warnings to test output
699
+ func WithTestLoggerContext (ctx context.Context , logger interface {
700
+ Logf (format string , args ... interface {})
701
+ }) context.Context {
702
+ return context .WithValue (ctx , testLoggerKey {}, logger )
703
+ }
704
+
705
+ // shouldSuppressWarning checks whether warnings should be suppressed
706
+ func shouldSuppressWarning (ctx context.Context ) bool {
707
+ if ctx == nil {
708
+ return false
709
+ }
710
+ suppress , ok := ctx .Value (contextKey {}).(bool )
711
+ return ok && suppress
712
+ }
713
+
714
+ // getTestLogger retrieves test logger from context if available
715
+ func getTestLogger (ctx context.Context ) interface {
716
+ Logf (format string , args ... interface {})
717
+ } {
718
+ if ctx == nil {
719
+ return nil
720
+ }
721
+ logger , ok := ctx .Value (testLoggerKey {}).(interface {
722
+ Logf (format string , args ... interface {})
723
+ })
724
+ if ok {
725
+ return logger
726
+ }
727
+ return nil
728
+ }
729
+
672
730
// KnownFeatures returns a slice of strings describing the FeatureGate's known features.
673
731
// preAlpha, Deprecated and GA features are hidden from the list.
674
732
func (f * featureGate ) KnownFeatures () []string {
0 commit comments