client-go/features: warn when ordering initialization issue

ReplaceFeatureGates logs a warning when the default env var
implementation has been already used.
Such a situation indicates a potential ordering issue and usually is unwanted.

Kubernetes-commit: 04bbd3481f70825eea54b4b154a04d2496dcf652
This commit is contained in:
Lukasz Szaszkiewicz
2024-01-10 17:15:01 +01:00
committed by Kubernetes Publisher
parent ca4f3a73f7
commit e8a81a3a43
3 changed files with 35 additions and 0 deletions

View File

@@ -17,6 +17,9 @@ limitations under the License.
package features
import (
"errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sync/atomic"
)
@@ -99,8 +102,23 @@ func AddFeaturesToExistingFeatureGates(registry Registry) error {
// // then replace client-go's feature gates implementation with your implementation
// clientgofeaturegate.ReplaceFeatureGates(utilfeature.DefaultMutableFeatureGate)
func ReplaceFeatureGates(newFeatureGates Gates) {
if replaceFeatureGatesWithWarningIndicator(newFeatureGates) {
utilruntime.HandleError(errors.New("the default feature gates implementation has already been used and now it's being overwritten. This might lead to unexpected behaviour. Check your initialization order"))
}
}
func replaceFeatureGatesWithWarningIndicator(newFeatureGates Gates) bool {
shouldProduceWarning := false
if defaultFeatureGates, ok := FeatureGates().(*envVarFeatureGates); ok {
if defaultFeatureGates.hasAlreadyReadEnvVar() {
shouldProduceWarning = true
}
}
wrappedFeatureGates := &featureGatesWrapper{newFeatureGates}
featureGates.Store(wrappedFeatureGates)
return shouldProduceWarning
}
func init() {