-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add featuregate package and use it in controllers #673
Merged
zeeke
merged 2 commits into
k8snetworkplumbingwg:master
from
ykulazhenkov:pr-feature-gate
Apr 4, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field is not used. I guess it will be used in some future PR, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is preparation for the next PR.