-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from ykulazhenkov/pr-feature-gate
Add featuregate package and use it in controllers
- Loading branch information
Showing
8 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package featuregate | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// FeatureGate provides methods to check state of the feature | ||
type FeatureGate interface { | ||
// IsEnabled returns state of the feature, | ||
// if feature name is unknown will always return false | ||
IsEnabled(feature string) bool | ||
// Init set state for the features from the provided map. | ||
// completely removes the previous state | ||
Init(features map[string]bool) | ||
// String returns string representation of the feature state | ||
String() string | ||
} | ||
|
||
// New returns default implementation of the FeatureGate interface | ||
func New() FeatureGate { | ||
return &featureGate{ | ||
lock: &sync.RWMutex{}, | ||
state: map[string]bool{}, | ||
} | ||
} | ||
|
||
type featureGate struct { | ||
lock *sync.RWMutex | ||
state map[string]bool | ||
} | ||
|
||
// IsEnabled returns state of the feature, | ||
// if feature name is unknown will always return false | ||
func (fg *featureGate) IsEnabled(feature string) bool { | ||
fg.lock.RLock() | ||
defer fg.lock.RUnlock() | ||
return fg.state[feature] | ||
} | ||
|
||
// Init set state for the features from the provided map. | ||
// completely removes the previous state | ||
func (fg *featureGate) Init(features map[string]bool) { | ||
fg.lock.Lock() | ||
defer fg.lock.Unlock() | ||
fg.state = make(map[string]bool, len(features)) | ||
for k, v := range features { | ||
fg.state[k] = v | ||
} | ||
} | ||
|
||
// String returns string representation of the feature state | ||
func (fg *featureGate) String() string { | ||
fg.lock.RLock() | ||
defer fg.lock.RUnlock() | ||
var result strings.Builder | ||
var sep string | ||
for k, v := range fg.state { | ||
result.WriteString(fmt.Sprintf("%s%s:%t", sep, k, v)) | ||
sep = ", " | ||
} | ||
return result.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package featuregate | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("FeatureGate", func() { | ||
Context("IsEnabled", func() { | ||
It("return false for unknown feature", func() { | ||
Expect(New().IsEnabled("something")).To(BeFalse()) | ||
}) | ||
}) | ||
Context("Init", func() { | ||
It("should update the state", func() { | ||
f := New() | ||
f.Init(map[string]bool{"feat1": true, "feat2": false}) | ||
Expect(f.IsEnabled("feat1")).To(BeTrue()) | ||
Expect(f.IsEnabled("feat2")).To(BeFalse()) | ||
}) | ||
}) | ||
Context("String", func() { | ||
It("no features", func() { | ||
Expect(New().String()).To(Equal("")) | ||
}) | ||
It("print feature state", func() { | ||
f := New() | ||
f.Init(map[string]bool{"feat1": true, "feat2": false}) | ||
Expect(f.String()).To(And(ContainSubstring("feat1:true"), ContainSubstring("feat2:false"))) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package featuregate | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"go.uber.org/zap/zapcore" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
func TestFeatureGate(t *testing.T) { | ||
log.SetLogger(zap.New( | ||
zap.WriteTo(GinkgoWriter), | ||
zap.Level(zapcore.Level(-2)), | ||
zap.UseDevMode(true))) | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Package featuregate Suite") | ||
} |