@@ -25,32 +25,40 @@ import (
2525 "k8s.io/client-go/rest"
2626)
2727
28- var FeatureGates Gates
28+ var defaultFeatureGates Gates
29+
30+ func Stage (key Feature ) StagedFeature {
31+ if defaultFeatureGates == nil {
32+ panic ("please init featureGates before use it" )
33+ }
34+
35+ return defaultFeatureGates .Stage (key )
36+ }
2937
3038type Gates interface {
3139 Stage (key Feature ) StagedFeature
3240}
3341
3442type StagedFeature interface {
35- Enabled (Stage ) bool
43+ Enabled (FeatureStage ) bool
3644}
3745
3846type featureGates struct {
3947 feats map [Feature ]spec
4048}
4149
42- type Stage int
50+ type FeatureStage int
4351
4452const (
45- INVAILD Stage = 0
53+ INVAILD FeatureStage = 0
4654
47- ALPHA Stage = 1 << iota
55+ ALPHA FeatureStage = 1 << iota
4856 BETA
4957 STABLE
5058 ANY = ALPHA | BETA | STABLE
5159)
5260
53- func stageFromString (s string ) Stage {
61+ func stageFromString (s string ) FeatureStage {
5462 switch s {
5563 case "ALPHA" :
5664 return ALPHA
@@ -65,10 +73,10 @@ func stageFromString(s string) Stage {
6573
6674type spec struct {
6775 enabled bool
68- stage Stage
76+ stage FeatureStage
6977}
7078
71- func (s spec ) Enabled (stage Stage ) bool {
79+ func (s spec ) Enabled (stage FeatureStage ) bool {
7280 if s .stage & stage == 0 {
7381 return false
7482 }
@@ -80,6 +88,9 @@ func (g *featureGates) Stage(key Feature) StagedFeature {
8088}
8189
8290func MustInitFeatureGates (cfg * rest.Config ) {
91+ if defaultFeatureGates != nil {
92+ return
93+ }
8394 gates , err := NewFeatureGates (cfg )
8495 if err != nil {
8596 // TODO: use a common panic util to panic
@@ -88,7 +99,7 @@ func MustInitFeatureGates(cfg *rest.Config) {
8899
89100 fmt .Println ("init feature gates" )
90101
91- FeatureGates = gates
102+ defaultFeatureGates = gates
92103}
93104
94105func NewFeatureGates (cfg * rest.Config ) (Gates , error ) {
0 commit comments