forked from openshift/azure-disk-csi-driver-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
featuregate.go
47 lines (39 loc) · 1.41 KB
/
featuregate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package featuregates
import (
"fmt"
configv1 "github.com/openshift/api/config/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
// FeatureGate indicates whether a given feature is enabled or not
// This interface is heavily influenced by k8s.io/component-base, but not exactly compatible.
type FeatureGate interface {
// Enabled returns true if the key is enabled.
Enabled(key configv1.FeatureGateName) bool
// KnownFeatures returns a slice of strings describing the FeatureGate's known features.
KnownFeatures() []configv1.FeatureGateName
}
type featureGate struct {
enabled sets.Set[configv1.FeatureGateName]
disabled sets.Set[configv1.FeatureGateName]
}
func NewFeatureGate(enabled, disabled []configv1.FeatureGateName) FeatureGate {
return &featureGate{
enabled: sets.New[configv1.FeatureGateName](enabled...),
disabled: sets.New[configv1.FeatureGateName](disabled...),
}
}
func (f *featureGate) Enabled(key configv1.FeatureGateName) bool {
if f.enabled.Has(key) {
return true
}
if f.disabled.Has(key) {
return false
}
panic(fmt.Errorf("feature %q is not registered in FeatureGates %v", key, f.KnownFeatures()))
}
func (f *featureGate) KnownFeatures() []configv1.FeatureGateName {
allKnown := sets.NewString()
allKnown.Insert(FeatureGateNamesToStrings(f.enabled.UnsortedList())...)
allKnown.Insert(FeatureGateNamesToStrings(f.disabled.UnsortedList())...)
return StringsToFeatureGateNames(allKnown.List())
}